diff --git a/libsolidity/AST.cpp b/libsolidity/AST.cpp index e5967caa5..7620eeece 100644 --- a/libsolidity/AST.cpp +++ b/libsolidity/AST.cpp @@ -68,19 +68,21 @@ void ContractDefinition::checkTypeRequirements() set> hashes; for (auto const& hashAndFunction: getInterfaceFunctionList()) { - FixedHash<4> const& hash = hashAndFunction.first; + FixedHash<4> const& hash = std::get<0>(hashAndFunction); if (hashes.count(hash)) - BOOST_THROW_EXCEPTION(createTypeError("Function signature hash collision for " + - hashAndFunction.second->getCanonicalSignature())); + BOOST_THROW_EXCEPTION(createTypeError( + "Function signature hash collision for " + + std::get<1>(hashAndFunction)>->getCanonicalSignature(std::get<2>(hashAndFunction)->getName()))); hashes.insert(hash); } } -map, FunctionDefinition const*> ContractDefinition::getInterfaceFunctions() const +map, pair> ContractDefinition::getInterfaceFunctions() const { - vector, FunctionDefinition const*>> exportedFunctionList = getInterfaceFunctionList(); - map, FunctionDefinition const*> exportedFunctions(exportedFunctionList.begin(), - exportedFunctionList.end()); + vector, FunctionType const*, Declaration const*>>> exportedFunctionList = getInterfaceFunctionList(); + map, pair> exportedFunctions(exportedFunctionList.begin(), + exportedFunctionList.end()); + solAssert(exportedFunctionList.size() == exportedFunctions.size(), "Hash collision at Function Definition Hash calculation"); @@ -134,20 +136,31 @@ void ContractDefinition::checkIllegalOverrides() const } } -vector, FunctionDefinition const*>> const& ContractDefinition::getInterfaceFunctionList() const +vector, FunctionType const*, Declaration const*>> const& ContractDefinition::getInterfaceFunctionList() const { if (!m_interfaceFunctionList) { set functionsSeen; - m_interfaceFunctionList.reset(new vector, FunctionDefinition const*>>()); + m_interfaceFunctionList.reset(new vector, FunctionType const*, Declaration const*>>()); for (ContractDefinition const* contract: getLinearizedBaseContracts()) + { for (ASTPointer const& f: contract->getDefinedFunctions()) if (f->isPublic() && !f->isConstructor() && functionsSeen.count(f->getName()) == 0) { functionsSeen.insert(f->getName()); FixedHash<4> hash(dev::sha3(f->getCanonicalSignature())); - m_interfaceFunctionList->push_back(make_pair(hash, f.get())); + m_interfaceFunctionList->push_back(make_tuple(hash, FunctionType(*f), f.get())); } + + for (ASTPointer const& v: contract->getStateVariables()) + if (v->isPublic()) + { + FunctionType ftype(*v); + functionsSeen.insert(v->getName()); + FixedHash<4> hash(dev::sha3(ftype.getCanonicalSignature(v->getName())); + m_interfaceFunctionList->push_back(make_tuple(hash, ftype, v.get())); + } + } } return *m_interfaceFunctionList; } @@ -219,7 +232,7 @@ void FunctionDefinition::checkTypeRequirements() string FunctionDefinition::getCanonicalSignature() const { - return getName() + FunctionType(*this).getCanonicalSignature(); + return FunctionType(*this).getCanonicalSignature(getName()); } Declaration::LValueType VariableDeclaration::getLValueType() const @@ -504,5 +517,18 @@ void Literal::checkTypeRequirements() BOOST_THROW_EXCEPTION(createTypeError("Invalid literal value.")); } + +ASTPointer FunctionDescription::getDocumentation() +{ + auto function = dynamic_cast(m_description.second); + if (function) + return function->getDocumentation(); +} + +string FunctionDescription::getSignature() +{ + return m_description.first->getCanonicalSignature(m_description.second->getName()); +} + } } diff --git a/libsolidity/AST.h b/libsolidity/AST.h index e047f9761..695e504ac 100755 --- a/libsolidity/AST.h +++ b/libsolidity/AST.h @@ -156,6 +156,23 @@ private: Declaration const* m_scope; }; + +/** + * Generic function description able to describe both normal functions and + * functions that should be made as accessors to state variables + */ +struct FunctionDescription +{ + FunctionDescription(FunctionType const *_type, Declaration const* _decl): + m_description(_type, _decl){} + + ASTPointer getDocumentation(); + std::string getSignature(); + + std::pair m_description; +}; + + /** * Definition of a contract. This is the only AST nodes where child nodes are not visited in * document order. It first visits all struct declarations, then all variable declarations and @@ -202,7 +219,7 @@ public: /// @returns a map of canonical function signatures to FunctionDefinitions /// as intended for use by the ABI. - std::map, FunctionDefinition const*> getInterfaceFunctions() const; + std::map, std::pair> getInterfaceFunctions() const; /// List of all (direct and indirect) base contracts in order from derived to base, including /// the contract itself. Available after name resolution @@ -215,7 +232,7 @@ public: private: void checkIllegalOverrides() const; - std::vector, FunctionDefinition const*>> const& getInterfaceFunctionList() const; + std::vector, FunctionType const*, Declaration const*>> const& getInterfaceFunctionList() const; std::vector> m_baseContracts; std::vector> m_definedStructs; @@ -225,7 +242,7 @@ private: ASTPointer m_documentation; std::vector m_linearizedBaseContracts; - mutable std::unique_ptr, FunctionDefinition const*>>> m_interfaceFunctionList; + mutable std::unique_ptr, FunctionType const*, Declaration const*>>> m_interfaceFunctionList; }; class InheritanceSpecifier: public ASTNode @@ -372,8 +389,8 @@ class VariableDeclaration: public Declaration { public: VariableDeclaration(Location const& _location, ASTPointer const& _type, - ASTPointer const& _name): - Declaration(_location, _name), m_typeName(_type) {} + ASTPointer const& _name, bool _isPublic): + Declaration(_location, _name), m_typeName(_type), m_isPublic(_isPublic) {} virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTConstVisitor& _visitor) const override; @@ -385,10 +402,13 @@ public: void setType(std::shared_ptr const& _type) { m_type = _type; } virtual LValueType getLValueType() const override; + bool isLocalVariable() const { return !!dynamic_cast(getScope()); } + bool isPublic() const { return m_isPublic; } -private: - ASTPointer m_typeName; ///< can be empty ("var") +private: + ASTPointer m_typeName; ///< can be empty ("var") + bool m_isPublic; ///< Whether there is an accessor for it or not std::shared_ptr m_type; ///< derived type, initially empty }; @@ -1076,5 +1096,6 @@ private: /// @} + } } diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index 5190f93f3..13f8282e6 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -201,7 +201,7 @@ set Compiler::getFunctionsCalled(set void Compiler::appendFunctionSelector(ContractDefinition const& _contract) { - map, FunctionDefinition const*> interfaceFunctions = _contract.getInterfaceFunctions(); + map, FunctionType const*, Declaration const*> interfaceFunctions = _contract.getInterfaceFunctions(); map, const eth::AssemblyItem> callDataUnpackerEntryPoints; // retrieve the function signature hash from the calldata @@ -209,7 +209,6 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract) CompilerUtils(m_context).loadFromMemory(0, 4, false, true); // stack now is: 1 0 - // for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) for (auto const& it: interfaceFunctions) { callDataUnpackerEntryPoints.insert(std::make_pair(it.first, m_context.newTag())); @@ -220,29 +219,28 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract) for (auto const& it: interfaceFunctions) { - FunctionDefinition const& function = *it.second; + FunctionType const* functionType = *it.second.first; m_context << callDataUnpackerEntryPoints.at(it.first); eth::AssemblyItem returnTag = m_context.pushNewTag(); - appendCalldataUnpacker(function); - m_context.appendJumpTo(m_context.getFunctionEntryLabel(function)); + appendCalldataUnpacker(functionType->getParameterTypes()); + m_context.appendJumpTo(m_context.getFunctionEntryLabel(it.second.second)); m_context << returnTag; - appendReturnValuePacker(function); + appendReturnValuePacker(functionType->getReturnParameterTypes()); } } -unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, bool _fromMemory) +unsigned Compiler::appendCalldataUnpacker(TypePointers const& _typeParameters, bool _fromMemory) { // We do not check the calldata size, everything is zero-padded. unsigned dataOffset = CompilerUtils::dataStartOffset; // the 4 bytes of the function hash signature //@todo this can be done more efficiently, saving some CALLDATALOAD calls - for (ASTPointer const& var: _function.getParameters()) + for (TypePointer const& type: _typeParameters) { - unsigned const c_numBytes = var->getType()->getCalldataEncodedSize(); + unsigned const c_numBytes = type->getCalldataEncodedSize(); if (c_numBytes > 32) BOOST_THROW_EXCEPTION(CompilerError() - << errinfo_sourceLocation(var->getLocation()) - << errinfo_comment("Type " + var->getType()->toString() + " not yet supported.")); - bool const c_leftAligned = var->getType()->getCategory() == Type::Category::STRING; + << errinfo_comment("Type " + type->toString() + " not yet supported.")); + bool const c_leftAligned = type->getCategory() == Type::Category::STRING; bool const c_padToWords = true; dataOffset += CompilerUtils(m_context).loadFromMemory(dataOffset, c_numBytes, c_leftAligned, !_fromMemory, c_padToWords); @@ -250,26 +248,26 @@ unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, b return dataOffset; } -void Compiler::appendReturnValuePacker(FunctionDefinition const& _function) +void Compiler::appendReturnValuePacker(TypePointers const& _typeParameters) { //@todo this can be also done more efficiently unsigned dataOffset = 0; - vector> const& parameters = _function.getReturnParameters(); - unsigned stackDepth = CompilerUtils(m_context).getSizeOnStack(parameters); - for (unsigned i = 0; i < parameters.size(); ++i) + unsigned stackDepth = 0; + for (TypePointer const& type: _typeParameters) + stackDepth += type->getSizeOnStack(); + + for (TypePointer const& type: _typeParameters) { - Type const& paramType = *parameters[i]->getType(); - unsigned numBytes = paramType.getCalldataEncodedSize(); + unsigned numBytes = type->getCalldataEncodedSize(); if (numBytes > 32) BOOST_THROW_EXCEPTION(CompilerError() - << errinfo_sourceLocation(parameters[i]->getLocation()) - << errinfo_comment("Type " + paramType.toString() + " not yet supported.")); - CompilerUtils(m_context).copyToStackTop(stackDepth, paramType); - ExpressionCompiler::appendTypeConversion(m_context, paramType, paramType, true); - bool const c_leftAligned = paramType.getCategory() == Type::Category::STRING; + << errinfo_comment("Type " + type->toString() + " not yet supported.")); + CompilerUtils(m_context).copyToStackTop(stackDepth, *type); + ExpressionCompiler::appendTypeConversion(m_context, *type, *type, true); + bool const c_leftAligned = type->getCategory() == Type::Category::STRING; bool const c_padToWords = true; dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, c_leftAligned, c_padToWords); - stackDepth -= paramType.getSizeOnStack(); + stackDepth -= type->getSizeOnStack(); } // note that the stack is not cleaned up here m_context << u256(dataOffset) << u256(0) << eth::Instruction::RETURN; diff --git a/libsolidity/Compiler.h b/libsolidity/Compiler.h index b65476460..f40339b03 100644 --- a/libsolidity/Compiler.h +++ b/libsolidity/Compiler.h @@ -56,10 +56,10 @@ private: std::function const& _resolveFunctionOverride, std::function const& _resolveModifierOverride); void appendFunctionSelector(ContractDefinition const& _contract); - /// Creates code that unpacks the arguments for the given function, from memory if - /// @a _fromMemory is true, otherwise from call data. @returns the size of the data in bytes. - unsigned appendCalldataUnpacker(FunctionDefinition const& _function, bool _fromMemory = false); - void appendReturnValuePacker(FunctionDefinition const& _function); + /// Creates code that unpacks the arguments for the given function represented by a vector of TypePointers. + /// From memory if @a _fromMemory is true, otherwise from call data. @returns the size of the data in bytes. + unsigned appendCalldataUnpacker(TypePointers const& _typeParameters, bool _fromMemory = false); + void appendReturnValuePacker(TypePointers const& _typeParameters); void registerStateVariables(ContractDefinition const& _contract); diff --git a/libsolidity/CompilerContext.cpp b/libsolidity/CompilerContext.cpp index ad1877ba6..4edced940 100644 --- a/libsolidity/CompilerContext.cpp +++ b/libsolidity/CompilerContext.cpp @@ -83,9 +83,9 @@ bool CompilerContext::isLocalVariable(Declaration const* _declaration) const return m_localVariables.count(_declaration); } -eth::AssemblyItem CompilerContext::getFunctionEntryLabel(FunctionDefinition const& _function) const +eth::AssemblyItem CompilerContext::getFunctionEntryLabel(Declaration const& _declaration) const { - auto res = m_functionEntryLabels.find(&_function); + auto res = m_functionEntryLabels.find(&_declaration); solAssert(res != m_functionEntryLabels.end(), "Function entry label not found."); return res->second.tag(); } diff --git a/libsolidity/CompilerContext.h b/libsolidity/CompilerContext.h index d82dfe51e..aa438cf02 100644 --- a/libsolidity/CompilerContext.h +++ b/libsolidity/CompilerContext.h @@ -58,7 +58,7 @@ public: bool isLocalVariable(Declaration const* _declaration) const; bool isStateVariable(Declaration const* _declaration) const { return m_stateVariables.count(_declaration) != 0; } - eth::AssemblyItem getFunctionEntryLabel(FunctionDefinition const& _function) const; + eth::AssemblyItem getFunctionEntryLabel(Declaration const& _declaration) const; /// @returns the entry label of the given function and takes overrides into account. eth::AssemblyItem getVirtualFunctionEntryLabel(FunctionDefinition const& _function) const; ModifierDefinition const& getFunctionModifier(std::string const& _name) const; @@ -115,9 +115,12 @@ private: u256 m_stateVariablesSize = 0; /// Storage offsets of state variables std::map m_stateVariables; - /// Positions of local variables on the stack. + /// Offsets of local variables on the stack (relative to stack base). std::map m_localVariables; - /// Labels pointing to the entry points of funcitons. + /// Sum of stack sizes of local variables + unsigned m_localVariablesSize; + /// Labels pointing to the entry points of functions. + std::map m_functionEntryLabels; /// Labels pointing to the entry points of function overrides. std::map m_virtualFunctionEntryLabels; diff --git a/libsolidity/Parser.cpp b/libsolidity/Parser.cpp index c9ab551e4..1c61aab12 100644 --- a/libsolidity/Parser.cpp +++ b/libsolidity/Parser.cpp @@ -109,30 +109,6 @@ ASTPointer Parser::parseImportDirective() return nodeFactory.createNode(url); } -void Parser::addStateVariableAccessor(ASTPointer const& _varDecl, - vector> & _functions) -{ - ASTNodeFactory nodeFactory(*this); - nodeFactory.setLocationEmpty(); - ASTPointer emptyDoc; - - vector> parameters; - auto expression = nodeFactory.createNode(make_shared(_varDecl->getName())); - vector> block_statements = {nodeFactory.createNode(expression)}; - - _functions.push_back(nodeFactory.createNode( - make_shared(_varDecl->getName()), - true, // isPublic - false, // not a Constructor - emptyDoc, // no documentation - nodeFactory.createNode(vector>()), - true, // is constant - nodeFactory.createNode(vector>()), - nodeFactory.createNode(block_statements) - ) - ); -} - ASTPointer Parser::parseContractDefinition() { ASTNodeFactory nodeFactory(*this); @@ -174,9 +150,7 @@ ASTPointer Parser::parseContractDefinition() Token::isElementaryTypeName(currentToken)) { bool const allowVar = false; - stateVariables.push_back(parseVariableDeclaration(allowVar)); - if (visibilityIsPublic) - addStateVariableAccessor(stateVariables.back(), functions); + stateVariables.push_back(parseVariableDeclaration(allowVar, visibilityIsPublic)); expectToken(Token::SEMICOLON); } else if (currentToken == Token::MODIFIER) @@ -271,12 +245,12 @@ ASTPointer Parser::parseStructDefinition() return nodeFactory.createNode(name, members); } -ASTPointer Parser::parseVariableDeclaration(bool _allowVar) +ASTPointer Parser::parseVariableDeclaration(bool _allowVar, bool _isPublic) { ASTNodeFactory nodeFactory(*this); ASTPointer type = parseTypeName(_allowVar); nodeFactory.markEndPosition(); - return nodeFactory.createNode(type, expectIdentifierToken()); + return nodeFactory.createNode(type, expectIdentifierToken(), _isPublic); } ASTPointer Parser::parseModifierDefinition() diff --git a/libsolidity/Parser.h b/libsolidity/Parser.h index d911598b6..a8f97c6e3 100644 --- a/libsolidity/Parser.h +++ b/libsolidity/Parser.h @@ -52,7 +52,7 @@ private: ASTPointer parseInheritanceSpecifier(); ASTPointer parseFunctionDefinition(bool _isPublic, ASTString const* _contractName); ASTPointer parseStructDefinition(); - ASTPointer parseVariableDeclaration(bool _allowVar); + ASTPointer parseVariableDeclaration(bool _allowVar, bool _isPublic = false); ASTPointer parseModifierDefinition(); ASTPointer parseModifierInvocation(); ASTPointer parseIdentifier(); @@ -78,10 +78,6 @@ private: ///@{ ///@name Helper functions - /// Depending on whether a state Variable is Public, appends an accessor to the contract's functions - void addStateVariableAccessor(ASTPointer const& _varDecl, - std::vector> & _functions); - /// Peeks ahead in the scanner to determine if a variable definition is going to follow bool peekVariableDefinition(); diff --git a/libsolidity/Types.cpp b/libsolidity/Types.cpp index dad3a1455..d5b001636 100644 --- a/libsolidity/Types.cpp +++ b/libsolidity/Types.cpp @@ -489,7 +489,7 @@ MemberList const& ContractType::getMembers() const map> members(IntegerType::AddressMemberList.begin(), IntegerType::AddressMemberList.end()); for (auto const& it: m_contract.getInterfaceFunctions()) - members[it.second->getName()] = make_shared(*it.second, false); + members[it.second.second->getName()] = make_shared(*it.second.second, false); m_members.reset(new MemberList(members)); } return *m_members; @@ -512,7 +512,7 @@ u256 ContractType::getFunctionIdentifier(string const& _functionName) const { auto interfaceFunctions = m_contract.getInterfaceFunctions(); for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) - if (it->second->getName() == _functionName) + if (it->second.second->getName() == _functionName) return FixedHash<4>::Arith(it->first); return Invalid256; @@ -593,6 +593,19 @@ FunctionType::FunctionType(FunctionDefinition const& _function, bool _isInternal swap(retParams, m_returnParameterTypes); } +FunctionType::FunctionType(VariableDeclaration const& _varDecl): + m_location(Location::INTERNAL) +{ + TypePointers params; + TypePointers retParams; + // for now, no input parameters LTODO: change for some things like mapping + params.reserve(0); + retParams.reserve(1); + retParams.push_back(_varDecl.getType()); + swap(params, m_parameterTypes); + swap(retParams, m_returnParameterTypes); +} + bool FunctionType::operator==(Type const& _other) const { if (_other.getCategory() != getCategory()) @@ -672,9 +685,9 @@ MemberList const& FunctionType::getMembers() const } } -string FunctionType::getCanonicalSignature() const +string FunctionType::getCanonicalSignature(std::string const& _name) const { - string ret = "("; + string ret = _name + "("; for (auto it = m_parameterTypes.cbegin(); it != m_parameterTypes.cend(); ++it) ret += (*it)->toString() + (it + 1 == m_parameterTypes.cend() ? "" : ","); diff --git a/libsolidity/Types.h b/libsolidity/Types.h index b7f87d760..b3a92021b 100644 --- a/libsolidity/Types.h +++ b/libsolidity/Types.h @@ -353,6 +353,7 @@ public: virtual Category getCategory() const override { return Category::FUNCTION; } explicit FunctionType(FunctionDefinition const& _function, bool _isInternal = true); + explicit FunctionType(VariableDeclaration const& _varDecl); FunctionType(strings const& _parameterTypes, strings const& _returnParameterTypes, Location _location = Location::INTERNAL): FunctionType(parseElementaryTypeVector(_parameterTypes), parseElementaryTypeVector(_returnParameterTypes), @@ -375,7 +376,7 @@ public: virtual MemberList const& getMembers() const override; Location const& getLocation() const { return m_location; } - std::string getCanonicalSignature() const; + std::string getCanonicalSignature(std::string const &_name) const; bool gasSet() const { return m_gasSet; } bool valueSet() const { return m_valueSet; }