mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Allow aliases during import.
This commit is contained in:
parent
aa645d1192
commit
f20a604c5a
@ -49,6 +49,8 @@ Declaration const* DeclarationContainer::conflictingDeclaration(
|
||||
if (!dynamic_cast<FunctionDefinition const*>(declaration))
|
||||
return declaration;
|
||||
}
|
||||
else if (declarations.size() == 1 && declarations.front() == &_declaration)
|
||||
return nullptr;
|
||||
else if (!declarations.empty())
|
||||
return declarations.front();
|
||||
|
||||
@ -73,13 +75,12 @@ bool DeclarationContainer::registerDeclaration(
|
||||
m_declarations.erase(*_name);
|
||||
m_invisibleDeclarations.erase(*_name);
|
||||
}
|
||||
else if (conflictingDeclaration(_declaration))
|
||||
else if (conflictingDeclaration(_declaration, _name))
|
||||
return false;
|
||||
|
||||
if (_invisible)
|
||||
m_invisibleDeclarations[*_name].push_back(&_declaration);
|
||||
else
|
||||
m_declarations[*_name].push_back(&_declaration);
|
||||
vector<Declaration const*>& decls = _invisible ? m_invisibleDeclarations[*_name] : m_declarations[*_name];
|
||||
if (!contains(decls, &_declaration))
|
||||
decls.push_back(&_declaration);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -76,23 +76,58 @@ bool NameAndTypeResolver::performImports(SourceUnit& _sourceUnit, map<string, So
|
||||
string const& path = imp->annotation().absolutePath;
|
||||
if (!_sourceUnits.count(path))
|
||||
{
|
||||
reportDeclarationError( node->location(),
|
||||
"Import \"" +
|
||||
path +
|
||||
"\" (referenced as \"" +
|
||||
imp->path() +
|
||||
"\") not found."
|
||||
reportDeclarationError(
|
||||
imp->location(),
|
||||
"Import \"" + path + "\" (referenced as \"" + imp->path() + "\") not found."
|
||||
);
|
||||
error = true;
|
||||
continue;
|
||||
}
|
||||
auto scope = m_scopes.find(_sourceUnits.at(path));
|
||||
solAssert(scope != end(m_scopes), "");
|
||||
if (!imp->symbolAliases().empty())
|
||||
for (auto const& alias: imp->symbolAliases())
|
||||
{
|
||||
auto declarations = scope->second->resolveName(alias.first->name(), false);
|
||||
if (declarations.empty())
|
||||
{
|
||||
reportDeclarationError(
|
||||
imp->location(),
|
||||
"Declaration \"" +
|
||||
alias.first->name() +
|
||||
"\" not found in \"" +
|
||||
path +
|
||||
"\" (referenced as \"" +
|
||||
imp->path() +
|
||||
"\")."
|
||||
);
|
||||
error = true;
|
||||
}
|
||||
else
|
||||
for (Declaration const* declaration: declarations)
|
||||
{
|
||||
ASTString const* name = alias.second ? alias.second.get() : &declaration->name();
|
||||
if (!target.registerDeclaration(*declaration, name))
|
||||
{
|
||||
reportDeclarationError(
|
||||
imp->location(),
|
||||
"Identifier \"" + *name + "\" already declared."
|
||||
);
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (imp->name().empty())
|
||||
{
|
||||
auto scope = m_scopes.find(_sourceUnits.at(path));
|
||||
solAssert(scope != end(m_scopes), "");
|
||||
for (auto const& nameAndDeclaration: scope->second->declarations())
|
||||
for (auto const& declaration: nameAndDeclaration.second)
|
||||
target.registerDeclaration(*declaration, &nameAndDeclaration.first);
|
||||
}
|
||||
if (!target.registerDeclaration(*declaration, &nameAndDeclaration.first))
|
||||
{
|
||||
reportDeclarationError(
|
||||
imp->location(),
|
||||
"Identifier \"" + nameAndDeclaration.first + "\" already declared."
|
||||
);
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
return !error;
|
||||
}
|
||||
|
@ -201,6 +201,10 @@ public:
|
||||
virtual void accept(ASTConstVisitor& _visitor) const override;
|
||||
|
||||
ASTString const& path() const { return *m_path; }
|
||||
std::vector<std::pair<ASTPointer<Identifier>, ASTPointer<ASTString>>> const& symbolAliases() const
|
||||
{
|
||||
return m_symbolAliases;
|
||||
}
|
||||
virtual ImportAnnotation& annotation() const override;
|
||||
|
||||
virtual TypePointer type() const override;
|
||||
|
@ -101,6 +101,31 @@ BOOST_AUTO_TEST_CASE(simple_alias)
|
||||
BOOST_CHECK(c.compile());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(complex_import)
|
||||
{
|
||||
CompilerStack c;
|
||||
c.addSource("a", "contract A {} contract B {} contract C { struct S { uint a; } }");
|
||||
c.addSource("b", "import \"a\" as x; import {B as b, C as c, C} from \"a\"; "
|
||||
"contract D is b { function f(c.S var1, x.C.S var2, C.S var3) internal {} }");
|
||||
BOOST_CHECK(c.compile());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(name_clash_in_import)
|
||||
{
|
||||
CompilerStack c;
|
||||
c.addSource("a", "contract A {}");
|
||||
c.addSource("b", "import \"a\"; contract A {} ");
|
||||
BOOST_CHECK(!c.compile());
|
||||
c.addSource("b", "import \"a\" as A; contract A {} ");
|
||||
BOOST_CHECK(!c.compile());
|
||||
c.addSource("b", "import {A as b} from \"a\"; contract b {} ");
|
||||
BOOST_CHECK(!c.compile());
|
||||
c.addSource("b", "import {A} from \"a\"; contract A {} ");
|
||||
BOOST_CHECK(!c.compile());
|
||||
c.addSource("b", "import {A} from \"a\"; contract B {} ");
|
||||
BOOST_CHECK(c.compile());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user