-
-
Notifications
You must be signed in to change notification settings - Fork 672
add AliasAssign declarations #11846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add AliasAssign declarations #11846
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Add Alias Assignment | ||
|
|
||
| This adds the ability for an alias declaration inside a template to be | ||
| assigned a new value. For example, the recursive template: | ||
|
|
||
| --- | ||
| template staticMap(alias F, T...) | ||
| { | ||
| static if (T.length == 0) | ||
| alias staticMap = AliasSym!(); | ||
| else | ||
| alias staticMap = AliasSym!(F!(T[0]), staticMap!(T[0 .. T.length])); | ||
| } | ||
| --- | ||
|
|
||
| can now be reworked into an iterative template: | ||
|
|
||
| --- | ||
| template staticMap(alias F, T...) | ||
| { | ||
| alias A = AliasSeq!(); | ||
| static foreach (t; T) | ||
| A = AliasSeq!(A, F!t); // alias assignment here | ||
| alias staticMap = A; | ||
| } | ||
| --- | ||
|
|
||
| Using the iterative approach will eliminate the combinatorial explosion of recursive | ||
| template instantiations, eliminating the associated high memory and runtime costs, | ||
| as well as eliminating the issues with limits on the nesting depth of templates. | ||
| It will eliminate the obtuse error messages generated when deep in recursion. | ||
|
|
||
| The grammar: | ||
|
|
||
| --- | ||
| AliasAssign: | ||
| Identifier = Type; | ||
| --- | ||
|
|
||
| is added to the expansion of DeclDef. The Identifier must resolve to a lexically | ||
| preceding AliasDeclaration: | ||
|
|
||
| --- | ||
| alias Identifier = Type; | ||
| --- | ||
|
|
||
| where the Identifier's match, and both are members of the same TemplateDeclaration. | ||
| Upon semantic processing, when the AliasAssign is encountered the Type in the | ||
| AliasAssign replaces the Type from the corresponding AliasDeclaration or any previous matching | ||
| AliasAssign. | ||
|
|
||
| The AliasAssign grammar was previously rejected by the parser, so adding it | ||
| should not break existing code. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1219,6 +1219,7 @@ extern (C++) class Dsymbol : ASTNode | |
| inout(Declaration) isDeclaration() inout { return null; } | ||
| inout(StorageClassDeclaration) isStorageClassDeclaration() inout { return null; } | ||
| inout(ExpressionDsymbol) isExpressionDsymbol() inout { return null; } | ||
| inout(AliasAssign) isAliasAssign() inout { return null; } | ||
| inout(ThisDeclaration) isThisDeclaration() inout { return null; } | ||
| inout(TypeInfoDeclaration) isTypeInfoDeclaration() inout { return null; } | ||
| inout(TupleDeclaration) isTupleDeclaration() inout { return null; } | ||
|
|
@@ -2113,7 +2114,7 @@ extern (C++) final class ForwardingScopeDsymbol : ScopeDsymbol | |
| } | ||
|
|
||
| /** | ||
| * Class that holds an expression in a Dsymbol wraper. | ||
| * Class that holds an expression in a Dsymbol wrapper. | ||
| * This is not an AST node, but a class used to pass | ||
| * an expression as a function parameter of type Dsymbol. | ||
| */ | ||
|
|
@@ -2132,6 +2133,45 @@ extern (C++) final class ExpressionDsymbol : Dsymbol | |
| } | ||
| } | ||
|
|
||
| /********************************************** | ||
| * Encapsulate assigning to an alias: | ||
| * `identifier = type;` | ||
| * where `identifier` is an AliasDeclaration in scope. | ||
| */ | ||
| extern (C++) final class AliasAssign : Dsymbol | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't see this being added to dsymbol.h |
||
| { | ||
| Identifier ident; /// Dsymbol's ident will be null, as this class is anonymous | ||
| Type type; /// replace previous RHS of AliasDeclaration with `type` | ||
|
|
||
| extern (D) this(const ref Loc loc, Identifier ident, Type type) | ||
| { | ||
| super(loc, null); | ||
| this.ident = ident; | ||
| this.type = type; | ||
| } | ||
|
|
||
| override Dsymbol syntaxCopy(Dsymbol s) | ||
| { | ||
| assert(!s); | ||
| AliasAssign aa = new AliasAssign(loc, ident, type.syntaxCopy()); | ||
| return aa; | ||
| } | ||
|
|
||
| override inout(AliasAssign) isAliasAssign() inout | ||
| { | ||
| return this; | ||
| } | ||
|
|
||
| override const(char)* kind() const | ||
| { | ||
| return "aliasAssign"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "alias assignment" |
||
| } | ||
|
|
||
| override void accept(Visitor v) | ||
| { | ||
| v.visit(this); | ||
| } | ||
| } | ||
|
|
||
| /*********************************************************** | ||
| * Table of Dsymbol's | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is reassignment limited to types? What about aliases for other symbols?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because if this doesn't work, it isn't worth the effort to extend it.