Skip to content

Commit

Permalink
Mark a lot of functions const (where possible)
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Aug 22, 2017
1 parent c94b1f8 commit e2cfc9e
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 41 deletions.
10 changes: 5 additions & 5 deletions libevmasm/ConstantOptimiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void ConstantOptimisationMethod::replaceConstants(
_items = std::move(replaced);
}

bigint LiteralMethod::gasNeeded()
bigint LiteralMethod::gasNeeded() const
{
return combineGas(
simpleRunGas({Instruction::PUSH1}),
Expand All @@ -139,7 +139,7 @@ CodeCopyMethod::CodeCopyMethod(Params const& _params, u256 const& _value):
{
}

bigint CodeCopyMethod::gasNeeded()
bigint CodeCopyMethod::gasNeeded() const
{
return combineGas(
// Run gas: we ignore memory increase costs
Expand All @@ -151,7 +151,7 @@ bigint CodeCopyMethod::gasNeeded()
);
}

AssemblyItems CodeCopyMethod::execute(Assembly& _assembly)
AssemblyItems CodeCopyMethod::execute(Assembly& _assembly) const
{
bytes data = toBigEndian(m_value);
AssemblyItems actualCopyRoutine = copyRoutine();
Expand Down Expand Up @@ -234,7 +234,7 @@ AssemblyItems ComputeMethod::findRepresentation(u256 const& _value)
}
}

bool ComputeMethod::checkRepresentation(u256 const& _value, AssemblyItems const& _routine)
bool ComputeMethod::checkRepresentation(u256 const& _value, AssemblyItems const& _routine) const
{
// This is a tiny EVM that can only evaluate some instructions.
vector<u256> stack;
Expand Down Expand Up @@ -282,7 +282,7 @@ bool ComputeMethod::checkRepresentation(u256 const& _value, AssemblyItems const&
return stack.size() == 1 && stack.front() == _value;
}

bigint ComputeMethod::gasNeeded(AssemblyItems const& _routine)
bigint ComputeMethod::gasNeeded(AssemblyItems const& _routine) const
{
size_t numExps = count(_routine.begin(), _routine.end(), Instruction::EXP);
return combineGas(
Expand Down
22 changes: 11 additions & 11 deletions libevmasm/ConstantOptimiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ class ConstantOptimisationMethod

explicit ConstantOptimisationMethod(Params const& _params, u256 const& _value):
m_params(_params), m_value(_value) {}
virtual bigint gasNeeded() = 0;
virtual bigint gasNeeded() const = 0;
/// Executes the method, potentially appending to the assembly and returns a vector of
/// assembly items the constant should be relpaced with in one sweep.
/// If the vector is empty, the constants will not be deleted.
virtual AssemblyItems execute(Assembly& _assembly) = 0;
virtual AssemblyItems execute(Assembly& _assembly) const = 0;

protected:
size_t dataSize() const { return std::max<size_t>(1, dev::bytesRequired(m_value)); }
Expand All @@ -84,7 +84,7 @@ class ConstantOptimisationMethod
bigint const& _runGas,
bigint const& _repeatedDataGas,
bigint const& _uniqueDataGas
)
) const
{
// _runGas is not multiplied by _multiplicity because the runs are "per opcode"
return m_params.runs * _runGas + m_params.multiplicity * _repeatedDataGas + _uniqueDataGas;
Expand All @@ -106,8 +106,8 @@ class LiteralMethod: public ConstantOptimisationMethod
public:
explicit LiteralMethod(Params const& _params, u256 const& _value):
ConstantOptimisationMethod(_params, _value) {}
virtual bigint gasNeeded() override;
virtual AssemblyItems execute(Assembly&) override { return AssemblyItems{}; }
virtual bigint gasNeeded() const override;
virtual AssemblyItems execute(Assembly&) const override { return AssemblyItems{}; }
};

/**
Expand All @@ -117,8 +117,8 @@ class CodeCopyMethod: public ConstantOptimisationMethod
{
public:
explicit CodeCopyMethod(Params const& _params, u256 const& _value);
virtual bigint gasNeeded() override;
virtual AssemblyItems execute(Assembly& _assembly) override;
virtual bigint gasNeeded() const override;
virtual AssemblyItems execute(Assembly& _assembly) const override;

protected:
AssemblyItems const& copyRoutine() const;
Expand All @@ -141,8 +141,8 @@ class ComputeMethod: public ConstantOptimisationMethod
);
}

virtual bigint gasNeeded() override { return gasNeeded(m_routine); }
virtual AssemblyItems execute(Assembly&) override
virtual bigint gasNeeded() const override { return gasNeeded(m_routine); }
virtual AssemblyItems execute(Assembly&) const override
{
return m_routine;
}
Expand All @@ -151,8 +151,8 @@ class ComputeMethod: public ConstantOptimisationMethod
/// Tries to recursively find a way to compute @a _value.
AssemblyItems findRepresentation(u256 const& _value);
/// Recomputes the value from the calculated representation and checks for correctness.
bool checkRepresentation(u256 const& _value, AssemblyItems const& _routine);
bigint gasNeeded(AssemblyItems const& _routine);
bool checkRepresentation(u256 const& _value, AssemblyItems const& _routine) const;
bigint gasNeeded(AssemblyItems const& _routine) const;

/// Counter for the complexity of optimization, will stop when it reaches zero.
size_t m_maxSteps = 10000;
Expand Down
2 changes: 1 addition & 1 deletion liblll/CompilerState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CompilerState::CompilerState()
{
}

CodeFragment const& CompilerState::getDef(std::string const& _s)
CodeFragment const& CompilerState::getDef(std::string const& _s) const
{
if (defs.count(_s))
return defs.at(_s);
Expand Down
2 changes: 1 addition & 1 deletion liblll/CompilerState.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct CompilerState
{
CompilerState();

CodeFragment const& getDef(std::string const& _s);
CodeFragment const& getDef(std::string const& _s) const;
void populateStandard();

unsigned stackSize = 128;
Expand Down
6 changes: 3 additions & 3 deletions libsolidity/ast/ASTJsonConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ASTJsonConverter: public ASTConstVisitor
);
std::string sourceLocationToString(SourceLocation const& _location) const;
std::string namePathToString(std::vector<ASTString> const& _namePath) const;
Json::Value idOrNull(ASTNode const* _pt)
Json::Value idOrNull(ASTNode const* _pt) const
{
return _pt ? Json::Value(nodeId(*_pt)) : Json::nullValue;
}
Expand All @@ -134,12 +134,12 @@ class ASTJsonConverter: public ASTConstVisitor
std::string literalTokenKind(Token::Value _token);
std::string type(Expression const& _expression);
std::string type(VariableDeclaration const& _varDecl);
int nodeId(ASTNode const& _node)
int nodeId(ASTNode const& _node) const
{
return _node.id();
}
template<class Container>
Json::Value getContainerIds(Container const& container)
Json::Value getContainerIds(Container const& container) const
{
Json::Value tmp(Json::arrayValue);
for (auto const& element: container)
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/codegen/ABIFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ string ABIFunctions::createFunction(string const& _name, function<string ()> con
return _name;
}

size_t ABIFunctions::headSize(TypePointers const& _targetTypes)
size_t ABIFunctions::headSize(TypePointers const& _targetTypes) const
{
size_t headSize = 0;
for (auto const& t: _targetTypes)
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/codegen/ABIFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class ABIFunctions
std::string createFunction(std::string const& _name, std::function<std::string()> const& _creator);

/// @returns the size of the static part of the encoding of the given types.
size_t headSize(TypePointers const& _targetTypes);
size_t headSize(TypePointers const& _targetTypes) const;

/// Map from function name to code for a multi-use function.
std::map<std::string, std::string> m_requestedFunctions;
Expand Down
6 changes: 3 additions & 3 deletions libsolidity/codegen/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class Compiler
ContractDefinition const& _contract,
std::map<ContractDefinition const*, eth::Assembly const*> const& _contracts
);
eth::Assembly const& assembly() { return m_context.assembly(); }
eth::LinkerObject assembledObject() { return m_context.assembledObject(); }
eth::LinkerObject runtimeObject() { return m_context.assembledRuntimeObject(m_runtimeSub); }
eth::Assembly const& assembly() const { return m_context.assembly(); }
eth::LinkerObject assembledObject() const { return m_context.assembledObject(); }
eth::LinkerObject runtimeObject() const { return m_context.assembledRuntimeObject(m_runtimeSub); }
/// @arg _sourceCodes is the map of input files to source code strings
/// @arg _inJsonFromat shows whether the out should be in Json format
Json::Value streamAssembly(std::ostream& _stream, StringMap const& _sourceCodes = StringMap(), bool _inJsonFormat = false) const
Expand Down
4 changes: 2 additions & 2 deletions libsolidity/codegen/CompilerContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ class CompilerContext
return m_asm->stream(_stream, "", _sourceCodes, _inJsonFormat);
}

eth::LinkerObject const& assembledObject() { return m_asm->assemble(); }
eth::LinkerObject const& assembledRuntimeObject(size_t _subIndex) { return m_asm->sub(_subIndex).assemble(); }
eth::LinkerObject const& assembledObject() const { return m_asm->assemble(); }
eth::LinkerObject const& assembledRuntimeObject(size_t _subIndex) const { return m_asm->sub(_subIndex).assemble(); }

/**
* Helper class to pop the visited nodes stack when a scope closes
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/codegen/ExpressionCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1811,7 +1811,7 @@ void ExpressionCompiler::setLValueToStorageItem(Expression const& _expression)
setLValue<StorageItem>(_expression, *_expression.annotation().type);
}

bool ExpressionCompiler::cleanupNeededForOp(Type::Category _type, Token::Value _op)
bool ExpressionCompiler::cleanupNeededForOp(Type::Category _type, Token::Value _op) const
{
if (Token::isCompareOp(_op) || Token::isShiftOp(_op))
return true;
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/codegen/ExpressionCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ExpressionCompiler: private ASTConstVisitor

/// @returns true if the operator applied to the given type requires a cleanup prior to the
/// operation.
bool cleanupNeededForOp(Type::Category _type, Token::Value _op);
bool cleanupNeededForOp(Type::Category _type, Token::Value _op) const;

/// @returns the CompilerUtils object containing the current context.
CompilerUtils utils();
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/inlineasm/AsmParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Parser: public ParserBase

protected:
/// Creates an inline assembly node with the given source location.
template <class T> T createWithLocation(SourceLocation const& _loc = SourceLocation())
template <class T> T createWithLocation(SourceLocation const& _loc = SourceLocation()) const
{
T r;
r.location = _loc;
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/inlineasm/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ string AsmPrinter::operator()(Block const& _block)
return "{\n " + body + "\n}";
}

string AsmPrinter::appendTypeName(std::string const& _type)
string AsmPrinter::appendTypeName(std::string const& _type) const
{
if (m_julia)
return ":" + _type;
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/inlineasm/AsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class AsmPrinter: public boost::static_visitor<std::string>
std::string operator()(assembly::Block const& _block);

private:
std::string appendTypeName(std::string const& _type);
std::string appendTypeName(std::string const& _type) const;

bool m_julia = false;
};
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/inlineasm/AsmScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Scope::Identifier* Scope::lookup(string const& _name)
return nullptr;
}

bool Scope::exists(string const& _name)
bool Scope::exists(string const& _name) const
{
if (identifiers.count(_name))
return true;
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/inlineasm/AsmScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ struct Scope
}
/// @returns true if the name exists in this scope or in super scopes (also searches
/// across function and assembly boundaries).
bool exists(std::string const& _name);
bool exists(std::string const& _name) const;

/// @returns the number of variables directly registered inside the scope.
size_t numberOfVariables() const;
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/interface/CompilerStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class CompilerStack: boost::noncopyable
m_errorReporter(m_errorList) {}

/// @returns the list of errors that occured during parsing and type checking.
ErrorList const& errors() { return m_errorReporter.errors(); }
ErrorList const& errors() const { return m_errorReporter.errors(); }

/// @returns the current state.
State state() const { return m_stackState; }
Expand Down
10 changes: 5 additions & 5 deletions libsolidity/parsing/Scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class CharStream
int position() const { return m_position; }
bool isPastEndOfInput(size_t _charsForward = 0) const { return (m_position + _charsForward) >= m_source.size(); }
char get(size_t _charsForward = 0) const { return m_source[m_position + _charsForward]; }
char advanceAndGet(size_t _chars=1);
char advanceAndGet(size_t _chars = 1);
char rollback(size_t _amount);

void reset() { m_position = 0; }
Expand Down Expand Up @@ -118,11 +118,11 @@ class Scanner
///@name Information about the current token

/// @returns the current token
Token::Value currentToken()
Token::Value currentToken() const
{
return m_currentToken.token;
}
ElementaryTypeNameToken currentElementaryTypeNameToken()
ElementaryTypeNameToken currentElementaryTypeNameToken() const
{
unsigned firstSize;
unsigned secondSize;
Expand Down Expand Up @@ -219,8 +219,8 @@ class Scanner
bool scanEscape();

/// Return the current source position.
int sourcePos() { return m_source.position(); }
bool isSourcePastEndOfInput() { return m_source.isPastEndOfInput(); }
int sourcePos() const { return m_source.position(); }
bool isSourcePastEndOfInput() const { return m_source.isPastEndOfInput(); }

TokenDesc m_skippedComment; // desc for current skipped comment
TokenDesc m_nextSkippedComment; // desc for next skiped comment
Expand Down

0 comments on commit e2cfc9e

Please sign in to comment.