Extract scopes into compiler stack.

This commit is contained in:
chriseth 2017-01-31 22:59:56 +01:00
parent c87bafd2ed
commit e67faa9839
7 changed files with 19 additions and 6 deletions

View File

@ -34,8 +34,10 @@ namespace solidity
NameAndTypeResolver::NameAndTypeResolver( NameAndTypeResolver::NameAndTypeResolver(
vector<Declaration const*> const& _globals, vector<Declaration const*> const& _globals,
map<ASTNode const*, shared_ptr<DeclarationContainer>>& _scopes,
ErrorList& _errors ErrorList& _errors
) : ) :
m_scopes(_scopes),
m_errors(_errors) m_errors(_errors)
{ {
if (!m_scopes[nullptr]) if (!m_scopes[nullptr])

View File

@ -42,7 +42,11 @@ namespace solidity
class NameAndTypeResolver: private boost::noncopyable class NameAndTypeResolver: private boost::noncopyable
{ {
public: public:
NameAndTypeResolver(std::vector<Declaration const*> const& _globals, ErrorList& _errors); NameAndTypeResolver(
std::vector<Declaration const*> const& _globals,
std::map<ASTNode const*, std::shared_ptr<DeclarationContainer>>& _scopes,
ErrorList& _errors
);
/// Registers all declarations found in the AST node, usually a source unit. /// Registers all declarations found in the AST node, usually a source unit.
/// @returns false in case of error. /// @returns false in case of error.
bool registerDeclarations(ASTNode& _sourceUnit); bool registerDeclarations(ASTNode& _sourceUnit);
@ -113,7 +117,7 @@ private:
/// where nullptr denotes the global scope. Note that structs are not scope since they do /// where nullptr denotes the global scope. Note that structs are not scope since they do
/// not contain code. /// not contain code.
/// Aliases (for example `import "x" as y;`) create multiple pointers to the same scope. /// Aliases (for example `import "x" as y;`) create multiple pointers to the same scope.
std::map<ASTNode const*, std::shared_ptr<DeclarationContainer>> m_scopes; std::map<ASTNode const*, std::shared_ptr<DeclarationContainer>>& m_scopes;
DeclarationContainer* m_currentScope = nullptr; DeclarationContainer* m_currentScope = nullptr;
ErrorList& m_errors; ErrorList& m_errors;

View File

@ -88,6 +88,7 @@ void CompilerStack::reset(bool _keepSources)
m_optimize = false; m_optimize = false;
m_optimizeRuns = 200; m_optimizeRuns = 200;
m_globalContext.reset(); m_globalContext.reset();
m_scopes.clear();
m_sourceOrder.clear(); m_sourceOrder.clear();
m_contracts.clear(); m_contracts.clear();
m_errors.clear(); m_errors.clear();
@ -165,7 +166,7 @@ bool CompilerStack::parse()
noErrors = false; noErrors = false;
m_globalContext = make_shared<GlobalContext>(); m_globalContext = make_shared<GlobalContext>();
NameAndTypeResolver resolver(m_globalContext->declarations(), m_errors); NameAndTypeResolver resolver(m_globalContext->declarations(), m_scopes, m_errors);
for (Source const* source: m_sourceOrder) for (Source const* source: m_sourceOrder)
if (!resolver.registerDeclarations(*source->ast)) if (!resolver.registerDeclarations(*source->ast))
return false; return false;

View File

@ -52,6 +52,7 @@ namespace solidity
// forward declarations // forward declarations
class Scanner; class Scanner;
class ASTNode;
class ContractDefinition; class ContractDefinition;
class FunctionDefinition; class FunctionDefinition;
class SourceUnit; class SourceUnit;
@ -59,6 +60,7 @@ class Compiler;
class GlobalContext; class GlobalContext;
class InterfaceHandler; class InterfaceHandler;
class Error; class Error;
class DeclarationContainer;
enum class DocumentationType: uint8_t enum class DocumentationType: uint8_t
{ {
@ -271,6 +273,7 @@ private:
bool m_parseSuccessful; bool m_parseSuccessful;
std::map<std::string const, Source> m_sources; std::map<std::string const, Source> m_sources;
std::shared_ptr<GlobalContext> m_globalContext; std::shared_ptr<GlobalContext> m_globalContext;
std::map<ASTNode const*, std::shared_ptr<DeclarationContainer>> m_scopes;
std::vector<Source const*> m_sourceOrder; std::vector<Source const*> m_sourceOrder;
std::map<std::string const, Contract> m_contracts; std::map<std::string const, Contract> m_contracts;
std::string m_formalTranslation; std::string m_formalTranslation;

View File

@ -53,7 +53,8 @@ eth::AssemblyItems compileContract(const string& _sourceCode)
BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode)))); BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
BOOST_CHECK(!!sourceUnit); BOOST_CHECK(!!sourceUnit);
NameAndTypeResolver resolver({}, errors); map<ASTNode const*, shared_ptr<DeclarationContainer>> scopes;
NameAndTypeResolver resolver({}, scopes, errors);
solAssert(Error::containsOnlyWarnings(errors), ""); solAssert(Error::containsOnlyWarnings(errors), "");
resolver.registerDeclarations(*sourceUnit); resolver.registerDeclarations(*sourceUnit);
for (ASTPointer<ASTNode> const& node: sourceUnit->nodes()) for (ASTPointer<ASTNode> const& node: sourceUnit->nodes())

View File

@ -114,7 +114,8 @@ bytes compileFirstExpression(
declarations.push_back(variable.get()); declarations.push_back(variable.get());
ErrorList errors; ErrorList errors;
NameAndTypeResolver resolver(declarations, errors); map<ASTNode const*, shared_ptr<DeclarationContainer>> scopes;
NameAndTypeResolver resolver(declarations, scopes, errors);
resolver.registerDeclarations(*sourceUnit); resolver.registerDeclarations(*sourceUnit);
vector<ContractDefinition const*> inheritanceHierarchy; vector<ContractDefinition const*> inheritanceHierarchy;

View File

@ -66,7 +66,8 @@ parseAnalyseAndReturnError(string const& _source, bool _reportWarnings = false,
return make_pair(sourceUnit, errors.at(0)); return make_pair(sourceUnit, errors.at(0));
std::shared_ptr<GlobalContext> globalContext = make_shared<GlobalContext>(); std::shared_ptr<GlobalContext> globalContext = make_shared<GlobalContext>();
NameAndTypeResolver resolver(globalContext->declarations(), errors); map<ASTNode const*, shared_ptr<DeclarationContainer>> scopes;
NameAndTypeResolver resolver(globalContext->declarations(), scopes, errors);
solAssert(Error::containsOnlyWarnings(errors), ""); solAssert(Error::containsOnlyWarnings(errors), "");
resolver.registerDeclarations(*sourceUnit); resolver.registerDeclarations(*sourceUnit);