運算子優先級
以下是按評估順序列出的運算子優先級,
| 優先級 | 描述 | 運算子 |
|---|---|---|
| 1 | 后置自增和自減 | ++, -- |
| 創建型別實體 | new <typename> | |
| 陣列元素 | <array>[<index>] | |
| 訪問成員 | <object>.<member> | |
| 函式呼叫 | <func>(<args...>) | |
| 小括號 | (<statement>) | |
| 2 | 前置自增和自減 | ++, -- |
| 一元運算的加和減 | +, - | |
| 一元運算子 | delete | |
| 邏輯非 | ! | |
| 按位非 | ~ | |
| 3 | 乘方 | ** |
| 4 | 乘、除和模運算 | *, /, % |
| 5 | 算術加和減 | +, - |
| 6 | 移位運算子 | <<, >> |
| 7 | 按位與 | & |
| 8 | 按位異或 | ^ |
| 9 | 按位或 | | |
| 10 | 非等運算子 | <, >, <=, >= |
| 11 | 等于運算子 | ==, != |
| 12 | 邏輯與 | && |
| 13 | 邏輯或 | || |
| 14 | 三元運算子 | <conditional> ? <if-true> : <if-false> |
| 15 | 賦值運算子 | =, |=, ^=, &=, <<=, >>=, +=, -=, *=, /=, %= |
| 16 | 逗號 | , |
全域變數
abi.encode(...) returns (bytes): ABI - 對給定引數進行編碼abi.encodePacked(...) returns (bytes):對給定引數執行 緊打包編碼abi.encodeWithSelector(bytes4 selector, ...) returns (bytes): ABI - 對給定引數進行編碼,并以給定的函式選擇器作為起始的 4 位元組資料一起回傳abi.encodeWithSignature(string signature, ...) returns (bytes):等價于abi.encodeWithSelector(bytes4(keccak256(signature), ...)block.blockhash(uint blockNumber) returns (bytes32):指定區塊的區塊哈希——僅可用于最新的 256 個區塊且不包括當前區塊;而 blocks 從 0.4.22 版本開始已經不推薦使用,由blockhash(uint blockNumber)代替block.coinbase(address):挖出當前區塊的礦工的地址block.difficulty(uint):當前區塊的難度值block.gaslimit(uint):當前區塊的 gas 上限block.number(uint):當前區塊的區塊號block.timestamp(uint):當前區塊的時間戳gasleft() returns (uint256):剩余的 gasmsg.data(bytes):完整的 calldatamsg.gas(uint):剩余的 gas - 自 0.4.21 版本開始已經不推薦使用,由gesleft()代替msg.sender(address):訊息發送方(當前呼叫)msg.value(uint):隨訊息發送的 wei 的數量now(uint):當前區塊的時間戳(等價于block.timestamp)tx.gasprice(uint):交易的 gas pricetx.origin(address):交易發送方(完整呼叫鏈上的原始發送方)assert(bool condition):如果條件值為false則中止執行并回退所有狀態變更(用做內部錯誤)require(bool condition):如果條件值為false則中止執行并回退所有狀態變更(用做例外輸入或外部組件錯誤)require(bool condition, string message):如果條件值為false則中止執行并回退所有狀態變更(用做例外輸入或外部組件錯誤),可以同時提供錯誤訊息revert():中止執行并回復所有狀態變更revert(string message):中止執行并回復所有狀態變更,可以同時提供錯誤訊息blockhash(uint blockNumber) returns (bytes32):指定區塊的區塊哈希——僅可用于最新的 256 個區塊keccak256(...) returns (bytes32):計算 緊打包編碼 的 Ethereum-SHA-3(Keccak-256)哈希sha3(...) returns (bytes32):等價于keccak256sha256(...) returns (bytes32):計算 緊打包編碼 的 SHA-256 哈希ripemd160(...) returns (bytes20):計算 緊打包編碼 的 RIPEMD-160 哈希ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address):基于橢圓曲線簽名找回與指定公鑰關聯的地址,發生錯誤的時候回傳 0addmod(uint x, uint y, uint k) returns (uint):計算(x + y) % k的值,其中加法的結果即使超過2**256也不會被截取,從 0.5.0 版本開始會加入對k != 0的 assert(即會在此函式開頭執行assert(k != 0);作為引數檢查,譯者注),mulmod(uint x, uint y, uint k) returns (uint):計算(x * y) % k的值,其中乘法的結果即使超過2**256也不會被截取,從 0.5.0 版本開始會加入對k != 0的 assert(即會在此函式開頭執行assert(k != 0);作為引數檢查,譯者注),this(型別為當前合約的變數):當前合約實體,可以準確地轉換為addresssuper:當前合約的上一級繼承關系的合約selfdestruct(address recipient):銷毀當前合約,把余額發送到給定地址suicide(address recipient):與selfdestruct等價,但已不推薦使用<address>.balance(uint256): 地址型別 的余額,以 Wei 為單位<address>.send(uint256 amount) returns (bool):向 地址型別 發送給定數量的 Wei,失敗時回傳false<address>.transfer(uint256 amount):向 地址型別 發送給定數量的 Wei,失敗時會把錯誤拋出(throw)
注解
不要用 block.timestamp、now 或者 blockhash 作為隨機種子,除非你明確知道你在做什么,
時間戳和區塊哈希都可以在一定程度上被礦工所影響,如果你用哈希值作為隨機種子,那么例如挖礦團體中的壞人就可以使用給定的哈希來執行一個賭場功能,如果他們沒贏錢,他們可以簡單地換一個哈希再試,
當前區塊的時間戳必須比前一個區塊的時間戳大,但唯一可以確定的就是它會是權威鏈(主鏈或者主分支)上兩個連續區塊時間戳之間的一個數值,
注解
出于擴展性的原因,你無法取得所有區塊的哈希,只有最新的 256 個區塊的哈希可以拿到,其他的都將為 0,
函式可見性說明符
function myFunction() <visibility specifier> returns (bool) {
return true;
}
public:內部、外部均可見(參考為存盤/狀態變數創建 getter 函式)private:僅在當前合約內可見external:僅在外部可見(僅可修飾函式)——就是說,僅可用于訊息呼叫(即使在合約內呼叫,也只能通過this.func的方式)internal:僅在內部可見(也就是在當前 Solidity 源代碼檔案內均可見,不僅限于當前合約內,譯者注)
修改器
pure修飾函式時:不允許修改或訪問狀態——但目前并不是強制的,view修飾函式時:不允許修改狀態——但目前不是強制的,payable修飾函式時:允許從呼叫中接收 以太幣Ether ,constant修飾狀態變數時:不允許賦值(除初始化以外),不會占據 存盤插槽storage slot ,constant修飾函式時:與view等價,anonymous修飾事件時:不把事件簽名作為 topic 存盤,indexed修飾事件時:將引數作為 topic 存盤,
保留字
以下是 Solidity 的保留字,未來可能會變為語法的一部分:
abstract, after, alias, apply, auto, case, catch, copyof, default, define, final, immutable, implements, in, inline, let, macro, match, mutable, null, of, override, partial, promise, reference, relocatable, sealed, sizeof, static, supports, switch, try, type, typedef, typeof,unchecked.
語法表
SourceUnit = (PragmaDirective | ImportDirective | ContractDefinition)*
// Pragma actually parses anything up to the trailing ';' to be fully forward-compatible.
PragmaDirective = 'pragma' Identifier ([^;]+) ';'
ImportDirective = 'import' StringLiteral ('as' Identifier)? ';'
| 'import' ('*' | Identifier) ('as' Identifier)? 'from' StringLiteral ';'
| 'import' '{' Identifier ('as' Identifier)? ( ',' Identifier ('as' Identifier)? )* '}' 'from' StringLiteral ';'
ContractDefinition = ( 'contract' | 'library' | 'interface' ) Identifier
( 'is' InheritanceSpecifier (',' InheritanceSpecifier )* )?
'{' ContractPart* '}'
ContractPart = StateVariableDeclaration | UsingForDeclaration
| StructDefinition | ModifierDefinition | FunctionDefinition | EventDefinition | EnumDefinition
InheritanceSpecifier = UserDefinedTypeName ( '(' Expression ( ',' Expression )* ')' )?
StateVariableDeclaration = TypeName ( 'public' | 'internal' | 'private' | 'constant' )* Identifier ('=' Expression)? ';'
UsingForDeclaration = 'using' Identifier 'for' ('*' | TypeName) ';'
StructDefinition = 'struct' Identifier '{'
( VariableDeclaration ';' (VariableDeclaration ';')* ) '}'
ModifierDefinition = 'modifier' Identifier ParameterList? Block
ModifierInvocation = Identifier ( '(' ExpressionList? ')' )?
FunctionDefinition = 'function' Identifier? ParameterList
( ModifierInvocation | StateMutability | 'external' | 'public' | 'internal' | 'private' )*
( 'returns' ParameterList )? ( ';' | Block )
EventDefinition = 'event' Identifier EventParameterList 'anonymous'? ';'
EnumValue = Identifier
EnumDefinition = 'enum' Identifier '{' EnumValue? (',' EnumValue)* '}'
ParameterList = '(' ( Parameter (',' Parameter)* )? ')'
Parameter = TypeName StorageLocation? Identifier?
EventParameterList = '(' ( EventParameter (',' EventParameter )* )? ')'
EventParameter = TypeName 'indexed'? Identifier?
FunctionTypeParameterList = '(' ( FunctionTypeParameter (',' FunctionTypeParameter )* )? ')'
FunctionTypeParameter = TypeName StorageLocation?
// semantic restriction: mappings and structs (recursively) containing mappings
// are not allowed in argument lists
VariableDeclaration = TypeName StorageLocation? Identifier
TypeName = ElementaryTypeName
| UserDefinedTypeName
| Mapping
| ArrayTypeName
| FunctionTypeName
UserDefinedTypeName = Identifier ( '.' Identifier )*
Mapping = 'mapping' '(' ElementaryTypeName '=>' TypeName ')'
ArrayTypeName = TypeName '[' Expression? ']'
FunctionTypeName = 'function' FunctionTypeParameterList ( 'internal' | 'external' | StateMutability )*
( 'returns' FunctionTypeParameterList )?
StorageLocation = 'memory' | 'storage' | 'calldata'
StateMutability = 'pure' | 'constant' | 'view' | 'payable'
Block = '{' Statement* '}'
Statement = IfStatement | WhileStatement | ForStatement | Block | InlineAssemblyStatement |
( DoWhileStatement | PlaceholderStatement | Continue | Break | Return |
Throw | EmitStatement | SimpleStatement ) ';'
ExpressionStatement = Expression
IfStatement = 'if' '(' Expression ')' Statement ( 'else' Statement )?
WhileStatement = 'while' '(' Expression ')' Statement
PlaceholderStatement = '_'
SimpleStatement = VariableDefinition | ExpressionStatement
ForStatement = 'for' '(' (SimpleStatement)? ';' (Expression)? ';' (ExpressionStatement)? ')' Statement
InlineAssemblyStatement = 'assembly' StringLiteral? InlineAssemblyBlock
DoWhileStatement = 'do' Statement 'while' '(' Expression ')'
Continue = 'continue'
Break = 'break'
Return = 'return' Expression?
Throw = 'throw'
EmitStatement = 'emit' FunctionCall
VariableDefinition = ('var' IdentifierList | VariableDeclaration | '(' VariableDeclaration? (',' VariableDeclaration? )* ')' ) ( '=' Expression )?
IdentifierList = '(' ( Identifier? ',' )* Identifier? ')'
// Precedence by order (see github.com/ethereum/solidity/pull/732)
Expression
= Expression ('++' | '--')
| NewExpression
| IndexAccess
| MemberAccess
| FunctionCall
| '(' Expression ')'
| ('!' | '~' | 'delete' | '++' | '--' | '+' | '-') Expression
| Expression '**' Expression
| Expression ('*' | '/' | '%') Expression
| Expression ('+' | '-') Expression
| Expression ('<<' | '>>') Expression
| Expression '&' Expression
| Expression '^' Expression
| Expression '|' Expression
| Expression ('<' | '>' | '<=' | '>=') Expression
| Expression ('==' | '!=') Expression
| Expression '&&' Expression
| Expression '||' Expression
| Expression '?' Expression ':' Expression
| Expression ('=' | '|=' | '^=' | '&=' | '<<=' | '>>=' | '+=' | '-=' | '*=' | '/=' | '%=') Expression
| PrimaryExpression
PrimaryExpression = BooleanLiteral
| NumberLiteral
| HexLiteral
| StringLiteral
| TupleExpression
| Identifier
| ElementaryTypeNameExpression
ExpressionList = Expression ( ',' Expression )*
NameValueList = Identifier ':' Expression ( ',' Identifier ':' Expression )*
FunctionCall = Expression '(' FunctionCallArguments ')'
FunctionCallArguments = '{' NameValueList? '}'
| ExpressionList?
NewExpression = 'new' TypeName
MemberAccess = Expression '.' Identifier
IndexAccess = Expression '[' Expression? ']'
BooleanLiteral = 'true' | 'false'
NumberLiteral = ( HexNumber | DecimalNumber ) (' ' NumberUnit)?
NumberUnit = 'wei' | 'szabo' | 'finney' | 'ether'
| 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'years'
HexLiteral = 'hex' ('"' ([0-9a-fA-F]{2})* '"' | '\'' ([0-9a-fA-F]{2})* '\'')
StringLiteral = '"' ([^"\r\n\\] | '\\' .)* '"'
Identifier = [a-zA-Z_$] [a-zA-Z_$0-9]*
HexNumber = '0x' [0-9a-fA-F]+
DecimalNumber = [0-9]+ ( '.' [0-9]* )? ( [eE] [0-9]+ )?
TupleExpression = '(' ( Expression? ( ',' Expression? )* )? ')'
| '[' ( Expression ( ',' Expression )* )? ']'
ElementaryTypeNameExpression = ElementaryTypeName
ElementaryTypeName = 'address' | 'bool' | 'string' | 'var'
| Int | Uint | Byte | Fixed | Ufixed
Int = 'int' | 'int8' | 'int16' | 'int24' | 'int32' | 'int40' | 'int48' | 'int56' | 'int64' | 'int72' | 'int80' | 'int88' | 'int96' | 'int104' | 'int112' | 'int120' | 'int128' | 'int136' | 'int144' | 'int152' | 'int160' | 'int168' | 'int176' | 'int184' | 'int192' | 'int200' | 'int208' | 'int216' | 'int224' | 'int232' | 'int240' | 'int248' | 'int256'
Uint = 'uint' | 'uint8' | 'uint16' | 'uint24' | 'uint32' | 'uint40' | 'uint48' | 'uint56' | 'uint64' | 'uint72' | 'uint80' | 'uint88' | 'uint96' | 'uint104' | 'uint112' | 'uint120' | 'uint128' | 'uint136' | 'uint144' | 'uint152' | 'uint160' | 'uint168' | 'uint176' | 'uint184' | 'uint192' | 'uint200' | 'uint208' | 'uint216' | 'uint224' | 'uint232' | 'uint240' | 'uint248' | 'uint256'
Byte = 'byte' | 'bytes' | 'bytes1' | 'bytes2' | 'bytes3' | 'bytes4' | 'bytes5' | 'bytes6' | 'bytes7' | 'bytes8' | 'bytes9' | 'bytes10' | 'bytes11' | 'bytes12' | 'bytes13' | 'bytes14' | 'bytes15' | 'bytes16' | 'bytes17' | 'bytes18' | 'bytes19' | 'bytes20' | 'bytes21' | 'bytes22' | 'bytes23' | 'bytes24' | 'bytes25' | 'bytes26' | 'bytes27' | 'bytes28' | 'bytes29' | 'bytes30' | 'bytes31' | 'bytes32'
Fixed = 'fixed' | ( 'fixed' [0-9]+ 'x' [0-9]+ )
Ufixed = 'ufixed' | ( 'ufixed' [0-9]+ 'x' [0-9]+ )
InlineAssemblyBlock = '{' AssemblyItem* '}'
AssemblyItem = Identifier | FunctionalAssemblyExpression | InlineAssemblyBlock | AssemblyLocalBinding | AssemblyAssignment | AssemblyLabel | NumberLiteral | StringLiteral | HexLiteral
AssemblyLocalBinding = 'let' Identifier ':=' FunctionalAssemblyExpression
AssemblyAssignment = ( Identifier ':=' FunctionalAssemblyExpression ) | ( '=:' Identifier )
AssemblyLabel = Identifier ':'
FunctionalAssemblyExpression = Identifier '(' AssemblyItem? ( ',' AssemblyItem )* ')'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/132271.html
標籤:其他
