aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLefteris Karapetsas <lefteris@refu.co>2015-01-22 00:19:57 +0800
committerLefteris Karapetsas <lefteris@refu.co>2015-01-29 04:46:16 +0800
commit97c31b3e7d6a0bc4b024b15bc13d19b851b68ead (patch)
treebb87bbd36711ff31dbda53613f15d5cfdad9de5e
parent282d4b8add13fd81a0320fa0d3db331e09b3d487 (diff)
downloaddexon-solidity-97c31b3e7d6a0bc4b024b15bc13d19b851b68ead.tar.gz
dexon-solidity-97c31b3e7d6a0bc4b024b15bc13d19b851b68ead.tar.zst
dexon-solidity-97c31b3e7d6a0bc4b024b15bc13d19b851b68ead.zip
Parsing accessor functions for public contract state variables
- During the contract parsing depending on whether or not a state variable is public an extra acessor FunctionDefinition is parsed for it
-rw-r--r--Parser.cpp25
-rw-r--r--Parser.h4
2 files changed, 29 insertions, 0 deletions
diff --git a/Parser.cpp b/Parser.cpp
index d99d33ac..cd395c77 100644
--- a/Parser.cpp
+++ b/Parser.cpp
@@ -109,6 +109,29 @@ ASTPointer<ImportDirective> Parser::parseImportDirective()
return nodeFactory.createNode<ImportDirective>(url);
}
+void Parser::addStateVariableAccessor(ASTPointer<VariableDeclaration> const& _varDecl,
+ vector<ASTPointer<FunctionDefinition>> & _functions)
+{
+ ASTNodeFactory nodeFactory(*this);
+ ASTPointer<ASTString> emptyDoc;
+ ASTPointer<ParameterList> emptyParamList;
+ nodeFactory.markEndPosition();
+ auto expression = nodeFactory.createNode<Identifier>(make_shared<ASTString>(_varDecl->getName()));
+ vector<ASTPointer<Statement>> block_statements = {nodeFactory.createNode<Return>(expression)};
+
+ _functions.push_back(nodeFactory.createNode<FunctionDefinition>(
+ make_shared<ASTString>(_varDecl->getName()),
+ true, // isPublic
+ false, // not a Constructor
+ emptyDoc, // no documentation
+ emptyParamList, // no parameters (temporary, a mapping would need parameters for example)
+ true, // is constant
+ emptyParamList, // no return parameters
+ nodeFactory.createNode<Block>(block_statements)
+ )
+ );
+}
+
ASTPointer<ContractDefinition> Parser::parseContractDefinition()
{
ASTNodeFactory nodeFactory(*this);
@@ -151,6 +174,8 @@ ASTPointer<ContractDefinition> Parser::parseContractDefinition()
{
bool const allowVar = false;
stateVariables.push_back(parseVariableDeclaration(allowVar));
+ if (visibilityIsPublic)
+ addStateVariableAccessor(stateVariables.back(), functions);
expectToken(Token::SEMICOLON);
}
else if (currentToken == Token::MODIFIER)
diff --git a/Parser.h b/Parser.h
index 211e952d..d911598b 100644
--- a/Parser.h
+++ b/Parser.h
@@ -78,6 +78,10 @@ private:
///@{
///@name Helper functions
+ /// Depending on whether a state Variable is Public, appends an accessor to the contract's functions
+ void addStateVariableAccessor(ASTPointer<VariableDeclaration> const& _varDecl,
+ std::vector<ASTPointer<FunctionDefinition>> & _functions);
+
/// Peeks ahead in the scanner to determine if a variable definition is going to follow
bool peekVariableDefinition();