-
Notifications
You must be signed in to change notification settings - Fork 24
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
(WIP) Add support for Yul AST nodes (addresses #163) #166
Open
d1ll0n
wants to merge
28
commits into
Consensys:master
Choose a base branch
from
d1ll0n:yul-typed-ast
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
693d1ac
Initial yul class definitions
143a7a2
initial (untested) modern processors for yul
7b6b668
add YulLiteralKind
1c9703a
make documentation methods generic to avoid type errors on ASTNodeWit…
c7d8b6d
partially implement new yul ast writers
2fd74a2
Add arg extractors for yul nodes
9fb5efe
Add make functions for Yul nodes
d1ll0n 3fc2f9f
Don't try to convert nonexistent AST nodes
d1ll0n 2e37b2f
allow empty parameter lists, disallow empty block
d1ll0n 7a6e93d
YulNode -> YulBlock, add documentation
d1ll0n d0d3103
Add isYulASTNode
d1ll0n e7bc8ca
Add temporary(?) patch for yul node IDs.
d1ll0n 8e7e059
only lookup standard AST nodes when testing total node count
d1ll0n a8167f3
add deepFindIn
d1ll0n 18e4ec1
Partial patch for node formatter
d1ll0n 06866c0
Implement all remaining ASTNodeWriter classes for Yul and mark old ve…
d1ll0n c2fb88d
Add yul export to index
d1ll0n e5b975e
Remove unused variable
d1ll0n 1e45bec
Add make functions for yul identifiers and function calls
d1ll0n 443696a
Add yul builtin function type and function type, as well as u256 defa…
d1ll0n 1452d1d
Add yulTypes with only u256
d1ll0n 348fb4d
Add yul builtin functions
d1ll0n 5e8f01f
Merge branch 'yul-typed-ast' of github.com:d1ll0n/solc-typed-ast into…
d1ll0n 7402bc6
Add isInstanceOf utility function
d1ll0n e8279d4
Add implementation index
d1ll0n 65018da
Add isPure to yul builtins
d1ll0n 1e5a09c
Add support for yul constant evaluation
d1ll0n 692496f
Add tests for yul constant evaluation
d1ll0n 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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,6 @@ | ||
export * from "./declaration"; | ||
export * from "./expression"; | ||
export * from "./meta"; | ||
export * from "./statement"; | ||
export * from "./type"; | ||
export * from "./yul"; |
This file contains 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 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,5 @@ | ||
export * from "./yul_expression"; | ||
export * from "./yul_function_call"; | ||
export * from "./yul_literal"; | ||
export * from "./yul_identifier"; | ||
export * from "./yul_typed_name"; |
This file contains 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,3 @@ | ||
import { YulASTNode } from "../yul_ast_node"; | ||
|
||
export class YulExpression extends YulASTNode {} |
56 changes: 56 additions & 0 deletions
56
src/ast/implementation/yul/expression/yul_function_call.ts
This file contains 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,56 @@ | ||
import { ExternalReferenceType } from "../../../constants"; | ||
import { YulIdentifier } from "./yul_identifier"; | ||
import { YulExpression } from "./yul_expression"; | ||
import { YulFunctionDefinition } from "../statement/yul_function_definition"; | ||
import { YulASTNode } from "../yul_ast_node"; | ||
|
||
export class YulFunctionCall extends YulExpression { | ||
/** | ||
* YulIdentifier that defines the callee | ||
*/ | ||
vFunctionName: YulIdentifier; | ||
|
||
/** | ||
* Call arguments, e.g array with `1` and `2` expressions in `foo(1, 2)` | ||
*/ | ||
vArguments: YulExpression[]; | ||
|
||
constructor( | ||
id: number, | ||
src: string, | ||
functionName: YulIdentifier, | ||
args: YulExpression[], | ||
raw?: any | ||
) { | ||
super(id, src, raw); | ||
this.vFunctionName = functionName; | ||
this.vArguments = args; | ||
|
||
this.acceptChildren(); | ||
} | ||
|
||
get children(): readonly YulASTNode[] { | ||
return this.pickNodes(this.vFunctionName, this.vArguments); | ||
} | ||
|
||
/** | ||
* Identifier of the function name, e.g. `sha3(...)` | ||
*/ | ||
get vIdentifier(): string { | ||
return this.vFunctionName.name; | ||
} | ||
|
||
/** | ||
* Solidity builtin or user-defined function | ||
*/ | ||
get vFunctionCallType(): ExternalReferenceType { | ||
return this.vFunctionName.vIdentifierType; | ||
} | ||
|
||
/** | ||
* Called function definition reference | ||
*/ | ||
get vReferencedDeclaration(): YulFunctionDefinition | undefined { | ||
return this.vFunctionName.vReferencedDeclaration as YulFunctionDefinition; | ||
} | ||
} |
This file contains 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,52 @@ | ||
import { ASTNode } from "../../../ast_node"; | ||
import { ExternalReferenceType } from "../../../constants"; | ||
import { YulExpression } from "./yul_expression"; | ||
|
||
export class YulIdentifier extends YulExpression { | ||
/** | ||
* Name of the identifier | ||
*/ | ||
name: string; | ||
|
||
/** | ||
* Id of the referenced declaration | ||
*/ | ||
referencedDeclaration: number; | ||
|
||
constructor(id: number, src: string, name: string, referencedDeclaration?: number, raw?: any) { | ||
super(id, src, raw); | ||
|
||
this.name = name; | ||
this.referencedDeclaration = referencedDeclaration ?? -1; | ||
} | ||
|
||
/** | ||
* Attribute to access the converted referenced declaration. | ||
* | ||
* Is `undefined` when this is a Solidity internal identifier. | ||
*/ | ||
get vReferencedDeclaration(): ASTNode | undefined { | ||
return this.requiredContext.locate(this.referencedDeclaration); | ||
} | ||
|
||
set vReferencedDeclaration(value: ASTNode | undefined) { | ||
if (value === undefined) { | ||
this.referencedDeclaration = -1; | ||
} else { | ||
if (!this.requiredContext.contains(value)) { | ||
throw new Error(`Node ${value.type}#${value.id} not belongs to a current context`); | ||
} | ||
|
||
this.referencedDeclaration = value.id; | ||
} | ||
} | ||
|
||
/** | ||
* Solidity builtin or user-defined reference | ||
*/ | ||
get vIdentifierType(): ExternalReferenceType { | ||
return this.vReferencedDeclaration | ||
? ExternalReferenceType.UserDefined | ||
: ExternalReferenceType.Builtin; | ||
} | ||
} |
This file contains 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,41 @@ | ||
import { YulLiteralKind } from "../../../constants"; | ||
import { YulExpression } from "./yul_expression"; | ||
|
||
export class YulLiteral extends YulExpression { | ||
/** | ||
* The type of literal: `number`, `string` or `bool` | ||
*/ | ||
kind: YulLiteralKind; | ||
|
||
/** | ||
* Hexadecimal representation of value of the literal symbol | ||
*/ | ||
hexValue: string; | ||
|
||
/** | ||
* Value of the literal symbol | ||
*/ | ||
value: string; | ||
|
||
/** | ||
* Yul type string, e.g.u256 | ||
*/ | ||
typeString: string; | ||
|
||
constructor( | ||
id: number, | ||
src: string, | ||
kind: YulLiteralKind, | ||
value: string, | ||
hexValue: string, | ||
typeString = "", | ||
raw?: any | ||
) { | ||
super(id, src, raw); | ||
|
||
this.kind = kind; | ||
this.value = value; | ||
this.hexValue = hexValue; | ||
this.typeString = typeString; | ||
} | ||
} |
This file contains 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,20 @@ | ||
import { YulExpression } from "./yul_expression"; | ||
|
||
export class YulTypedName extends YulExpression { | ||
/** | ||
* Name of the identifier | ||
*/ | ||
name: string; | ||
|
||
/** | ||
* Yul type string, e.g.u256 | ||
*/ | ||
typeString: string; | ||
|
||
constructor(id: number, src: string, name: string, typeString = "", raw?: any) { | ||
super(id, src, raw); | ||
|
||
this.name = name; | ||
this.typeString = typeString; | ||
} | ||
} |
This file contains 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,3 @@ | ||
export * from "./expression"; | ||
export * from "./statement"; | ||
export * from "./yul_ast_node"; |
This file contains 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,13 @@ | ||
export * from "./yul_assignment"; | ||
export * from "./yul_block"; | ||
export * from "./yul_break"; | ||
export * from "./yul_case"; | ||
export * from "./yul_continue"; | ||
export * from "./yul_expression_statement"; | ||
export * from "./yul_for_loop"; | ||
export * from "./yul_function_definition"; | ||
export * from "./yul_if"; | ||
export * from "./yul_leave"; | ||
export * from "./yul_statement"; | ||
export * from "./yul_switch"; | ||
export * from "./yul_variable_declaration"; |
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.
Another option here is to consider using second standalone
ASTContext
for Yul nodes...It seems a bit more clear and sound, however it would have more impact on code overall. Some pros/cons:
1e5
, as ID space is separated.ASTReader
andASTNodeFactory
would have to track two different AST contexts. MaybeYulNode
s would also be affected if they refer to anyASTNode
s as referenced declarations...Unsure about consequences here. It may look cleaner but may also be impractical in the end. Still, I guess it may worth an attempt to see what would happend.