Skip to content

Commit

Permalink
Merge pull request #63 from ConsenSys/adopt-internal-types
Browse files Browse the repository at this point in the history
Adopt internal types from Scribble
  • Loading branch information
cd1m0 authored Sep 20, 2021
2 parents 64c1d1e + 7733ef3 commit bf51021
Show file tree
Hide file tree
Showing 8 changed files with 637 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/types/ast/builtin_struct_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Range } from "../../misc";
import { VersionDependentType } from "../utils";
import { BuiltinType } from "./builtin_type";

/**
* A utility type for referencing builtin structs.
* It is not directly referenced in typeString parser grammar.
*/
export class BuiltinStructType extends BuiltinType {
readonly members: Map<string, VersionDependentType>;

constructor(name: string, members: Map<string, VersionDependentType>, src?: Range) {
super(name, src);

this.members = members;
}

pp(): string {
return `builtin_struct ${this.name}`;
}

getFields(): any[] {
return [this.name];
}
}
33 changes: 33 additions & 0 deletions src/types/ast/import_ref_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ImportDirective } from "../../ast";
import { assert, Range } from "../../misc";
import { TypeNode } from "./type";

/**
* A utility type for referencing aliased unit member access.
* It is not directly referenced in typeString parser grammar.
*/
export class ImportRefType extends TypeNode {
readonly importStmt: ImportDirective;

constructor(importStmt: ImportDirective, src?: Range) {
super(src);

assert(
importStmt.vSymbolAliases.length === 0 && importStmt.unitAlias !== "",
`ImportRefTypes only applicable to unit alias imports, not ${importStmt.print()}`
);

this.importStmt = importStmt;
}

getFields(): any[] {
return [this.importStmt];
}

pp(): string {
const path = this.importStmt.vSourceUnit.sourceEntryKey;
const alias = this.importStmt.unitAlias;

return `<import ${path} as ${alias}>`;
}
}
2 changes: 2 additions & 0 deletions src/types/ast/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export * from "./address";
export * from "./array";
export * from "./bool";
export * from "./builtin_struct_type";
export * from "./builtin_type";
export * from "./bytes";
export * from "./fixed_bytes";
export * from "./function_type";
export * from "./import_ref_type";
export * from "./int_literal";
export * from "./int_type";
export * from "./mapping_type";
Expand Down
246 changes: 246 additions & 0 deletions src/types/builtins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
import { DataLocation, FunctionStateMutability, FunctionVisibility } from "../ast/constants";
import {
AddressType,
BoolType,
BuiltinStructType,
BytesType,
FixedBytesType,
FunctionType,
IntType,
PointerType
} from "./ast";
import { VersionDependentType } from "./utils";

