aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity
diff options
context:
space:
mode:
Diffstat (limited to 'libsolidity')
-rw-r--r--libsolidity/analysis/NameAndTypeResolver.cpp129
-rw-r--r--libsolidity/analysis/NameAndTypeResolver.h9
-rw-r--r--libsolidity/analysis/StaticAnalyzer.cpp9
-rw-r--r--libsolidity/analysis/StaticAnalyzer.h6
-rw-r--r--libsolidity/ast/Types.cpp101
-rw-r--r--libsolidity/ast/Types.h23
-rw-r--r--libsolidity/codegen/CompilerUtils.cpp2
-rw-r--r--libsolidity/interface/CompilerStack.cpp31
-rw-r--r--libsolidity/interface/CompilerStack.h40
-rw-r--r--libsolidity/interface/ErrorReporter.cpp14
-rw-r--r--libsolidity/interface/ErrorReporter.h22
-rw-r--r--libsolidity/interface/StandardCompiler.cpp4
-rw-r--r--libsolidity/parsing/Token.cpp10
13 files changed, 227 insertions, 173 deletions
diff --git a/libsolidity/analysis/NameAndTypeResolver.cpp b/libsolidity/analysis/NameAndTypeResolver.cpp
index aac90311..df83f382 100644
--- a/libsolidity/analysis/NameAndTypeResolver.cpp
+++ b/libsolidity/analysis/NameAndTypeResolver.cpp
@@ -104,29 +104,18 @@ bool NameAndTypeResolver::performImports(SourceUnit& _sourceUnit, map<string, So
}
else
for (Declaration const* declaration: declarations)
- {
- ASTString const* name = alias.second ? alias.second.get() : &declaration->name();
- if (!target.registerDeclaration(*declaration, name))
- {
- m_errorReporter.declarationError(
- imp->location(),
- "Identifier \"" + *name + "\" already declared."
- );
+ if (!DeclarationRegistrationHelper::registerDeclaration(
+ target, *declaration, alias.second.get(), &imp->location(), true, m_errorReporter
+ ))
error = true;
- }
- }
}
else if (imp->name().empty())
for (auto const& nameAndDeclaration: scope->second->declarations())
for (auto const& declaration: nameAndDeclaration.second)
- if (!target.registerDeclaration(*declaration, &nameAndDeclaration.first))
- {
- m_errorReporter.declarationError(
- imp->location(),
- "Identifier \"" + nameAndDeclaration.first + "\" already declared."
- );
- error = true;
- }
+ if (!DeclarationRegistrationHelper::registerDeclaration(
+ target, *declaration, &nameAndDeclaration.first, &imp->location(), true, m_errorReporter
+ ))
+ error = true;
}
return !error;
}
@@ -450,6 +439,75 @@ DeclarationRegistrationHelper::DeclarationRegistrationHelper(
solAssert(m_currentScope == _currentScope, "Scopes not correctly closed.");
}
+bool DeclarationRegistrationHelper::registerDeclaration(
+ DeclarationContainer& _container,
+ Declaration const& _declaration,
+ string const* _name,
+ SourceLocation const* _errorLocation,
+ bool _warnOnShadow,
+ ErrorReporter& _errorReporter
+)
+{
+ if (!_errorLocation)
+ _errorLocation = &_declaration.location();
+
+ Declaration const* shadowedDeclaration = nullptr;
+ if (_warnOnShadow && !_declaration.name().empty())
+ for (auto const* decl: _container.resolveName(_declaration.name(), true))
+ if (decl != &_declaration)
+ {
+ shadowedDeclaration = decl;
+ break;
+ }
+
+ if (!_container.registerDeclaration(_declaration, _name, !_declaration.isVisibleInContract()))
+ {
+ SourceLocation firstDeclarationLocation;
+ SourceLocation secondDeclarationLocation;
+ Declaration const* conflictingDeclaration = _container.conflictingDeclaration(_declaration, _name);
+ solAssert(conflictingDeclaration, "");
+ bool const comparable =
+ _errorLocation->sourceName &&
+ conflictingDeclaration->location().sourceName &&
+ *_errorLocation->sourceName == *conflictingDeclaration->location().sourceName;
+ if (comparable && _errorLocation->start < conflictingDeclaration->location().start)
+ {
+ firstDeclarationLocation = *_errorLocation;
+ secondDeclarationLocation = conflictingDeclaration->location();
+ }
+ else
+ {
+ firstDeclarationLocation = conflictingDeclaration->location();
+ secondDeclarationLocation = *_errorLocation;
+ }
+
+ _errorReporter.declarationError(
+ secondDeclarationLocation,
+ SecondarySourceLocation().append("The previous declaration is here:", firstDeclarationLocation),
+ "Identifier already declared."
+ );
+ return false;
+ }
+ else if (shadowedDeclaration)
+ {
+ if (dynamic_cast<MagicVariableDeclaration const*>(shadowedDeclaration))
+ _errorReporter.warning(
+ _declaration.location(),
+ "This declaration shadows a builtin symbol."
+ );
+ else
+ {
+ auto shadowedLocation = shadowedDeclaration->location();
+ _errorReporter.warning(
+ _declaration.location(),
+ "This declaration shadows an existing declaration.",
+ SecondarySourceLocation().append("The shadowed declaration is here:", shadowedLocation)
+ );
+ }
+ }
+ return true;
+}
+
bool DeclarationRegistrationHelper::visit(SourceUnit& _sourceUnit)
{
if (!m_scopes[&_sourceUnit])
@@ -590,30 +648,21 @@ void DeclarationRegistrationHelper::closeCurrentScope()
void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaration, bool _opensScope)
{
solAssert(m_currentScope && m_scopes.count(m_currentScope), "No current scope.");
- if (!m_scopes[m_currentScope]->registerDeclaration(_declaration, nullptr, !_declaration.isVisibleInContract()))
- {
- SourceLocation firstDeclarationLocation;
- SourceLocation secondDeclarationLocation;
- Declaration const* conflictingDeclaration = m_scopes[m_currentScope]->conflictingDeclaration(_declaration);
- solAssert(conflictingDeclaration, "");
- if (_declaration.location().start < conflictingDeclaration->location().start)
- {
- firstDeclarationLocation = _declaration.location();
- secondDeclarationLocation = conflictingDeclaration->location();
- }
- else
- {
- firstDeclarationLocation = conflictingDeclaration->location();
- secondDeclarationLocation = _declaration.location();
- }
+ bool warnAboutShadowing = true;
+ // Do not warn about shadowing for structs and enums because their members are
+ // not accessible without prefixes.
+ if (
+ dynamic_cast<StructDefinition const*>(m_currentScope) ||
+ dynamic_cast<EnumDefinition const*>(m_currentScope)
+ )
+ warnAboutShadowing = false;
+ // Do not warn about the constructor shadowing the contract.
+ if (auto fun = dynamic_cast<FunctionDefinition const*>(&_declaration))
+ if (fun->isConstructor())
+ warnAboutShadowing = false;
- m_errorReporter.declarationError(
- secondDeclarationLocation,
- SecondarySourceLocation().append("The previous declaration is here:", firstDeclarationLocation),
- "Identifier already declared."
- );
- }
+ registerDeclaration(*m_scopes[m_currentScope], _declaration, nullptr, nullptr, warnAboutShadowing, m_errorReporter);
_declaration.setScope(m_currentScope);
if (_opensScope)
diff --git a/libsolidity/analysis/NameAndTypeResolver.h b/libsolidity/analysis/NameAndTypeResolver.h
index 84628778..a498c7ba 100644
--- a/libsolidity/analysis/NameAndTypeResolver.h
+++ b/libsolidity/analysis/NameAndTypeResolver.h
@@ -136,6 +136,15 @@ public:
ASTNode const* _currentScope = nullptr
);
+ static bool registerDeclaration(
+ DeclarationContainer& _container,
+ Declaration const& _declaration,
+ std::string const* _name,
+ SourceLocation const* _errorLocation,
+ bool _warnOnShadow,
+ ErrorReporter& _errorReporter
+ );
+
private:
bool visit(SourceUnit& _sourceUnit) override;
void endVisit(SourceUnit& _sourceUnit) override;
diff --git a/libsolidity/analysis/StaticAnalyzer.cpp b/libsolidity/analysis/StaticAnalyzer.cpp
index b1b31163..46477e1e 100644
--- a/libsolidity/analysis/StaticAnalyzer.cpp
+++ b/libsolidity/analysis/StaticAnalyzer.cpp
@@ -38,12 +38,14 @@ bool StaticAnalyzer::analyze(SourceUnit const& _sourceUnit)
bool StaticAnalyzer::visit(ContractDefinition const& _contract)
{
m_library = _contract.isLibrary();
+ m_currentContract = &_contract;
return true;
}
void StaticAnalyzer::endVisit(ContractDefinition const&)
{
m_library = false;
+ m_currentContract = nullptr;
}
bool StaticAnalyzer::visit(FunctionDefinition const& _function)
@@ -54,6 +56,7 @@ bool StaticAnalyzer::visit(FunctionDefinition const& _function)
solAssert(!m_currentFunction, "");
solAssert(m_localVarUseCount.empty(), "");
m_nonPayablePublic = _function.isPublic() && !_function.isPayable();
+ m_constructor = _function.isConstructor();
return true;
}
@@ -61,6 +64,7 @@ void StaticAnalyzer::endVisit(FunctionDefinition const&)
{
m_currentFunction = nullptr;
m_nonPayablePublic = false;
+ m_constructor = false;
for (auto const& var: m_localVarUseCount)
if (var.second == 0)
m_errorReporter.warning(var.first->location(), "Unused local variable");
@@ -131,6 +135,11 @@ bool StaticAnalyzer::visit(MemberAccess const& _memberAccess)
"\"callcode\" has been deprecated in favour of \"delegatecall\"."
);
+ if (m_constructor && m_currentContract)
+ if (ContractType const* type = dynamic_cast<ContractType const*>(_memberAccess.expression().annotation().type.get()))
+ if (type->contractDefinition() == *m_currentContract)
+ m_errorReporter.warning(_memberAccess.location(), "\"this\" used in constructor.");
+
return true;
}
diff --git a/libsolidity/analysis/StaticAnalyzer.h b/libsolidity/analysis/StaticAnalyzer.h
index cd6913b5..21a487df 100644
--- a/libsolidity/analysis/StaticAnalyzer.h
+++ b/libsolidity/analysis/StaticAnalyzer.h
@@ -77,6 +77,12 @@ private:
std::map<VariableDeclaration const*, int> m_localVarUseCount;
FunctionDefinition const* m_currentFunction = nullptr;
+
+ /// Flag that indicates a constructor.
+ bool m_constructor = false;
+
+ /// Current contract.
+ ContractDefinition const* m_currentContract = nullptr;
};
}
diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp
index 76bfb1a8..bcfccc3e 100644
--- a/libsolidity/ast/Types.cpp
+++ b/libsolidity/ast/Types.cpp
@@ -196,9 +196,9 @@ TypePointer Type::fromElementaryTypeName(ElementaryTypeNameToken const& _type)
case Token::UInt:
return make_shared<IntegerType>(256, IntegerType::Modifier::Unsigned);
case Token::Fixed:
- return make_shared<FixedPointType>(128, 128, FixedPointType::Modifier::Signed);
+ return make_shared<FixedPointType>(128, 19, FixedPointType::Modifier::Signed);
case Token::UFixed:
- return make_shared<FixedPointType>(128, 128, FixedPointType::Modifier::Unsigned);
+ return make_shared<FixedPointType>(128, 19, FixedPointType::Modifier::Unsigned);
case Token::Byte:
return make_shared<FixedBytesType>(1);
case Token::Address:
@@ -352,12 +352,11 @@ bool IntegerType::isImplicitlyConvertibleTo(Type const& _convertTo) const
else if (_convertTo.category() == Category::FixedPoint)
{
FixedPointType const& convertTo = dynamic_cast<FixedPointType const&>(_convertTo);
- if (convertTo.integerBits() < m_bits || isAddress())
+
+ if (isAddress())
return false;
- else if (isSigned())
- return convertTo.isSigned();
else
- return !convertTo.isSigned() || convertTo.integerBits() > m_bits;
+ return maxValue() <= convertTo.maxIntegerValue() && minValue() >= convertTo.minIntegerValue();
}
else
return false;
@@ -487,22 +486,20 @@ MemberList::MemberMap IntegerType::nativeMembers(ContractDefinition const*) cons
return MemberList::MemberMap();
}
-FixedPointType::FixedPointType(int _integerBits, int _fractionalBits, FixedPointType::Modifier _modifier):
- m_integerBits(_integerBits), m_fractionalBits(_fractionalBits), m_modifier(_modifier)
+FixedPointType::FixedPointType(int _totalBits, int _fractionalDigits, FixedPointType::Modifier _modifier):
+ m_totalBits(_totalBits), m_fractionalDigits(_fractionalDigits), m_modifier(_modifier)
{
solAssert(
- m_integerBits + m_fractionalBits > 0 &&
- m_integerBits + m_fractionalBits <= 256 &&
- m_integerBits % 8 == 0 &&
- m_fractionalBits % 8 == 0,
+ 8 <= m_totalBits && m_totalBits <= 256 && m_totalBits % 8 == 0 &&
+ 0 <= m_fractionalDigits && m_fractionalDigits <= 80,
"Invalid bit number(s) for fixed type: " +
- dev::toString(_integerBits) + "x" + dev::toString(_fractionalBits)
- );
+ dev::toString(_totalBits) + "x" + dev::toString(_fractionalDigits)
+ );
}
string FixedPointType::identifier() const
{
- return "t_" + string(isSigned() ? "" : "u") + "fixed" + std::to_string(integerBits()) + "x" + std::to_string(fractionalBits());
+ return "t_" + string(isSigned() ? "" : "u") + "fixed" + std::to_string(m_totalBits) + "x" + std::to_string(m_fractionalDigits);
}
bool FixedPointType::isImplicitlyConvertibleTo(Type const& _convertTo) const
@@ -510,12 +507,10 @@ bool FixedPointType::isImplicitlyConvertibleTo(Type const& _convertTo) const
if (_convertTo.category() == category())
{
FixedPointType const& convertTo = dynamic_cast<FixedPointType const&>(_convertTo);
- if (convertTo.m_integerBits < m_integerBits || convertTo.m_fractionalBits < m_fractionalBits)
+ if (convertTo.numBits() < m_totalBits || convertTo.fractionalDigits() < m_fractionalDigits)
return false;
- else if (isSigned())
- return convertTo.isSigned();
else
- return !convertTo.isSigned() || (convertTo.m_integerBits > m_integerBits);
+ return convertTo.maxIntegerValue() >= maxIntegerValue() && convertTo.minIntegerValue() <= minIntegerValue();
}
return false;
}
@@ -549,13 +544,30 @@ bool FixedPointType::operator==(Type const& _other) const
if (_other.category() != category())
return false;
FixedPointType const& other = dynamic_cast<FixedPointType const&>(_other);
- return other.m_integerBits == m_integerBits && other.m_fractionalBits == m_fractionalBits && other.m_modifier == m_modifier;
+ return other.m_totalBits == m_totalBits && other.m_fractionalDigits == m_fractionalDigits && other.m_modifier == m_modifier;
}
string FixedPointType::toString(bool) const
{
string prefix = isSigned() ? "fixed" : "ufixed";
- return prefix + dev::toString(m_integerBits) + "x" + dev::toString(m_fractionalBits);
+ return prefix + dev::toString(m_totalBits) + "x" + dev::toString(m_fractionalDigits);
+}
+
+bigint FixedPointType::maxIntegerValue() const
+{
+ bigint maxValue = (bigint(1) << (m_totalBits - (isSigned() ? 1 : 0))) - 1;
+ return maxValue / pow(bigint(10), m_fractionalDigits);
+}
+
+bigint FixedPointType::minIntegerValue() const
+{
+ if (isSigned())
+ {
+ bigint minValue = -(bigint(1) << (m_totalBits - (isSigned() ? 1 : 0)));
+ return minValue / pow(bigint(10), m_fractionalDigits);
+ }
+ else
+ return bigint(0);
}
TypePointer FixedPointType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const
@@ -743,13 +755,9 @@ bool RationalNumberType::isImplicitlyConvertibleTo(Type const& _convertTo) const
else if (_convertTo.category() == Category::FixedPoint)
{
if (auto fixed = fixedPointType())
- {
- // We disallow implicit conversion if we would have to truncate (fixedPointType()
- // can return a type that requires truncation).
- rational value = m_value * (bigint(1) << fixed->fractionalBits());
- return value.denominator() == 1 && fixed->isImplicitlyConvertibleTo(_convertTo);
- }
- return false;
+ return fixed->isImplicitlyConvertibleTo(_convertTo);
+ else
+ return false;
}
else if (_convertTo.category() == Category::FixedBytes)
{
@@ -953,10 +961,9 @@ u256 RationalNumberType::literalValue(Literal const*) const
else
{
auto fixed = fixedPointType();
- solAssert(!!fixed, "");
- rational shifted = m_value * (bigint(1) << fixed->fractionalBits());
- // truncate
- shiftedValue = shifted.numerator() / shifted.denominator();
+ solAssert(fixed, "");
+ int fractionalDigits = fixed->fractionalDigits();
+ shiftedValue = (m_value.numerator() / m_value.denominator()) * pow(bigint(10), fractionalDigits);
}
// we ignore the literal and hope that the type was correctly determined
@@ -997,22 +1004,21 @@ shared_ptr<IntegerType const> RationalNumberType::integerType() const
shared_ptr<FixedPointType const> RationalNumberType::fixedPointType() const
{
bool negative = (m_value < 0);
- unsigned fractionalBits = 0;
+ unsigned fractionalDigits = 0;
rational value = abs(m_value); // We care about the sign later.
rational maxValue = negative ?
rational(bigint(1) << 255, 1):
rational((bigint(1) << 256) - 1, 1);
- while (value * 0x100 <= maxValue && value.denominator() != 1 && fractionalBits < 256)
+ while (value * 10 <= maxValue && value.denominator() != 1 && fractionalDigits < 80)
{
- value *= 0x100;
- fractionalBits += 8;
+ value *= 10;
+ fractionalDigits++;
}
if (value > maxValue)
return shared_ptr<FixedPointType const>();
- // u256(v) is the actual value that will be put on the stack
- // From here on, very similar to integerType()
+ // This means we round towards zero for positive and negative values.
bigint v = value.numerator() / value.denominator();
if (negative)
// modify value to satisfy bit requirements for negative numbers:
@@ -1022,26 +1028,11 @@ shared_ptr<FixedPointType const> RationalNumberType::fixedPointType() const
if (v > u256(-1))
return shared_ptr<FixedPointType const>();
- unsigned totalBits = bytesRequired(v) * 8;
+ unsigned totalBits = max(bytesRequired(v), 1u) * 8;
solAssert(totalBits <= 256, "");
- unsigned integerBits = totalBits >= fractionalBits ? totalBits - fractionalBits : 0;
- // Special case: Numbers between -1 and 0 have their sign bit in the fractional part.
- if (negative && abs(m_value) < 1 && totalBits > fractionalBits)
- {
- fractionalBits += 8;
- integerBits = 0;
- }
-
- if (integerBits > 256 || fractionalBits > 256 || fractionalBits + integerBits > 256)
- return shared_ptr<FixedPointType const>();
- if (integerBits == 0 && fractionalBits == 0)
- {
- integerBits = 0;
- fractionalBits = 8;
- }
return make_shared<FixedPointType>(
- integerBits, fractionalBits,
+ totalBits, fractionalDigits,
negative ? FixedPointType::Modifier::Signed : FixedPointType::Modifier::Unsigned
);
}
diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h
index c24cc11a..3d7dad16 100644
--- a/libsolidity/ast/Types.h
+++ b/libsolidity/ast/Types.h
@@ -342,7 +342,7 @@ public:
};
virtual Category category() const override { return Category::FixedPoint; }
- explicit FixedPointType(int _integerBits, int _fractionalBits, Modifier _modifier = Modifier::Unsigned);
+ explicit FixedPointType(int _totalBits, int _fractionalDigits, Modifier _modifier = Modifier::Unsigned);
virtual std::string identifier() const override;
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
@@ -352,8 +352,8 @@ public:
virtual bool operator==(Type const& _other) const override;
- virtual unsigned calldataEncodedSize(bool _padded = true) const override { return _padded ? 32 : (m_integerBits + m_fractionalBits) / 8; }
- virtual unsigned storageBytes() const override { return (m_integerBits + m_fractionalBits) / 8; }
+ virtual unsigned calldataEncodedSize(bool _padded = true) const override { return _padded ? 32 : m_totalBits / 8; }
+ virtual unsigned storageBytes() const override { return m_totalBits / 8; }
virtual bool isValueType() const override { return true; }
virtual std::string toString(bool _short) const override;
@@ -361,14 +361,21 @@ public:
virtual TypePointer encodingType() const override { return shared_from_this(); }
virtual TypePointer interfaceType(bool) const override { return shared_from_this(); }
- int numBits() const { return m_integerBits + m_fractionalBits; }
- int integerBits() const { return m_integerBits; }
- int fractionalBits() const { return m_fractionalBits; }
+ /// Number of bits used for this type in total.
+ int numBits() const { return m_totalBits; }
+ /// Number of decimal digits after the radix point.
+ int fractionalDigits() const { return m_fractionalDigits; }
bool isSigned() const { return m_modifier == Modifier::Signed; }
+ /// @returns the largest integer value this type con hold. Note that this is not the
+ /// largest value in general.
+ bigint maxIntegerValue() const;
+ /// @returns the smallest integer value this type can hold. Note hat this is not the
+ /// smallest value in general.
+ bigint minIntegerValue() const;
private:
- int m_integerBits;
- int m_fractionalBits;
+ int m_totalBits;
+ int m_fractionalDigits;
Modifier m_modifier;
};
diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp
index 7067ddd5..782aad9d 100644
--- a/libsolidity/codegen/CompilerUtils.cpp
+++ b/libsolidity/codegen/CompilerUtils.cpp
@@ -504,7 +504,7 @@ void CompilerUtils::convertType(
//shift all integer bits onto the left side of the fixed type
FixedPointType const& targetFixedPointType = dynamic_cast<FixedPointType const&>(_targetType);
if (auto typeOnStack = dynamic_cast<IntegerType const*>(&_typeOnStack))
- if (targetFixedPointType.integerBits() > typeOnStack->numBits())
+ if (targetFixedPointType.numBits() > typeOnStack->numBits())
cleanHigherOrderBits(*typeOnStack);
solUnimplemented("Not yet implemented - FixedPointType.");
}
diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp
index 9b630c31..f06dd4d7 100644
--- a/libsolidity/interface/CompilerStack.cpp
+++ b/libsolidity/interface/CompilerStack.cpp
@@ -87,6 +87,7 @@ void CompilerStack::reset(bool _keepSources)
m_stackState = Empty;
m_sources.clear();
}
+ m_libraries.clear();
m_optimize = false;
m_optimizeRuns = 200;
m_globalContext.reset();
@@ -106,12 +107,6 @@ bool CompilerStack::addSource(string const& _name, string const& _content, bool
return existed;
}
-void CompilerStack::setSource(string const& _sourceCode)
-{
- reset();
- addSource("", _sourceCode);
-}
-
bool CompilerStack::parse()
{
//reset
@@ -252,23 +247,11 @@ bool CompilerStack::analyze()
return false;
}
-bool CompilerStack::parse(string const& _sourceCode)
-{
- setSource(_sourceCode);
- return parse();
-}
-
bool CompilerStack::parseAndAnalyze()
{
return parse() && analyze();
}
-bool CompilerStack::parseAndAnalyze(std::string const& _sourceCode)
-{
- setSource(_sourceCode);
- return parseAndAnalyze();
-}
-
vector<string> CompilerStack::contractNames() const
{
if (m_stackState < AnalysisSuccessful)
@@ -279,17 +262,12 @@ vector<string> CompilerStack::contractNames() const
return contractNames;
}
-
-bool CompilerStack::compile(bool _optimize, unsigned _runs, map<string, h160> const& _libraries)
+bool CompilerStack::compile()
{
if (m_stackState < AnalysisSuccessful)
if (!parseAndAnalyze())
return false;
- m_optimize = _optimize;
- m_optimizeRuns = _runs;
- m_libraries = _libraries;
-
map<ContractDefinition const*, eth::Assembly const*> compiledContracts;
for (Source const* source: m_sourceOrder)
for (ASTPointer<ASTNode> const& node: source->ast->nodes())
@@ -300,11 +278,6 @@ bool CompilerStack::compile(bool _optimize, unsigned _runs, map<string, h160> co
return true;
}
-bool CompilerStack::compile(string const& _sourceCode, bool _optimize, unsigned _runs)
-{
- return parseAndAnalyze(_sourceCode) && compile(_optimize, _runs);
-}
-
void CompilerStack::link()
{
for (auto& contract: m_contracts)
diff --git a/libsolidity/interface/CompilerStack.h b/libsolidity/interface/CompilerStack.h
index 6b8fb538..356389db 100644
--- a/libsolidity/interface/CompilerStack.h
+++ b/libsolidity/interface/CompilerStack.h
@@ -96,46 +96,45 @@ public:
/// Sets path remappings in the format "context:prefix=target"
void setRemappings(std::vector<std::string> const& _remappings);
+ /// Sets library addresses. Addresses are cleared iff @a _libraries is missing.
+ /// Will not take effect before running compile.
+ void setLibraries(std::map<std::string, h160> const& _libraries = std::map<std::string, h160>{})
+ {
+ m_libraries = _libraries;
+ }
+
+ /// Changes the optimiser settings.
+ /// Will not take effect before running compile.
+ void setOptimiserSettings(bool _optimize, unsigned _runs = 200)
+ {
+ m_optimize = _optimize;
+ m_optimizeRuns = _runs;
+ }
+
/// Resets the compiler to a state where the sources are not parsed or even removed.
+ /// Sets the state to SourcesSet if @a _keepSources is true, otherwise to Empty.
+ /// All settings, with the exception of remappings, are reset.
void reset(bool _keepSources = false);
/// Adds a source object (e.g. file) to the parser. After this, parse has to be called again.
/// @returns true if a source object by the name already existed and was replaced.
- void addSources(StringMap const& _nameContents, bool _isLibrary = false)
- {
- for (auto const& i: _nameContents) addSource(i.first, i.second, _isLibrary);
- }
bool addSource(std::string const& _name, std::string const& _content, bool _isLibrary = false);
- void setSource(std::string const& _sourceCode);
/// Parses all source units that were added
/// @returns false on error.
bool parse();
- /// Sets the given source code as the only source unit apart from standard sources and parses it.
- /// @returns false on error.
- bool parse(std::string const& _sourceCode);
- /// performs the analyisis steps (imports, scopesetting, syntaxCheck, referenceResolving,
+ /// Performs the analysis steps (imports, scopesetting, syntaxCheck, referenceResolving,
/// typechecking, staticAnalysis) on previously set sources
/// @returns false on error.
bool analyze();
/// Parses and analyzes all source units that were added
/// @returns false on error.
bool parseAndAnalyze();
- /// Sets the given source code as the only source unit apart from standard sources and parses and analyzes it.
- /// @returns false on error.
- bool parseAndAnalyze(std::string const& _sourceCode);
/// @returns a list of the contract names in the sources.
std::vector<std::string> contractNames() const;
/// Compiles the source units that were previously added and parsed.
/// @returns false on error.
- bool compile(
- bool _optimize = false,
- unsigned _runs = 200,
- std::map<std::string, h160> const& _libraries = std::map<std::string, h160>{}
- );
- /// Parses and compiles the given source code.
- /// @returns false on error.
- bool compile(std::string const& _sourceCode, bool _optimize = false, unsigned _runs = 200);
+ bool compile();
/// @returns the assembled object for a contract.
eth::LinkerObject const& object(std::string const& _contractName = "") const;
@@ -202,6 +201,7 @@ public:
/// @returns the list of errors that occured during parsing and type checking.
ErrorList const& errors() { return m_errorReporter.errors(); }
+ /// @returns the current state.
State state() const { return m_stackState; }
private:
diff --git a/libsolidity/interface/ErrorReporter.cpp b/libsolidity/interface/ErrorReporter.cpp
index 6e2667a5..f9ef4ceb 100644
--- a/libsolidity/interface/ErrorReporter.cpp
+++ b/libsolidity/interface/ErrorReporter.cpp
@@ -42,11 +42,23 @@ void ErrorReporter::warning(string const& _description)
error(Error::Type::Warning, SourceLocation(), _description);
}
-void ErrorReporter::warning(SourceLocation const& _location, string const& _description)
+void ErrorReporter::warning(
+ SourceLocation const& _location,
+ string const& _description
+)
{
error(Error::Type::Warning, _location, _description);
}
+void ErrorReporter::warning(
+ SourceLocation const& _location,
+ string const& _description,
+ SecondarySourceLocation const& _secondaryLocation
+)
+{
+ error(Error::Type::Warning, _location, _secondaryLocation, _description);
+}
+
void ErrorReporter::error(Error::Type _type, SourceLocation const& _location, string const& _description)
{
auto err = make_shared<Error>(_type);
diff --git a/libsolidity/interface/ErrorReporter.h b/libsolidity/interface/ErrorReporter.h
index e5605d24..8b066a3e 100644
--- a/libsolidity/interface/ErrorReporter.h
+++ b/libsolidity/interface/ErrorReporter.h
@@ -41,30 +41,30 @@ public:
ErrorReporter& operator=(ErrorReporter const& _errorReporter);
- void warning(std::string const& _description = std::string());
+ void warning(std::string const& _description);
+
+ void warning(SourceLocation const& _location, std::string const& _description);
void warning(
- SourceLocation const& _location = SourceLocation(),
- std::string const& _description = std::string()
+ SourceLocation const& _location,
+ std::string const& _description,
+ SecondarySourceLocation const& _secondaryLocation
);
void error(
Error::Type _type,
- SourceLocation const& _location = SourceLocation(),
- std::string const& _description = std::string()
- );
-
- void declarationError(
SourceLocation const& _location,
- SecondarySourceLocation const& _secondaryLocation = SecondarySourceLocation(),
- std::string const& _description = std::string()
+ std::string const& _description
);
void declarationError(
SourceLocation const& _location,
- std::string const& _description = std::string()
+ SecondarySourceLocation const& _secondaryLocation,
+ std::string const& _description
);
+ void declarationError(SourceLocation const& _location, std::string const& _description);
+
void fatalDeclarationError(SourceLocation const& _location, std::string const& _description);
void parserError(SourceLocation const& _location, std::string const& _description);
diff --git a/libsolidity/interface/StandardCompiler.cpp b/libsolidity/interface/StandardCompiler.cpp
index 82eeac3d..23687340 100644
--- a/libsolidity/interface/StandardCompiler.cpp
+++ b/libsolidity/interface/StandardCompiler.cpp
@@ -249,6 +249,7 @@ Json::Value StandardCompiler::compileInternal(Json::Value const& _input)
Json::Value optimizerSettings = settings.get("optimizer", Json::Value());
bool const optimize = optimizerSettings.get("enabled", Json::Value(false)).asBool();
unsigned const optimizeRuns = optimizerSettings.get("runs", Json::Value(200u)).asUInt();
+ m_compilerStack.setOptimiserSettings(optimize, optimizeRuns);
map<string, h160> libraries;
Json::Value jsonLibraries = settings.get("libraries", Json::Value());
@@ -259,6 +260,7 @@ Json::Value StandardCompiler::compileInternal(Json::Value const& _input)
// @TODO use libraries only for the given source
libraries[library] = h160(jsonSourceName[library].asString());
}
+ m_compilerStack.setLibraries(libraries);
Json::Value metadataSettings = settings.get("metadata", Json::Value());
m_compilerStack.useMetadataLiteralSources(metadataSettings.get("useLiteralContent", Json::Value(false)).asBool());
@@ -267,7 +269,7 @@ Json::Value StandardCompiler::compileInternal(Json::Value const& _input)
try
{
- m_compilerStack.compile(optimize, optimizeRuns, libraries);
+ m_compilerStack.compile();
for (auto const& error: m_compilerStack.errors())
{
diff --git a/libsolidity/parsing/Token.cpp b/libsolidity/parsing/Token.cpp
index 66312f69..9cec0303 100644
--- a/libsolidity/parsing/Token.cpp
+++ b/libsolidity/parsing/Token.cpp
@@ -70,7 +70,7 @@ void ElementaryTypeNameToken::assertDetails(Token::Value _baseType, unsigned con
else if (_baseType == Token::UFixedMxN || _baseType == Token::FixedMxN)
{
solAssert(
- _first + _second <= 256 && _first % 8 == 0 && _second % 8 == 0,
+ _first >= 8 && _first <= 256 && _first % 8 == 0 && _second <= 80,
"No elementary type " + string(Token::toString(_baseType)) + to_string(_first) + "x" + to_string(_second) + "."
);
}
@@ -157,12 +157,8 @@ tuple<Token::Value, unsigned int, unsigned int> Token::fromIdentifierOrKeyword(s
) {
int n = parseSize(positionX + 1, _literal.end());
if (
- 0 <= m && m <= 256 &&
- 8 <= n && n <= 256 &&
- m + n > 0 &&
- m + n <= 256 &&
- m % 8 == 0 &&
- n % 8 == 0
+ 8 <= m && m <= 256 && m % 8 == 0 &&
+ 0 <= n && n <= 80
) {
if (keyword == Token::UFixed)
return make_tuple(Token::UFixedMxN, m, n);