Skip to content

Commit

Permalink
Merge pull request #82 from ConsenSys/followup-tweaks
Browse files Browse the repository at this point in the history
Introduce `canonicalName` as getter, support `UserDefinedValueTypeDefinition` in `ASTNodeFactory.makeIdentifierFor()`
  • Loading branch information
blitz-1306 authored Nov 16, 2021
2 parents 4564583 + 9aa0611 commit 6a54b43
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 482 deletions.
22 changes: 10 additions & 12 deletions src/ast/ast_node_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ const argExtractionMapping = new Map<ASTNodeConstructor<ASTNode>, (node: any) =>
EnumDefinition,
(node: EnumDefinition): Specific<ConstructorParameters<typeof EnumDefinition>> => [
node.name,
node.canonicalName,
node.vMembers,
node.nameLocation,
node.raw
Expand Down Expand Up @@ -183,7 +182,6 @@ const argExtractionMapping = new Map<ASTNodeConstructor<ASTNode>, (node: any) =>
StructDefinition,
(node: StructDefinition): Specific<ConstructorParameters<typeof StructDefinition>> => [
node.name,
node.canonicalName,
node.scope,
node.visibility,
node.vMembers,
Expand All @@ -197,7 +195,6 @@ const argExtractionMapping = new Map<ASTNodeConstructor<ASTNode>, (node: any) =>
node: UserDefinedValueTypeDefinition
): Specific<ConstructorParameters<typeof UserDefinedValueTypeDefinition>> => [
node.name,
node.canonicalName,
node.underlyingType,
node.nameLocation,
node.raw
Expand Down Expand Up @@ -1032,6 +1029,7 @@ export class ASTNodeFactory {
| ErrorDefinition
| EventDefinition
| EnumDefinition
| UserDefinedValueTypeDefinition
| ImportDirective
): Identifier {
let typeString: string;
Expand Down Expand Up @@ -1060,6 +1058,12 @@ export class ASTNodeFactory {
typeString = result.join(" ");
} else if (target instanceof ContractDefinition) {
typeString = `type(contract ${target.name})`;
} else if (target instanceof StructDefinition) {
typeString = `type(struct ${target.canonicalName} storage pointer)`;
} else if (target instanceof EnumDefinition) {
typeString = `type(enum ${target.canonicalName})`;
} else if (target instanceof UserDefinedValueTypeDefinition) {
typeString = `type(${target.canonicalName})`;
} else if (target instanceof EventDefinition || target instanceof ErrorDefinition) {
const args = target.vParameters.vParameters.map(this.typeExtractor);

Expand All @@ -1071,15 +1075,9 @@ export class ASTNodeFactory {
throw new Error('Target ImportDirective required to have valid "unitAlias"');
}
} else {
const name =
target.vScope instanceof ContractDefinition
? target.vScope.name + "." + target.name
: target.name;

typeString =
target instanceof StructDefinition
? `type(struct ${name} storage pointer)`
: `type(enum ${name})`;
throw new Error(
"ASTNodeFactory.makeIdentifierFor(): Unable to compose typeString for supplied target"
);
}

return this.makeIdentifier(
Expand Down
16 changes: 8 additions & 8 deletions src/ast/implementation/declaration/enum_definition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { enumToIntType } from "../../..";
import { enumToIntType, getUserDefinedTypeFQName } from "../../..";
import { ASTNodeWithChildren } from "../../ast_node";
import { SourceUnit } from "../meta/source_unit";
import { ContractDefinition } from "./contract_definition";
Expand All @@ -15,25 +15,18 @@ export class EnumDefinition extends ASTNodeWithChildren<EnumValue> {
*/
nameLocation?: string;

/**
* Canonical name (or qualified name), e.g. `DefiningContract.SomeEnum`
*/
canonicalName: string;

constructor(
id: number,
src: string,
type: string,
name: string,
canonicalName: string,
members: Iterable<EnumValue>,
nameLocation?: string,
raw?: any
) {
super(id, src, type, raw);

this.name = name;
this.canonicalName = canonicalName;

for (const member of members) {
this.appendChild(member);
Expand All @@ -42,6 +35,13 @@ export class EnumDefinition extends ASTNodeWithChildren<EnumValue> {
this.nameLocation = nameLocation;
}

/**
* Canonical name (or qualified name), e.g. `DefiningContract.SomeEnum`
*/
get canonicalName(): string {
return getUserDefinedTypeFQName(this);
}

/**
* Array of the enum values
*/
Expand Down
15 changes: 8 additions & 7 deletions src/ast/implementation/declaration/struct_definition.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getUserDefinedTypeFQName } from "../../..";
import { ASTNodeWithChildren } from "../../ast_node";
import { SourceUnit } from "../meta/source_unit";
import { ContractDefinition } from "./contract_definition";
Expand All @@ -9,11 +10,6 @@ export class StructDefinition extends ASTNodeWithChildren<VariableDeclaration> {
*/
name: string;

/**
* Canonical name (or qualified name), e.g. `DefiningContract.SomeStruct`
*/
canonicalName: string;

/**
* The source range for name string
*/
Expand All @@ -34,7 +30,6 @@ export class StructDefinition extends ASTNodeWithChildren<VariableDeclaration> {
src: string,
type: string,
name: string,
canonicalName: string,
scope: number,
visibility: string,
members: Iterable<VariableDeclaration>,
Expand All @@ -44,7 +39,6 @@ export class StructDefinition extends ASTNodeWithChildren<VariableDeclaration> {
super(id, src, type, raw);

this.name = name;
this.canonicalName = canonicalName;
this.scope = scope;
this.visibility = visibility;
this.nameLocation = nameLocation;
Expand All @@ -54,6 +48,13 @@ export class StructDefinition extends ASTNodeWithChildren<VariableDeclaration> {
}
}

/**
* Canonical name (or qualified name), e.g. `DefiningContract.SomeStruct`
*/
get canonicalName(): string {
return getUserDefinedTypeFQName(this);
}

/**
* Members of the struct
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getUserDefinedTypeFQName } from "../../..";
import { ASTNode } from "../../ast_node";
import { SourceUnit } from "../meta/source_unit";
import { ElementaryTypeName } from "../type/elementary_type_name";
Expand All @@ -9,12 +10,6 @@ export class UserDefinedValueTypeDefinition extends ASTNode {
*/
name: string;

/**
* Canonical name (or qualified name), e.g. `DefiningContract.SomeType`.
* Is `undefined` for Solidity 0.8.8. Available since Solidity 0.8.9.
*/
canonicalName?: string;

/**
* The source range for name string
*/
Expand All @@ -30,15 +25,13 @@ export class UserDefinedValueTypeDefinition extends ASTNode {
src: string,
type: string,
name: string,
canonicalName: string | undefined,
underlyingType: ElementaryTypeName,
nameLocation?: string,
raw?: any
) {
super(id, src, type, raw);

this.name = name;
this.canonicalName = canonicalName;
this.underlyingType = underlyingType;
this.nameLocation = nameLocation;

Expand All @@ -49,6 +42,13 @@ export class UserDefinedValueTypeDefinition extends ASTNode {
return this.pickNodes(this.underlyingType);
}

/**
* Canonical name (or qualified name), e.g. `DefiningContract.SomeType`
*/
get canonicalName(): string {
return getUserDefinedTypeFQName(this);
}

/**
* Reference to its scoped contract or source unit
*/
Expand Down
3 changes: 1 addition & 2 deletions src/ast/legacy/enum_definition_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export class LegacyEnumDefinitionProcessor extends LegacyNodeProcessor<EnumDefin
const members = reader.convertArray(raw.children, config) as EnumValue[];

const name: string = attributes.name;
const canonicalName: string = attributes.canonicalName;

return [id, src, type, name, canonicalName, members, undefined, raw];
return [id, src, type, name, members, undefined, raw];
}
}
3 changes: 1 addition & 2 deletions src/ast/legacy/struct_definition_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ export class LegacyStructDefinitionProcessor extends LegacyNodeProcessor<StructD
const members = reader.convertArray(raw.children, config) as VariableDeclaration[];

const name: string = attributes.name;
const canonicalName: string = attributes.canonicalName;
const scope: number = attributes.scope;
const visibility: string = attributes.visibility;

return [id, src, type, name, canonicalName, scope, visibility, members, undefined, raw];
return [id, src, type, name, scope, visibility, members, undefined, raw];
}
}
3 changes: 1 addition & 2 deletions src/ast/modern/enum_definition_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ export class ModernEnumDefinitionProcessor extends ModernNodeProcessor<EnumDefin
const [id, src, type] = super.process(reader, config, raw);

const name: string = raw.name;
const canonicalName: string = raw.canonicalName;
const nameLocation: string | undefined = raw.nameLocation;

const members = reader.convertArray(raw.members, config) as EnumValue[];

return [id, src, type, name, canonicalName, members, nameLocation, raw];
return [id, src, type, name, members, nameLocation, raw];
}
}
3 changes: 1 addition & 2 deletions src/ast/modern/struct_definition_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ export class ModernStructDefinitionProcessor extends ModernNodeProcessor<StructD
const [id, src, type] = super.process(reader, config, raw);

const name: string = raw.name;
const canonicalName: string = raw.canonicalName;
const scope: number = raw.scope;
const visibility: string = raw.visibility;
const nameLocation: string | undefined = raw.nameLocation;

const members = reader.convertArray(raw.members, config) as VariableDeclaration[];

return [id, src, type, name, canonicalName, scope, visibility, members, nameLocation, raw];
return [id, src, type, name, scope, visibility, members, nameLocation, raw];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ export class ModernUserDefinedValueTypeDefinitionProcessor extends ModernNodePro
const [id, src, type] = super.process(reader, config, raw);

const name: string = raw.name;
const canonicalName: string = raw.canonicalName;
const nameLocation: string | undefined = raw.nameLocation;
const underlyingType = reader.convert(raw.underlyingType, config) as ElementaryTypeName;

return [id, src, type, name, canonicalName, underlyingType, nameLocation, raw];
return [id, src, type, name, underlyingType, nameLocation, raw];
}
}
4 changes: 2 additions & 2 deletions test/samples/solidity/declarations/contract_050.nodes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -781,12 +781,12 @@ SourceUnit #146
src: "0:0:0"
type: "StructDefinition"
name: "St"
canonicalName: "C.St"
scope: 145
visibility: "public"
nameLocation: undefined
context: ASTContext #1000
parent: ContractDefinition #145
<getter> canonicalName: "C.St"
<getter> vMembers: Array(1) [ VariableDeclaration #108 ]
<getter> vScope: ContractDefinition #145
<getter> children: Array(1) [ VariableDeclaration #108 ]
Expand Down Expand Up @@ -848,10 +848,10 @@ SourceUnit #146
src: "0:0:0"
type: "EnumDefinition"
name: "En"
canonicalName: "C.En"
nameLocation: undefined
context: ASTContext #1000
parent: ContractDefinition #145
<getter> canonicalName: "C.En"
<getter> vMembers: Array(3) [ EnumValue #110, EnumValue #111, EnumValue #112 ]
<getter> vScope: ContractDefinition #145
<getter> children: Array(3) [ EnumValue #110, EnumValue #111, EnumValue #112 ]
Expand Down
14 changes: 7 additions & 7 deletions test/samples/solidity/latest_08.nodes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ SourceUnit #959
src: "0:0:0"
type: "EnumDefinition"
name: "EnumABC"
canonicalName: "EnumABC"
nameLocation: "91:7:0"
context: ASTContext #1000
parent: SourceUnit #959
<getter> canonicalName: "EnumABC"
<getter> vMembers: Array(3) [ EnumValue #494, EnumValue #495, EnumValue #496 ]
<getter> vScope: SourceUnit #959
<getter> children: Array(3) [ EnumValue #494, EnumValue #495, EnumValue #496 ]
Expand Down Expand Up @@ -2866,10 +2866,10 @@ SourceUnit #959
src: "0:0:0"
type: "EnumDefinition"
name: "EnumXYZ"
canonicalName: "Features082.EnumXYZ"
nameLocation: "1379:7:0"
context: ASTContext #1000
parent: ContractDefinition #713
<getter> canonicalName: "Features082.EnumXYZ"
<getter> vMembers: Array(3) [ EnumValue #633, EnumValue #634, EnumValue #635 ]
<getter> vScope: ContractDefinition #713
<getter> children: Array(3) [ EnumValue #633, EnumValue #634, EnumValue #635 ]
Expand Down Expand Up @@ -6080,12 +6080,12 @@ SourceUnit #959
src: "0:0:0"
type: "UserDefinedValueTypeDefinition"
name: "Price"
canonicalName: "Price"
underlyingType: ElementaryTypeName #800
nameLocation: "4739:5:0"
context: ASTContext #1000
parent: SourceUnit #959
<getter> children: Array(1) [ ElementaryTypeName #800 ]
<getter> canonicalName: "Price"
<getter> vScope: SourceUnit #959
<getter> firstChild: ElementaryTypeName #800
<getter> lastChild: ElementaryTypeName #800
Expand Down Expand Up @@ -6116,12 +6116,12 @@ SourceUnit #959
src: "0:0:0"
type: "UserDefinedValueTypeDefinition"
name: "Quantity"
canonicalName: "Quantity"
underlyingType: ElementaryTypeName #802
nameLocation: "4762:8:0"
context: ASTContext #1000
parent: SourceUnit #959
<getter> children: Array(1) [ ElementaryTypeName #802 ]
<getter> canonicalName: "Quantity"
<getter> vScope: SourceUnit #959
<getter> firstChild: ElementaryTypeName #802
<getter> lastChild: ElementaryTypeName #802
Expand Down Expand Up @@ -6190,12 +6190,12 @@ SourceUnit #959
src: "0:0:0"
type: "UserDefinedValueTypeDefinition"
name: "UFixed"
canonicalName: "LibWithUDVT_088.UFixed"
underlyingType: ElementaryTypeName #804
nameLocation: "4819:6:0"
context: ASTContext #1000
parent: ContractDefinition #891
<getter> children: Array(1) [ ElementaryTypeName #804 ]
<getter> canonicalName: "LibWithUDVT_088.UFixed"
<getter> vScope: ContractDefinition #891
<getter> firstChild: ElementaryTypeName #804
<getter> lastChild: ElementaryTypeName #804
Expand Down Expand Up @@ -8000,12 +8000,12 @@ SourceUnit #959
src: "0:0:0"
type: "UserDefinedValueTypeDefinition"
name: "EntityReference"
canonicalName: "InterfaceWithUDTV_088.EntityReference"
underlyingType: ElementaryTypeName #892
nameLocation: "5483:15:0"
context: ASTContext #1000
parent: ContractDefinition #902
<getter> children: Array(1) [ ElementaryTypeName #892 ]
<getter> canonicalName: "InterfaceWithUDTV_088.EntityReference"
<getter> vScope: ContractDefinition #902
<getter> firstChild: ElementaryTypeName #892
<getter> lastChild: ElementaryTypeName #892
Expand Down Expand Up @@ -9455,12 +9455,12 @@ SourceUnit #975
src: "0:0:0"
type: "StructDefinition"
name: "SomeStruct"
canonicalName: "SomeContract.SomeStruct"
scope: 973
visibility: "public"
nameLocation: "80:10:1"
context: ASTContext #1000
parent: ContractDefinition #973
<getter> canonicalName: "SomeContract.SomeStruct"
<getter> vMembers: Array(1) [ VariableDeclaration #963 ]
<getter> vScope: ContractDefinition #973
<getter> children: Array(1) [ VariableDeclaration #963 ]
Expand Down
Loading

0 comments on commit 6a54b43

Please sign in to comment.