Refactored annotations.

This commit is contained in:
chriseth 2015-09-21 18:55:58 +02:00
parent 34a81fd60e
commit 39d1e2bc06
8 changed files with 188 additions and 47 deletions

View File

@ -41,6 +41,14 @@ ASTNode::ASTNode(SourceLocation const& _location):
ASTNode::~ASTNode() ASTNode::~ASTNode()
{ {
delete m_annotation;
}
ASTAnnotation& ASTNode::annotation() const
{
if (!m_annotation)
m_annotation = new ASTAnnotation();
return *m_annotation;
} }
TypeError ASTNode::createTypeError(string const& _description) const TypeError ASTNode::createTypeError(string const& _description) const
@ -188,6 +196,20 @@ TypePointer ContractDefinition::type(ContractDefinition const* m_currentContract
return make_shared<TypeType>(make_shared<ContractType>(*this), m_currentContract); return make_shared<TypeType>(make_shared<ContractType>(*this), m_currentContract);
} }
ContractDefinitionAnnotation& ContractDefinition::annotation() const
{
if (!m_annotation)
m_annotation = new ContractDefinitionAnnotation();
return static_cast<ContractDefinitionAnnotation&>(*m_annotation);
}
TypeNameAnnotation& TypeName::annotation() const
{
if (!m_annotation)
m_annotation = new TypeNameAnnotation();
return static_cast<TypeNameAnnotation&>(*m_annotation);
}
TypePointer StructDefinition::type(ContractDefinition const*) const TypePointer StructDefinition::type(ContractDefinition const*) const
{ {
return make_shared<TypeType>(make_shared<StructType>(*this)); return make_shared<TypeType>(make_shared<StructType>(*this));
@ -225,6 +247,13 @@ TypePointer EventDefinition::type(ContractDefinition const*) const
return make_shared<FunctionType>(*this); return make_shared<FunctionType>(*this);
} }
UserDefinedTypeNameAnnotation& UserDefinedTypeName::annotation() const
{
if (!m_annotation)
m_annotation = new UserDefinedTypeNameAnnotation();
return static_cast<UserDefinedTypeNameAnnotation&>(*m_annotation);
}
bool VariableDeclaration::isLValue() const bool VariableDeclaration::isLValue() const
{ {
// External function parameters and constant declared variables are Read-Only // External function parameters and constant declared variables are Read-Only
@ -267,3 +296,52 @@ TypePointer VariableDeclaration::type(ContractDefinition const*) const
{ {
return annotation().type; return annotation().type;
} }
VariableDeclarationAnnotation& VariableDeclaration::annotation() const
{
if (!m_annotation)
m_annotation = new VariableDeclarationAnnotation();
return static_cast<VariableDeclarationAnnotation&>(*m_annotation);
}
ReturnAnnotation& Return::annotation() const
{
if (!m_annotation)
m_annotation = new ReturnAnnotation();
return static_cast<ReturnAnnotation&>(*m_annotation);
}
ExpressionAnnotation& Expression::annotation() const
{
if (!m_annotation)
m_annotation = new ExpressionAnnotation();
return static_cast<ExpressionAnnotation&>(*m_annotation);
}
MemberAccessAnnotation& MemberAccess::annotation() const
{
if (!m_annotation)
m_annotation = new MemberAccessAnnotation();
return static_cast<MemberAccessAnnotation&>(*m_annotation);
}
BinaryOperationAnnotation& BinaryOperation::annotation() const
{
if (!m_annotation)
m_annotation = new BinaryOperationAnnotation();
return static_cast<BinaryOperationAnnotation&>(*m_annotation);
}
FunctionCallAnnotation& FunctionCall::annotation() const
{
if (!m_annotation)
m_annotation = new FunctionCallAnnotation();
return static_cast<FunctionCallAnnotation&>(*m_annotation);
}
IdentifierAnnotation& Identifier::annotation() const
{
if (!m_annotation)
m_annotation = new IdentifierAnnotation();
return static_cast<IdentifierAnnotation&>(*m_annotation);
}

View File

@ -42,7 +42,6 @@ namespace solidity
class ASTVisitor; class ASTVisitor;
class ASTConstVisitor; class ASTConstVisitor;
struct ASTAnnotation;
/** /**
@ -79,7 +78,7 @@ public:
TypeError createTypeError(std::string const& _description) const; TypeError createTypeError(std::string const& _description) const;
///@todo make this const-safe by providing a different way to access the annotation ///@todo make this const-safe by providing a different way to access the annotation
ASTAnnotation& annotation() const { return const_cast<ASTAnnotation&>(m_annotation); } virtual ASTAnnotation& annotation() const;
///@{ ///@{
///@name equality operators ///@name equality operators
@ -88,10 +87,12 @@ public:
bool operator!=(ASTNode const& _other) const { return !operator==(_other); } bool operator!=(ASTNode const& _other) const { return !operator==(_other); }
///@} ///@}
protected:
/// Annotation - is specialised in derived classes, is created upon request (because of polymorphism).
mutable ASTAnnotation* m_annotation = nullptr;
private: private:
SourceLocation m_location; SourceLocation m_location;
ASTAnnotation m_annotation;
}; };
/** /**
@ -288,6 +289,8 @@ public:
virtual TypePointer type(ContractDefinition const* m_currentContract) const override; virtual TypePointer type(ContractDefinition const* m_currentContract) const override;
virtual ContractDefinitionAnnotation& annotation() const override;
private: private:
std::vector<ASTPointer<InheritanceSpecifier>> m_baseContracts; std::vector<ASTPointer<InheritanceSpecifier>> m_baseContracts;
std::vector<ASTPointer<StructDefinition>> m_definedStructs; std::vector<ASTPointer<StructDefinition>> m_definedStructs;
@ -538,6 +541,8 @@ public:
virtual TypePointer type(ContractDefinition const* m_currentContract) const override; virtual TypePointer type(ContractDefinition const* m_currentContract) const override;
virtual VariableDeclarationAnnotation& annotation() const override;
protected: protected:
Visibility defaultVisibility() const override { return Visibility::Internal; } Visibility defaultVisibility() const override { return Visibility::Internal; }
@ -666,6 +671,8 @@ public:
explicit TypeName(SourceLocation const& _location): ASTNode(_location) {} explicit TypeName(SourceLocation const& _location): ASTNode(_location) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
virtual TypeNameAnnotation& annotation() const override;
}; };
/** /**
@ -702,6 +709,8 @@ public:
ASTString const& name() const { return *m_name; } ASTString const& name() const { return *m_name; }
virtual UserDefinedTypeNameAnnotation& annotation() const override;
private: private:
ASTPointer<ASTString> m_name; ASTPointer<ASTString> m_name;
}; };
@ -904,6 +913,8 @@ public:
Expression const* expression() const { return m_expression.get(); } Expression const* expression() const { return m_expression.get(); }
virtual ReturnAnnotation& annotation() const override;
private: private:
ASTPointer<Expression> m_expression; ///< value to return, optional ASTPointer<Expression> m_expression; ///< value to return, optional
}; };
@ -970,6 +981,8 @@ class Expression: public ASTNode
{ {
public: public:
Expression(SourceLocation const& _location): ASTNode(_location) {} Expression(SourceLocation const& _location): ASTNode(_location) {}
ExpressionAnnotation& annotation() const override;
}; };
/// Assignment, can also be a compound assignment. /// Assignment, can also be a compound assignment.
@ -1050,6 +1063,8 @@ public:
Expression const& rightExpression() const { return *m_right; } Expression const& rightExpression() const { return *m_right; }
Token::Value getOperator() const { return m_operator; } Token::Value getOperator() const { return m_operator; }
BinaryOperationAnnotation& annotation() const override;
private: private:
ASTPointer<Expression> m_left; ASTPointer<Expression> m_left;
Token::Value m_operator; Token::Value m_operator;
@ -1072,6 +1087,8 @@ public:
std::vector<ASTPointer<Expression const>> arguments() const { return {m_arguments.begin(), m_arguments.end()}; } std::vector<ASTPointer<Expression const>> arguments() const { return {m_arguments.begin(), m_arguments.end()}; }
std::vector<ASTPointer<ASTString>> const& names() const { return m_names; } std::vector<ASTPointer<ASTString>> const& names() const { return m_names; }
virtual FunctionCallAnnotation& annotation() const override;
private: private:
ASTPointer<Expression> m_expression; ASTPointer<Expression> m_expression;
std::vector<ASTPointer<Expression>> m_arguments; std::vector<ASTPointer<Expression>> m_arguments;
@ -1109,6 +1126,8 @@ public:
Expression const& expression() const { return *m_expression; } Expression const& expression() const { return *m_expression; }
ASTString const& memberName() const { return *m_memberName; } ASTString const& memberName() const { return *m_memberName; }
virtual MemberAccessAnnotation& annotation() const override;
private: private:
ASTPointer<Expression> m_expression; ASTPointer<Expression> m_expression;
ASTPointer<ASTString> m_memberName; ASTPointer<ASTString> m_memberName;
@ -1157,6 +1176,8 @@ public:
ASTString const& name() const { return *m_name; } ASTString const& name() const { return *m_name; }
virtual IdentifierAnnotation& annotation() const override;
private: private:
ASTPointer<ASTString> m_name; ASTPointer<ASTString> m_name;
}; };

View File

@ -25,54 +25,99 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <libsolidity/ASTForward.h>
namespace dev namespace dev
{ {
namespace solidity namespace solidity
{ {
class ASTNode;
class ContractDefinition;
class Declaration;
class ParameterList;
class Type; class Type;
using TypePointer = std::shared_ptr<Type const>; using TypePointer = std::shared_ptr<Type const>;
struct ASTAnnotation struct ASTAnnotation
{ {
///@TODO save space here - we do not need all members for all types. virtual ~ASTAnnotation() {}
};
/// For expression: Inferred type. - For type declaration: Declared type. - For variable declaration: Type of variable. struct ContractDefinitionAnnotation: ASTAnnotation
TypePointer type; {
/// For expression: Whether it is an LValue (i.e. something that can be assigned to). /// Whether all functions are implemented.
bool isLValue = false;
/// For expression: Whether the expression is used in a context where the LValue is actually required.
bool lValueRequested = false;
/// For expressions: Types of arguments if the expression is a function that is called - used
/// for overload resolution.
std::shared_ptr<std::vector<TypePointer>> argumentTypes;
/// For contract: Whether all functions are implemented.
bool isFullyImplemented = true; bool isFullyImplemented = true;
/// For contract: List of all (direct and indirect) base contracts in order from derived to /// List of all (direct and indirect) base contracts in order from derived to
/// base, including the contract itself. /// base, including the contract itself.
std::vector<ContractDefinition const*> linearizedBaseContracts; std::vector<ContractDefinition const*> linearizedBaseContracts;
/// For member access and Identifer: Referenced declaration, set during overload resolution stage. };
Declaration const* referencedDeclaration = nullptr;
/// For Identifier: List of possible declarations it could refer to. struct VariableDeclarationAnnotation: ASTAnnotation
std::vector<Declaration const*> overloadedDeclarations; {
/// For function call: Whether this is an explicit type conversion. /// Type of variable (type of identifier referencing this variable).
bool isTypeConversion = false; TypePointer type;
/// For function call: Whether this is a struct constructor call. };
bool isStructConstructorCall = false;
/// For Return statement: Reference to the return parameters of the function. struct ReturnAnnotation: ASTAnnotation
{
/// Reference to the return parameters of the function.
ParameterList const* functionReturnParameters = nullptr; ParameterList const* functionReturnParameters = nullptr;
/// For Identifier: Stores a reference to the current contract. };
struct TypeNameAnnotation: ASTAnnotation
{
/// Type declared by this type name, i.e. type of a variable where this type name is used.
/// Set during reference resolution stage.
TypePointer type;
};
struct UserDefinedTypeNameAnnotation: TypeNameAnnotation
{
/// Referenced declaration, set during reference resolution stage.
Declaration const* referencedDeclaration = nullptr;
};
struct ExpressionAnnotation: ASTAnnotation
{
/// Inferred type of the expression.
TypePointer type;
/// Whether it is an LValue (i.e. something that can be assigned to).
bool isLValue = false;
/// Whether the expression is used in a context where the LValue is actually required.
bool lValueRequested = false;
/// Types of arguments if the expression is a function that is called - used
/// for overload resolution.
std::shared_ptr<std::vector<TypePointer>> argumentTypes;
};
struct IdentifierAnnotation: ExpressionAnnotation
{
/// Stores a reference to the current contract.
/// This is needed because types of base contracts change depending on the context. /// This is needed because types of base contracts change depending on the context.
ContractDefinition const* contractScope = nullptr; ContractDefinition const* contractScope = nullptr;
/// For BinaryOperation: The common type that is used for the operation, not necessarily the result type (e.g. for /// Referenced declaration, set at latest during overload resolution stage.
/// comparisons, this is always bool). Declaration const* referencedDeclaration = nullptr;
/// List of possible declarations it could refer to.
std::vector<Declaration const*> overloadedDeclarations;
};
struct MemberAccessAnnotation: ExpressionAnnotation
{
/// Referenced declaration, set at latest during overload resolution stage.
Declaration const* referencedDeclaration = nullptr;
};
struct BinaryOperationAnnotation: ExpressionAnnotation
{
/// The common type that is used for the operation, not necessarily the result type (which
/// e.g. for comparisons is bool).
TypePointer commonType; TypePointer commonType;
}; };
struct FunctionCallAnnotation: ExpressionAnnotation
{
/// Whether this is an explicit type conversion.
bool isTypeConversion = false;
/// Whether this is a struct constructor call.
bool isStructConstructorCall = false;
};
} }
} }

View File

@ -1274,9 +1274,9 @@ void ExpressionCompiler::appendExpressionCopyToMemory(Type const& _expectedType,
void ExpressionCompiler::setLValueFromDeclaration(Declaration const& _declaration, Expression const& _expression) void ExpressionCompiler::setLValueFromDeclaration(Declaration const& _declaration, Expression const& _expression)
{ {
if (m_context.isLocalVariable(&_declaration)) if (m_context.isLocalVariable(&_declaration))
setLValue<StackVariable>(_expression, _declaration); setLValue<StackVariable>(_expression, dynamic_cast<VariableDeclaration const&>(_declaration));
else if (m_context.isStateVariable(&_declaration)) else if (m_context.isStateVariable(&_declaration))
setLValue<StorageItem>(_expression, _declaration); setLValue<StorageItem>(_expression, dynamic_cast<VariableDeclaration const&>(_declaration));
else else
BOOST_THROW_EXCEPTION(InternalCompilerError() BOOST_THROW_EXCEPTION(InternalCompilerError()
<< errinfo_sourceLocation(_expression.location()) << errinfo_sourceLocation(_expression.location())

View File

@ -31,7 +31,7 @@ using namespace dev;
using namespace solidity; using namespace solidity;
StackVariable::StackVariable(CompilerContext& _compilerContext, Declaration const& _declaration): StackVariable::StackVariable(CompilerContext& _compilerContext, VariableDeclaration const& _declaration):
LValue(_compilerContext, *_declaration.annotation().type), LValue(_compilerContext, *_declaration.annotation().type),
m_baseStackOffset(m_context.baseStackOffsetOfVariable(_declaration)), m_baseStackOffset(m_context.baseStackOffsetOfVariable(_declaration)),
m_size(m_dataType.sizeOnStack()) m_size(m_dataType.sizeOnStack())
@ -131,7 +131,7 @@ void MemoryItem::setToZero(SourceLocation const&, bool _removeReference) const
m_context << eth::Instruction::POP; m_context << eth::Instruction::POP;
} }
StorageItem::StorageItem(CompilerContext& _compilerContext, Declaration const& _declaration): StorageItem::StorageItem(CompilerContext& _compilerContext, VariableDeclaration const& _declaration):
StorageItem(_compilerContext, *_declaration.annotation().type) StorageItem(_compilerContext, *_declaration.annotation().type)
{ {
auto const& location = m_context.storageLocationOfVariable(_declaration); auto const& location = m_context.storageLocationOfVariable(_declaration);

View File

@ -35,6 +35,7 @@ class Declaration;
class Type; class Type;
class ArrayType; class ArrayType;
class CompilerContext; class CompilerContext;
class VariableDeclaration;
/** /**
* Abstract class used to retrieve, delete and store data in lvalues/variables. * Abstract class used to retrieve, delete and store data in lvalues/variables.
@ -76,7 +77,7 @@ protected:
class StackVariable: public LValue class StackVariable: public LValue
{ {
public: public:
StackVariable(CompilerContext& _compilerContext, Declaration const& _declaration); StackVariable(CompilerContext& _compilerContext, VariableDeclaration const& _declaration);
virtual unsigned sizeOnStack() const override { return 0; } virtual unsigned sizeOnStack() const override { return 0; }
virtual void retrieveValue(SourceLocation const& _location, bool _remove = false) const override; virtual void retrieveValue(SourceLocation const& _location, bool _remove = false) const override;
@ -129,7 +130,7 @@ class StorageItem: public LValue
{ {
public: public:
/// Constructs the LValue and pushes the location of @a _declaration onto the stack. /// Constructs the LValue and pushes the location of @a _declaration onto the stack.
StorageItem(CompilerContext& _compilerContext, Declaration const& _declaration); StorageItem(CompilerContext& _compilerContext, VariableDeclaration const& _declaration);
/// Constructs the LValue and assumes that the storage reference is already on the stack. /// Constructs the LValue and assumes that the storage reference is already on the stack.
StorageItem(CompilerContext& _compilerContext, Type const& _type); StorageItem(CompilerContext& _compilerContext, Type const& _type);
virtual unsigned sizeOnStack() const override { return 2; } virtual unsigned sizeOnStack() const override { return 2; }

View File

@ -173,9 +173,9 @@ TypePointer ReferencesResolver::typeFor(TypeName const& _typeName)
TypePointer type; TypePointer type;
if (auto elemTypeName = dynamic_cast<ElementaryTypeName const*>(&_typeName)) if (auto elemTypeName = dynamic_cast<ElementaryTypeName const*>(&_typeName))
type = Type::fromElementaryTypeName(elemTypeName->typeName()); type = Type::fromElementaryTypeName(elemTypeName->typeName());
else if (dynamic_cast<UserDefinedTypeName const*>(&_typeName)) else if (auto typeName = dynamic_cast<UserDefinedTypeName const*>(&_typeName))
{ {
Declaration const* declaration = _typeName.annotation().referencedDeclaration; Declaration const* declaration = typeName->annotation().referencedDeclaration;
solAssert(!!declaration, ""); solAssert(!!declaration, "");
if (StructDefinition const* structDef = dynamic_cast<StructDefinition const*>(declaration)) if (StructDefinition const* structDef = dynamic_cast<StructDefinition const*>(declaration))
@ -185,7 +185,7 @@ TypePointer ReferencesResolver::typeFor(TypeName const& _typeName)
else if (ContractDefinition const* contract = dynamic_cast<ContractDefinition const*>(declaration)) else if (ContractDefinition const* contract = dynamic_cast<ContractDefinition const*>(declaration))
type = make_shared<ContractType>(*contract); type = make_shared<ContractType>(*contract);
else else
BOOST_THROW_EXCEPTION(_typeName.createTypeError( BOOST_THROW_EXCEPTION(typeName->createTypeError(
"Name has to refer to a struct, enum or contract." "Name has to refer to a struct, enum or contract."
)); ));
} }

View File

@ -402,7 +402,7 @@ bool TypeChecker::visit(FunctionDefinition const& _function)
visitManually( visitManually(
*modifier, *modifier,
_function.isConstructor() ? _function.isConstructor() ?
_function.scope()->annotation().linearizedBaseContracts : dynamic_cast<ContractDefinition const&>(*_function.scope()).annotation().linearizedBaseContracts :
vector<ContractDefinition const*>() vector<ContractDefinition const*>()
); );
if (_function.isImplemented()) if (_function.isImplemented())
@ -484,12 +484,8 @@ void TypeChecker::visitManually(
) )
{ {
std::vector<ASTPointer<Expression>> const& arguments = _modifier.arguments(); std::vector<ASTPointer<Expression>> const& arguments = _modifier.arguments();
_modifier.annotation().argumentTypes = make_shared<TypePointers>();
for (ASTPointer<Expression> const& argument: arguments) for (ASTPointer<Expression> const& argument: arguments)
{
argument->accept(*this); argument->accept(*this);
_modifier.annotation().argumentTypes->push_back(type(*argument));
}
_modifier.name()->accept(*this); _modifier.name()->accept(*this);
auto const* declaration = &dereference(*_modifier.name()); auto const* declaration = &dereference(*_modifier.name());
@ -1049,7 +1045,7 @@ bool TypeChecker::visit(IndexAccess const& _access)
bool TypeChecker::visit(Identifier const& _identifier) bool TypeChecker::visit(Identifier const& _identifier)
{ {
ASTAnnotation& annotation = _identifier.annotation(); IdentifierAnnotation& annotation = _identifier.annotation();
if (!annotation.referencedDeclaration) if (!annotation.referencedDeclaration)
{ {
if (!annotation.argumentTypes) if (!annotation.argumentTypes)