/**
* @see https://docs.soliditylang.org/en/latest/units-and-global-variables.html#special-variables-and-functions
* @see https://github.com/ethereum/solidity/releases/
*/
export const BuiltinSymbols = new Map<string, VersionDependentType>([
/**
* @todo Add support for encode(), decode(), encodePacked(), encodeWithSelector(), encodeWithSignature()
* @see https://github.com/ethereum/solidity/releases/tag/v0.4.22
*/
["abi", [new BuiltinStructType("abi", new Map()), ">=0.4.22"]],

/**
* @todo Add support for concat()
* @see https://github.com/ethereum/solidity/releases/tag/v0.8.4
*/
["bytes", [new BuiltinStructType("bytes", new Map()), ">=0.8.4"]],
[
"block",
[
new BuiltinStructType(
"block",
new Map<string, VersionDependentType>([
["coinbase", [new AddressType(true), ">=0.4.13"]],
["difficulty", [new IntType(256, false), ">=0.4.13"]],
["gaslimit", [new IntType(256, false), ">=0.4.13"]],
["number", [new IntType(256, false), ">=0.4.13"]],
["timestamp", [new IntType(256, false), ">=0.4.13"]],
[
"blockhash",
[
new FunctionType(
undefined,
[new IntType(256, false)],
[new FixedBytesType(32)],
FunctionVisibility.Default,
FunctionStateMutability.View
),
"<0.5.0"
]
],
["chainid", [new IntType(256, false), ">=0.8.0"]],
["basefee", [new IntType(256, false), ">=0.8.7"]]
])
),
">=0.4.13"
]
],
[
"msg",
[
new BuiltinStructType(
"msg",
new Map<string, VersionDependentType>([
["data", [new PointerType(new BytesType(), DataLocation.CallData), ">=0.4.13"]],
["sender", [new AddressType(true), ">=0.4.13"]],
["sig", [new FixedBytesType(4), ">=0.4.13"]],
["value", [new IntType(256, false), ">=0.4.13"]],
["gas", [new IntType(256, false), "<0.5.0"]]
])
),
">=0.4.13"
]
],
[
"tx",
[
new BuiltinStructType(
"tx",
new Map<string, VersionDependentType>([
["gasprice", [new IntType(256, false), ">=0.4.13"]],
["origin", [new AddressType(true), ">=0.4.13"]]
])
),
">=0.4.13"
]
],
[
"blockhash",
[
new FunctionType(
undefined,
[new IntType(256, false)],
[new FixedBytesType(32)],
FunctionVisibility.Default,
FunctionStateMutability.View
),
">=0.4.22"
]
],
[
"gasleft",
[
new FunctionType(
undefined,
[],
[new IntType(256, false)],
FunctionVisibility.Default,
FunctionStateMutability.View
),
">=0.4.21"
]
],
[
"now",
[
new FunctionType(
undefined,
[],
[new IntType(256, false)],
FunctionVisibility.Default,
FunctionStateMutability.View
),
"<0.7.0"
]
],
[
"addmod",
[
new FunctionType(
undefined,
[new IntType(256, false), new IntType(256, false), new IntType(256, false)],
[new IntType(256, false)],
FunctionVisibility.Default,
FunctionStateMutability.Pure
),
">=0.4.13"
]
],
[
"mulmod",
[
new FunctionType(
undefined,
[new IntType(256, false), new IntType(256, false), new IntType(256, false)],
[new IntType(256, false)],
FunctionVisibility.Default,
FunctionStateMutability.Pure
),
">=0.4.13"
]
],
/**
* @todo Add support for sha3() and suicide() before Solidity 0.5.0
* @see https://github.com/ethereum/solidity/releases/tag/v0.5.0
* @see https://docs.soliditylang.org/en/latest/050-breaking-changes.html
*/
[
"keccak256",
[
new FunctionType(
undefined,
[new PointerType(new BytesType(), DataLocation.Memory)],
[new FixedBytesType(32)],
FunctionVisibility.Default,
FunctionStateMutability.Pure
),
">=0.4.13"
]
],
[
"sha256",
[
new FunctionType(
undefined,
[new PointerType(new BytesType(), DataLocation.Memory)],
[new FixedBytesType(32)],
FunctionVisibility.Default,
FunctionStateMutability.Pure
),
">=0.4.13"
]
],
[
"ripemd160",
[
new FunctionType(
undefined,
[new PointerType(new BytesType(), DataLocation.Memory)],
[new FixedBytesType(20)],
FunctionVisibility.Default,
FunctionStateMutability.Pure
),
">=0.4.13"
]
],
[
"ecrecover",
[
new FunctionType(
undefined,
[
new FixedBytesType(32),
new IntType(8, false),
new FixedBytesType(32),
new FixedBytesType(32)
],
[new AddressType(false)],
FunctionVisibility.Default,
FunctionStateMutability.Pure
),
">=0.4.13"
]
]
/**
* @todo Add support for revert() and require(). Note their signature changes.
* @see https://github.com/ethereum/solidity/releases/tag/v0.4.22
*/
]);

export const BuiltinAddressMembers = new Map<string, VersionDependentType>([
["balance", [new IntType(256, false), ">=0.4.13"]],
[
"staticcall",
[
new FunctionType(
undefined,
[new PointerType(new BytesType(), DataLocation.Memory)],
[new BoolType(), new PointerType(new BytesType(), DataLocation.Memory)],
FunctionVisibility.Default,
FunctionStateMutability.View
),
">=0.4.13"
]
],
["code", [new PointerType(new BytesType(), DataLocation.Memory), ">=0.8.0"]]
]);

/**
* @todo Support type() members: min, max, interfaceId, name, runtimeCode, creationCode
* @see https://github.com/ethereum/solidity/releases/tag/v0.6.8
* @see https://github.com/ethereum/solidity/releases/tag/v0.6.7
* @see https://github.com/ethereum/solidity/releases/tag/v0.5.3
*/
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./ast";
export * from "./builtins";
export * from "./typeStrings";
export * from "./utils";
12 changes: 12 additions & 0 deletions src/types/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { satisfies } from "semver";
import {
ArrayTypeName,
ContractDefinition,
Expand Down Expand Up @@ -31,6 +32,17 @@ import {
UserDefinedType
} from "./ast";

export type VersionDependentType = [TypeNode, string];

export function getTypeForCompilerVersion(
typing: VersionDependentType,
compilerVersion: string
): TypeNode | undefined {
const [type, version] = typing;

return satisfies(compilerVersion, version) ? type : undefined;
}

/**
* Given a general type 'pattern' that doesn't contain any data locations, and a data location,
* produce a concrete instance of the general type for the target location.
Expand Down
Loading

0 comments on commit bf51021

Please sign in to comment.