def add_globals_and_events(_contracts, _defs, _events, _getters, _globals, item):
if item.value is not None:
raise StructureException('May not assign value whilst defining type', item)
elif isinstance(item.annotation, ast.Call) and item.annotation.func.id == "__log__":
if _globals or len(_defs):
raise StructureException("Events must all come before global declarations and function definitions", item)
_events.append(item)
elif not isinstance(item.target, ast.Name):
raise StructureException("Can only assign type to variable in top-level statement", item)
# Check if variable name is reserved or invalid
elif not is_varname_valid(item.target.id):
raise VariableDeclarationException("Variable name invalid or reserved: ", item.target)
# Check if global already exists, if so error
elif item.target.id in _globals:
raise VariableDeclarationException("Cannot declare a persistent variable twice!", item.target)
elif len(_defs):
raise StructureException("Global variables must all come before function definitions", item)
# If the type declaration is of the form public(<type here>), then proceed with
# the underlying type but also add getters
elif isinstance(item.annotation, ast.Call) and item.annotation.func.id == "address":
if len(item.annotation.args) != 1:
raise StructureException("Address expects one arg (the type)")
if item.annotation.args[0].id not in premade_contracts:
raise VariableDeclarationException("Unsupported premade contract declaration", item.annotation.args[0])
premade_contract = premade_contracts[item.annotation.args[0].id]
_contracts[item.target.id] = add_contract(premade_contract.body)
_globals[item.target.id] = VariableRecord(item.target.id, len(_globals), BaseType('address'), True)
elif isinstance(item, ast.AnnAssign) and isinstance(item.annotation, ast.Name) and item.annotation.id in _contracts:
_globals[item.target.id] = VariableRecord(item.target.id, len(_globals), BaseType('address', item.annotation.id), True)
elif isinstance(item.annotation, ast.Call) and item.annotation.func.id == "public":
if len(item.annotation.args) != 1:
raise StructureException("Public expects one arg (the type)")
if isinstance(item.annotation.args[0], ast.Name) and item.annotation.args[0].id in _contracts:
typ = BaseType('address', item.annotation.args[0].id)
else:
typ = parse_type(item.annotation.args[0], 'storage')
_globals[item.target.id] = VariableRecord(item.target.id, len(_globals), typ, True)
# Adding getters here
for getter in mk_getter(item.target.id, typ):
_getters.append(parse_line('\n' * (item.lineno - 1) + getter))
_getters[-1].pos = getpos(item)
else:
_globals[item.target.id] = VariableRecord(item.target.id, len(_globals), parse_type(item.annotation, 'storage'), True)
return _contracts, _events, _globals, _getters
# Parse top-level functions and variables
评论列表
文章目录