Type inference draft.

This commit is contained in:
Daniel Kirchner
2023-09-05 11:50:14 +02:00
committed by Nikola Matic
parent 6ec4d3dd98
commit 45f00dda3b
52 changed files with 5240 additions and 265 deletions
@@ -0,0 +1,159 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/experimental/analysis/Analysis.h>
#include <libsolidity/experimental/analysis/DebugWarner.h>
#include <libsolidity/experimental/analysis/SyntaxRestrictor.h>
#include <libsolidity/experimental/analysis/TypeInference.h>
#include <libsolidity/experimental/analysis/TypeRegistration.h>
using namespace std;
using namespace solidity::langutil;
using namespace solidity::frontend::experimental;
// TODO: creating all of them for all nodes up front may be wasteful, we should improve the mechanism.
struct Analysis::AnnotationContainer
{
TypeRegistration::Annotation typeRegistrationAnnotation;
TypeInference::Annotation typeInferenceAnnotation;
};
struct Analysis::GlobalAnnotationContainer
{
TypeRegistration::GlobalAnnotation typeRegistrationAnnotation;
TypeInference::GlobalAnnotation typeInferenceAnnotation;
};
template<>
TypeRegistration::Annotation& solidity::frontend::experimental::detail::AnnotationFetcher<TypeRegistration>::get(ASTNode const& _node)
{
return analysis.annotationContainer(_node).typeRegistrationAnnotation;
}
template<>
TypeRegistration::GlobalAnnotation const& solidity::frontend::experimental::detail::ConstAnnotationFetcher<TypeRegistration>::get() const
{
return analysis.annotationContainer().typeRegistrationAnnotation;
}
template<>
TypeRegistration::GlobalAnnotation& solidity::frontend::experimental::detail::AnnotationFetcher<TypeRegistration>::get()
{
return analysis.annotationContainer().typeRegistrationAnnotation;
}
template<>
TypeRegistration::Annotation const& solidity::frontend::experimental::detail::ConstAnnotationFetcher<TypeRegistration>::get(ASTNode const& _node) const
{
return analysis.annotationContainer(_node).typeRegistrationAnnotation;
}
template<>
TypeInference::Annotation& solidity::frontend::experimental::detail::AnnotationFetcher<TypeInference>::get(ASTNode const& _node)
{
return analysis.annotationContainer(_node).typeInferenceAnnotation;
}
template<>
TypeInference::Annotation const& solidity::frontend::experimental::detail::ConstAnnotationFetcher<TypeInference>::get(ASTNode const& _node) const
{
return analysis.annotationContainer(_node).typeInferenceAnnotation;
}
template<>
TypeInference::GlobalAnnotation const& solidity::frontend::experimental::detail::ConstAnnotationFetcher<TypeInference>::get() const
{
return analysis.annotationContainer().typeInferenceAnnotation;
}
template<>
TypeInference::GlobalAnnotation& solidity::frontend::experimental::detail::AnnotationFetcher<TypeInference>::get()
{
return analysis.annotationContainer().typeInferenceAnnotation;
}
Analysis::AnnotationContainer& Analysis::annotationContainer(ASTNode const& _node)
{
solAssert(_node.id() > 0);
size_t id = static_cast<size_t>(_node.id());
solAssert(id <= m_maxAstId);
return m_annotations[id];
}
Analysis::AnnotationContainer const& Analysis::annotationContainer(ASTNode const& _node) const
{
solAssert(_node.id() > 0);
size_t id = static_cast<size_t>(_node.id());
solAssert(id <= m_maxAstId);
return m_annotations[id];
}
Analysis::Analysis(langutil::ErrorReporter& _errorReporter, uint64_t _maxAstId):
m_errorReporter(_errorReporter),
m_maxAstId(_maxAstId),
m_annotations(std::make_unique<AnnotationContainer[]>(static_cast<size_t>(_maxAstId + 1))),
m_globalAnnotation(std::make_unique<GlobalAnnotationContainer>())
{
}
Analysis::~Analysis()
{}
template<size_t... Is>
std::tuple<std::integral_constant<size_t, Is>...> makeIndexTuple(std::index_sequence<Is...>) {
return std::make_tuple( std::integral_constant<size_t, Is>{}...);
}
bool Analysis::check(vector<shared_ptr<SourceUnit const>> const& _sourceUnits)
{
using AnalysisSteps = std::tuple<SyntaxRestrictor, TypeRegistration, TypeInference, DebugWarner>;
return std::apply([&](auto... _indexTuple) {
return ([&](auto&& _step) {
for (auto source: _sourceUnits)
if (!_step.analyze(*source))
return false;
return true;
}(std::tuple_element_t<decltype(_indexTuple)::value, AnalysisSteps>{*this}) && ...);
}, makeIndexTuple(std::make_index_sequence<std::tuple_size_v<AnalysisSteps>>{}));
/*
{
SyntaxRestrictor syntaxRestrictor{*this};
for (auto source: _sourceUnits)
if (!syntaxRestrictor.analyze(*source))
return false;
}
{
TypeRegistration typeRegistration{*this};
for (auto source: _sourceUnits)
if (!typeRegistration.analyze(*source))
return false;
}
{
TypeInference typeInference{*this};
for (auto source: _sourceUnits)
if (!typeInference.analyze(*source))
return false;
}
return true;
*/
}
@@ -0,0 +1,107 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/experimental/ast/TypeSystem.h>
#include <cstdint>
#include <memory>
#include <vector>
namespace solidity::frontend
{
class SourceUnit;
class ASTNode;
}
namespace solidity::langutil
{
class ErrorReporter;
}
namespace solidity::frontend::experimental
{
class TypeSystem;
class Analysis;
namespace detail
{
template<typename Step>
struct AnnotationFetcher
{
Analysis& analysis;
typename Step::Annotation& get(ASTNode const& _node);
typename Step::GlobalAnnotation& get();
};
template<typename Step>
struct ConstAnnotationFetcher
{
Analysis const& analysis;
typename Step::Annotation const& get(ASTNode const& _node) const;
typename Step::GlobalAnnotation const& get() const;
};
}
class Analysis
{
struct AnnotationContainer;
struct GlobalAnnotationContainer;
public:
Analysis(langutil::ErrorReporter& _errorReporter, uint64_t _maxAstId);
Analysis(Analysis const&) = delete;
~Analysis();
Analysis const& operator=(Analysis const&) = delete;
bool check(std::vector<std::shared_ptr<SourceUnit const>> const& _sourceUnits);
langutil::ErrorReporter& errorReporter() { return m_errorReporter; }
uint64_t maxAstId() const { return m_maxAstId; }
TypeSystem& typeSystem() { return m_typeSystem; }
TypeSystem const& typeSystem() const { return m_typeSystem; }
template<typename Step>
typename Step::Annotation& annotation(ASTNode const& _node)
{
return detail::AnnotationFetcher<Step>{*this}.get(_node);
}
template<typename Step>
typename Step::Annotation const& annotation(ASTNode const& _node) const
{
return detail::ConstAnnotationFetcher<Step>{*this}.get(_node);
}
template<typename Step>
typename Step::GlobalAnnotation& annotation()
{
return detail::AnnotationFetcher<Step>{*this}.get();
}
template<typename Step>
typename Step::GlobalAnnotation const& annotation() const
{
return detail::ConstAnnotationFetcher<Step>{*this}.get();
}
AnnotationContainer& annotationContainer(ASTNode const& _node);
AnnotationContainer const& annotationContainer(ASTNode const& _node) const;
GlobalAnnotationContainer& annotationContainer() { return *m_globalAnnotation; }
GlobalAnnotationContainer const& annotationContainer() const { return *m_globalAnnotation; }
private:
langutil::ErrorReporter& m_errorReporter;
TypeSystem m_typeSystem;
uint64_t m_maxAstId = 0;
std::unique_ptr<AnnotationContainer[]> m_annotations;
std::unique_ptr<GlobalAnnotationContainer> m_globalAnnotation;
};
}
@@ -0,0 +1,57 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/experimental/analysis/DebugWarner.h>
#include <libsolidity/experimental/analysis/Analysis.h>
#include <libsolidity/experimental/analysis/TypeInference.h>
#include <libsolidity/experimental/ast/TypeSystemHelper.h>
#include <liblangutil/Exceptions.h>
using namespace solidity::frontend;
using namespace solidity::frontend::experimental;
using namespace solidity::langutil;
DebugWarner::DebugWarner(Analysis& _analysis): m_analysis(_analysis), m_errorReporter(_analysis.errorReporter())
{}
bool DebugWarner::analyze(ASTNode const& _astRoot)
{
_astRoot.accept(*this);
return !Error::containsErrors(m_errorReporter.errors());
}
bool DebugWarner::visitNode(ASTNode const& _node)
{
auto const& typeInferenceAnnotation = m_analysis.annotation<TypeInference>(_node);
if (typeInferenceAnnotation.type)
{
Type type = *typeInferenceAnnotation.type;
Sort sort = m_analysis.typeSystem().env().sort(type);
std::string sortString;
if (sort.classes.size() != 1 || *sort.classes.begin() != m_analysis.typeSystem().primitiveClass(PrimitiveClass::Type))
sortString = ":" + TypeSystemHelpers{m_analysis.typeSystem()}.sortToString(m_analysis.typeSystem().env().sort(type));
m_errorReporter.info(
0000_error,
_node.location(),
"Inferred type: " + TypeEnvironmentHelpers{m_analysis.typeSystem().env()}.typeToString(type) + sortString
);
}
return true;
}
@@ -0,0 +1,42 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/ast/ASTVisitor.h>
#include <liblangutil/ErrorReporter.h>
namespace solidity::frontend::experimental
{
class Analysis;
class DebugWarner: public ASTConstVisitor
{
public:
DebugWarner(Analysis& _analysis);
bool analyze(ASTNode const& _astRoot);
private:
bool visitNode(ASTNode const& _node) override;
Analysis& m_analysis;
langutil::ErrorReporter& m_errorReporter;
};
}
@@ -0,0 +1,113 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/experimental/analysis/SyntaxRestrictor.h>
#include <libsolidity/experimental/analysis/Analysis.h>
#include <liblangutil/Exceptions.h>
using namespace solidity::frontend;
using namespace solidity::frontend::experimental;
using namespace solidity::langutil;
SyntaxRestrictor::SyntaxRestrictor(Analysis& _analysis): m_errorReporter(_analysis.errorReporter())
{}
bool SyntaxRestrictor::analyze(ASTNode const& _astRoot)
{
_astRoot.accept(*this);
return !Error::containsErrors(m_errorReporter.errors());
}
bool SyntaxRestrictor::visitNode(ASTNode const& _node)
{
if (!_node.experimentalSolidityOnly())
m_errorReporter.syntaxError(0000_error, _node.location(), "Unsupported AST node.");
return false;
}
bool SyntaxRestrictor::visit(ContractDefinition const& _contractDefinition)
{
if (_contractDefinition.contractKind() != ContractKind::Contract)
m_errorReporter.syntaxError(0000_error, _contractDefinition.location(), "Only contracts are supported.");
if (!_contractDefinition.baseContracts().empty())
m_errorReporter.syntaxError(0000_error, _contractDefinition.location(), "Inheritance unsupported.");
return true;
}
bool SyntaxRestrictor::visit(FunctionDefinition const& _functionDefinition)
{
if (!_functionDefinition.isImplemented())
m_errorReporter.syntaxError(0000_error, _functionDefinition.location(), "Functions must be implemented.");
if (!_functionDefinition.modifiers().empty())
m_errorReporter.syntaxError(0000_error, _functionDefinition.location(), "Function may not have modifiers.");
if (_functionDefinition.overrides())
m_errorReporter.syntaxError(0000_error, _functionDefinition.location(), "Function may not have override specifiers.");
solAssert(!_functionDefinition.returnParameterList());
if (_functionDefinition.isFree())
{
if (_functionDefinition.stateMutability() != StateMutability::NonPayable)
m_errorReporter.syntaxError(0000_error, _functionDefinition.location(), "Free functions may not have a mutability.");
}
else
{
if (_functionDefinition.isFallback())
{
if (_functionDefinition.visibility() != Visibility::External)
m_errorReporter.syntaxError(0000_error, _functionDefinition.location(), "Fallback function must be external.");
}
else
m_errorReporter.syntaxError(0000_error, _functionDefinition.location(), "Only fallback functions are supported in contracts.");
}
return true;
}
bool SyntaxRestrictor::visit(VariableDeclarationStatement const& _variableDeclarationStatement)
{
if (_variableDeclarationStatement.declarations().size() == 1)
{
if (!_variableDeclarationStatement.declarations().front())
m_errorReporter.syntaxError(0000_error, _variableDeclarationStatement.initialValue()->location(), "Variable declaration has to declare a single variable.");
}
else
m_errorReporter.syntaxError(0000_error, _variableDeclarationStatement.initialValue()->location(), "Variable declarations can only declare a single variable.");
return true;
}
bool SyntaxRestrictor::visit(VariableDeclaration const& _variableDeclaration)
{
if (_variableDeclaration.value())
m_errorReporter.syntaxError(0000_error, _variableDeclaration.value()->location(), "Variable declarations with initial value not supported.");
if (_variableDeclaration.isStateVariable())
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "State variables are not supported.");
if (!_variableDeclaration.isLocalVariable())
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "Only local variables are supported.");
if (_variableDeclaration.mutability() != VariableDeclaration::Mutability::Mutable)
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "Only mutable variables are supported.");
if (_variableDeclaration.isIndexed())
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "Indexed variables are not supported.");
if (!_variableDeclaration.noVisibilitySpecified())
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "Variables with visibility not supported.");
if (_variableDeclaration.overrides())
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "Variables with override specifier not supported.");
if (_variableDeclaration.referenceLocation() != VariableDeclaration::Location::Unspecified)
m_errorReporter.syntaxError(0000_error, _variableDeclaration.location(), "Variables with reference location not supported.");
return true;
}
@@ -0,0 +1,67 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/ast/ASTVisitor.h>
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/Exceptions.h>
namespace solidity::frontend::experimental
{
class Analysis;
class SyntaxRestrictor: public ASTConstVisitor
{
public:
SyntaxRestrictor(Analysis& _analysis);
bool analyze(ASTNode const& _astRoot);
private:
/// Default visit will reject all AST nodes that are not explicitly allowed.
bool visitNode(ASTNode const& _node) override;
bool visit(SourceUnit const&) override { return true; }
bool visit(PragmaDirective const&) override { return true; }
bool visit(ImportDirective const&) override { return true; }
bool visit(ContractDefinition const& _contractDefinition) override;
bool visit(FunctionDefinition const& _functionDefinition) override;
bool visit(ExpressionStatement const&) override { return true; }
bool visit(FunctionCall const&) override { return true; }
bool visit(Assignment const&) override { return true; }
bool visit(Block const&) override { return true; }
bool visit(InlineAssembly const&) override { return true; }
bool visit(Identifier const&) override { return true; }
bool visit(IdentifierPath const&) override { return true; }
bool visit(IfStatement const&) override { return true; }
bool visit(VariableDeclarationStatement const&) override;
bool visit(VariableDeclaration const&) override;
bool visit(ElementaryTypeName const&) override { return true; }
bool visit(ParameterList const&) override { return true; }
bool visit(Return const&) override { return true; }
bool visit(MemberAccess const&) override { return true; }
bool visit(BinaryOperation const&) override { return true; }
bool visit(ElementaryTypeNameExpression const&) override { return true; }
bool visit(TupleExpression const&) override { return true; }
bool visit(Literal const&) override { return true; }
langutil::ErrorReporter& m_errorReporter;
};
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,126 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/ast/ASTVisitor.h>
#include <libsolidity/experimental/ast/TypeSystem.h>
#include <liblangutil/ErrorReporter.h>
namespace solidity::frontend::experimental
{
class Analysis;
class TypeInference: public ASTConstVisitor
{
public:
TypeInference(Analysis& _analysis);
bool analyze(SourceUnit const& _sourceUnit);
struct Annotation
{
/// Expressions, variable declarations, function declarations.
std::optional<Type> type;
// Type classes.
std::optional<TypeClass> typeClass;
};
struct TypeMember
{
Type type;
};
struct GlobalAnnotation
{
std::map<BuiltinClass, TypeClass> builtinClasses;
std::map<std::string, BuiltinClass> builtinClassesByName;
std::map<TypeClass, std::map<std::string, Type>> typeClassFunctions;
std::map<Token, std::tuple<TypeClass, std::string>> operators;
std::map<TypeConstructor, std::map<std::string, TypeMember>> members;
};
bool visit(Block const&) override { return true; }
bool visit(VariableDeclarationStatement const&) override { return true; }
void endVisit(VariableDeclarationStatement const& _variableDeclarationStatement) override;
bool visit(VariableDeclaration const& _variableDeclaration) override;
bool visit(FunctionDefinition const& _functionDefinition) override;
bool visit(ParameterList const&) override { return true; }
void endVisit(ParameterList const& _parameterList) override;
bool visit(SourceUnit const&) override { return true; }
bool visit(ContractDefinition const&) override { return true; }
bool visit(InlineAssembly const& _inlineAssembly) override;
bool visit(PragmaDirective const&) override { return false; }
bool visit(IfStatement const&) override { return true; }
void endVisit(IfStatement const& _ifStatement) override;
bool visit(ExpressionStatement const&) override { return true; }
bool visit(Assignment const&) override { return true; }
void endVisit(Assignment const& _assignment) override;
bool visit(Identifier const&) override;
bool visit(IdentifierPath const&) override;
bool visit(FunctionCall const& _functionCall) override;
void endVisit(FunctionCall const& _functionCall) override;
bool visit(Return const&) override { return true; }
void endVisit(Return const& _return) override;
bool visit(MemberAccess const& _memberAccess) override;
void endVisit(MemberAccess const& _memberAccess) override;
bool visit(ElementaryTypeNameExpression const& _expression) override;
bool visit(TypeClassDefinition const& _typeClassDefinition) override;
bool visit(TypeClassInstantiation const& _typeClassInstantiation) override;
bool visit(TupleExpression const&) override { return true; }
void endVisit(TupleExpression const& _tupleExpression) override;
bool visit(TypeDefinition const& _typeDefinition) override;
bool visitNode(ASTNode const& _node) override;
bool visit(BinaryOperation const& _operation) override;
bool visit(Literal const& _literal) override;
private:
Analysis& m_analysis;
langutil::ErrorReporter& m_errorReporter;
TypeSystem& m_typeSystem;
TypeEnvironment* m_env = nullptr;
Type m_voidType;
Type m_wordType;
Type m_integerType;
Type m_unitType;
Type m_boolType;
std::optional<Type> m_currentFunctionType;
Type getType(ASTNode const& _node) const;
Annotation& annotation(ASTNode const& _node);
Annotation const& annotation(ASTNode const& _node) const;
GlobalAnnotation& annotation();
void unify(Type _a, Type _b, langutil::SourceLocation _location = {});
void unifyGeneralized(Type _type, Type _scheme, std::vector<Type> _monomorphicTypes, langutil::SourceLocation _location = {});
Type polymorphicInstance(Type _scheme, langutil::SourceLocation _location = {});
Type memberType(Type _type, std::string _memberName, langutil::SourceLocation _location = {});
enum class ExpressionContext { Term, Type, Sort };
Type handleIdentifierByReferencedDeclaration(langutil::SourceLocation _location, Declaration const& _declaration);
TypeConstructor typeConstructor(Declaration const* _type) const;
Type type(Declaration const* _type, std::vector<Type> _arguments) const;
ExpressionContext m_expressionContext = ExpressionContext::Term;
std::set<TypeClassInstantiation const*, ASTCompareByID<TypeClassInstantiation>> m_activeInstantiations;
};
}
@@ -0,0 +1,188 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/experimental/analysis/TypeRegistration.h>
#include <libsolidity/experimental/analysis/Analysis.h>
#include <libsolidity/experimental/ast/TypeSystemHelper.h>
#include <liblangutil/Exceptions.h>
#include <libyul/AsmAnalysis.h>
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/AST.h>
using namespace std;
using namespace solidity::frontend;
using namespace solidity::frontend::experimental;
using namespace solidity::langutil;
TypeRegistration::TypeRegistration(Analysis& _analysis):
m_analysis(_analysis),
m_errorReporter(_analysis.errorReporter()),
m_typeSystem(_analysis.typeSystem())
{
}
bool TypeRegistration::analyze(SourceUnit const& _sourceUnit)
{
_sourceUnit.accept(*this);
return !m_errorReporter.hasErrors();
}
bool TypeRegistration::visit(TypeClassDefinition const& _typeClassDefinition)
{
if (annotation(_typeClassDefinition).typeConstructor)
return false;
annotation(_typeClassDefinition).typeConstructor = m_typeSystem.declareTypeConstructor(
_typeClassDefinition.name(),
"t_" + *_typeClassDefinition.annotation().canonicalName + "_" + util::toString(_typeClassDefinition.id()),
0,
&_typeClassDefinition
);
return true;
}
bool TypeRegistration::visit(ElementaryTypeName const& _typeName)
{
if (annotation(_typeName).typeConstructor)
return false;
annotation(_typeName).typeConstructor = [&]() -> optional<TypeConstructor> {
switch(_typeName.typeName().token())
{
case Token::Void:
return m_typeSystem.constructor(PrimitiveType::Void);
case Token::Fun:
return m_typeSystem.constructor(PrimitiveType::Function);
case Token::Unit:
return m_typeSystem.constructor(PrimitiveType::Unit);
case Token::Pair:
return m_typeSystem.constructor(PrimitiveType::Pair);
case Token::Word:
return m_typeSystem.constructor(PrimitiveType::Word);
case Token::Integer:
return m_typeSystem.constructor(PrimitiveType::Integer);
case Token::Bool:
return m_typeSystem.constructor(PrimitiveType::Bool);
default:
m_errorReporter.fatalTypeError(0000_error, _typeName.location(), "Expected primitive type.");
return nullopt;
}
}();
return true;
}
void TypeRegistration::endVisit(ElementaryTypeNameExpression const & _typeNameExpression)
{
if (annotation(_typeNameExpression).typeConstructor)
return;
// TODO: this is not visited in the ElementaryTypeNameExpression visit - is that intentional?
_typeNameExpression.type().accept(*this);
if (auto constructor = annotation(_typeNameExpression.type()).typeConstructor)
annotation(_typeNameExpression).typeConstructor = constructor;
else
solAssert(m_errorReporter.hasErrors());
}
bool TypeRegistration::visit(UserDefinedTypeName const& _userDefinedTypeName)
{
if (annotation(_userDefinedTypeName).typeConstructor)
return false;
auto const* declaration = _userDefinedTypeName.pathNode().annotation().referencedDeclaration;
if (!declaration)
{
// TODO: fatal/non-fatal
m_errorReporter.fatalTypeError(0000_error, _userDefinedTypeName.pathNode().location(), "Expected declaration.");
return false;
}
declaration->accept(*this);
if (!(annotation(_userDefinedTypeName).typeConstructor = annotation(*declaration).typeConstructor))
{
// TODO: fatal/non-fatal
m_errorReporter.fatalTypeError(0000_error, _userDefinedTypeName.pathNode().location(), "Expected type declaration.");
return false;
}
return true;
}
bool TypeRegistration::visit(TypeClassInstantiation const& _typeClassInstantiation)
{
if (annotation(_typeClassInstantiation).typeConstructor)
return false;
_typeClassInstantiation.typeConstructor().accept(*this);
auto typeConstructor = annotation(_typeClassInstantiation.typeConstructor()).typeConstructor;
if (!typeConstructor)
{
m_errorReporter.typeError(0000_error, _typeClassInstantiation.typeConstructor().location(), "Invalid type name.");
return false;
}
auto* instantiations = std::visit(util::GenericVisitor{
[&](ASTPointer<IdentifierPath> _path) -> TypeClassInstantiations*
{
if (TypeClassDefinition const* classDefinition = dynamic_cast<TypeClassDefinition const*>(_path->annotation().referencedDeclaration))
return &annotation(*classDefinition).instantiations;
m_errorReporter.typeError(0000_error, _typeClassInstantiation.typeClass().location(), "Expected a type class.");
return nullptr;
},
[&](Token _token) -> TypeClassInstantiations*
{
if (auto typeClass = builtinClassFromToken(_token))
return &annotation().builtinClassInstantiations[*typeClass];
m_errorReporter.typeError(0000_error, _typeClassInstantiation.typeClass().location(), "Expected a type class.");
return nullptr;
}
}, _typeClassInstantiation.typeClass().name());
if (!instantiations)
return false;
if (
auto [instantiation, newlyInserted] = instantiations->emplace(*typeConstructor, &_typeClassInstantiation);
!newlyInserted
)
{
SecondarySourceLocation ssl;
ssl.append("Previous instantiation.", instantiation->second->location());
m_errorReporter.typeError(0000_error, _typeClassInstantiation.location(), ssl, "Duplicate type class instantiation.");
}
return true;
}
bool TypeRegistration::visit(TypeDefinition const& _typeDefinition)
{
if (annotation(_typeDefinition).typeConstructor)
return false;
annotation(_typeDefinition).typeConstructor = m_typeSystem.declareTypeConstructor(
_typeDefinition.name(),
"t_" + *_typeDefinition.annotation().canonicalName + "_" + util::toString(_typeDefinition.id()),
_typeDefinition.arguments() ? _typeDefinition.arguments()->parameters().size() : 0,
&_typeDefinition
);
return true;
}
TypeRegistration::Annotation& TypeRegistration::annotation(ASTNode const& _node)
{
return m_analysis.annotation<TypeRegistration>(_node);
}
TypeRegistration::GlobalAnnotation& TypeRegistration::annotation()
{
return m_analysis.annotation<TypeRegistration>();
}
@@ -0,0 +1,65 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/ast/ASTVisitor.h>
#include <libsolidity/experimental/ast/TypeSystem.h>
#include <liblangutil/ErrorReporter.h>
namespace solidity::frontend::experimental
{
class Analysis;
class TypeRegistration: public ASTConstVisitor
{
public:
using TypeClassInstantiations = std::map<TypeConstructor, TypeClassInstantiation const*>;
struct Annotation
{
// For type class definitions.
TypeClassInstantiations instantiations;
// For type definitions, type class definitions, type names and type name expressions.
std::optional<TypeConstructor> typeConstructor;
};
struct GlobalAnnotation
{
std::map<PrimitiveClass, TypeClassInstantiations> primitiveClassInstantiations;
std::map<BuiltinClass, TypeClassInstantiations> builtinClassInstantiations;
};
TypeRegistration(Analysis& _analysis);
bool analyze(SourceUnit const& _sourceUnit);
private:
bool visit(TypeClassDefinition const& _typeClassDefinition) override;
bool visit(TypeClassInstantiation const& _typeClassInstantiation) override;
bool visit(TypeDefinition const& _typeDefinition) override;
bool visit(UserDefinedTypeName const& _typeName) override;
void endVisit(ElementaryTypeNameExpression const& _typeName) override;
bool visit(ElementaryTypeName const& _typeName) override;
Annotation& annotation(ASTNode const& _node);
GlobalAnnotation& annotation();
Analysis& m_analysis;
langutil::ErrorReporter& m_errorReporter;
TypeSystem& m_typeSystem;
std::set<int64_t> m_visitedClasses;
};
}
+63
View File
@@ -0,0 +1,63 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/experimental/ast/Type.h>
#include <libsolidity/ast/AST.h>
#include <libsolutil/Visitor.h>
#include <range/v3/view/drop_last.hpp>
#include <range/v3/view/zip.hpp>
#include <sstream>
using namespace std;
using namespace solidity;
using namespace solidity::frontend::experimental;
bool Sort::operator==(Sort const& _rhs) const
{
if (classes.size() != _rhs.classes.size())
return false;
for (auto [lhs, rhs]: ranges::zip_view(classes, _rhs.classes))
if (lhs != rhs)
return false;
return true;
}
bool Sort::operator<=(Sort const& _rhs) const
{
for (auto c: classes)
if (!_rhs.classes.count(c))
return false;
return true;
}
Sort Sort::operator+(Sort const& _rhs) const
{
Sort result { classes };
result.classes += _rhs.classes;
return result;
}
Sort Sort::operator-(Sort const& _rhs) const
{
Sort result { classes };
result.classes -= _rhs.classes;
return result;
}
+153
View File
@@ -0,0 +1,153 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <set>
#include <variant>
#include <vector>
namespace solidity::frontend::experimental
{
class TypeSystem;
struct TypeConstant;
struct TypeVariable;
using Type = std::variant<std::monostate, TypeConstant, TypeVariable>;
enum class PrimitiveType
{
Void,
Function,
TypeFunction,
Itself,
Unit,
Pair,
Sum,
Word,
Bool,
Integer
};
enum class PrimitiveClass
{
Type
};
// TODO: move elsewhere?
enum class BuiltinClass
{
Integer,
Mul,
Add,
Equal,
Less,
LessOrEqual,
Greater,
GreaterOrEqual
};
struct TypeConstructor
{
public:
TypeConstructor(TypeConstructor const& _typeConstructor): m_index(_typeConstructor.m_index) {}
TypeConstructor& operator=(TypeConstructor const& _typeConstructor)
{
m_index = _typeConstructor.m_index;
return *this;
}
bool operator<(TypeConstructor const& _rhs) const
{
return m_index < _rhs.m_index;
}
bool operator==(TypeConstructor const& _rhs) const
{
return m_index == _rhs.m_index;
}
bool operator!=(TypeConstructor const& _rhs) const
{
return m_index != _rhs.m_index;
}
private:
friend class TypeSystem;
TypeConstructor(size_t _index): m_index(_index) {}
size_t m_index = 0;
};
struct TypeConstant
{
TypeConstructor constructor;
std::vector<Type> arguments;
};
struct TypeClass
{
public:
TypeClass(TypeClass const& _typeClass): m_index(_typeClass.m_index) {}
TypeClass& operator=(TypeClass const& _typeConstructor)
{
m_index = _typeConstructor.m_index;
return *this;
}
bool operator<(TypeClass const& _rhs) const
{
return m_index < _rhs.m_index;
}
bool operator==(TypeClass const& _rhs) const
{
return m_index == _rhs.m_index;
}
bool operator!=(TypeClass const& _rhs) const
{
return m_index != _rhs.m_index;
}
private:
friend class TypeSystem;
TypeClass(size_t _index): m_index(_index) {}
size_t m_index = 0;
};
struct Sort
{
std::set<TypeClass> classes;
bool operator==(Sort const& _rhs) const;
bool operator!=(Sort const& _rhs) const { return !operator==(_rhs); }
bool operator<=(Sort const& _rhs) const;
Sort operator+(Sort const& _rhs) const;
Sort operator-(Sort const& _rhs) const;
};
struct Arity
{
std::vector<Sort> argumentSorts;
TypeClass typeClass;
};
struct TypeVariable
{
size_t index() const { return m_index; }
Sort const& sort() const { return m_sort; }
private:
friend class TypeSystem;
size_t m_index = 0;
Sort m_sort;
TypeVariable(size_t _index, Sort _sort): m_index(_index), m_sort(std::move(_sort)) {}
};
}
+347
View File
@@ -0,0 +1,347 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/experimental/ast/TypeSystem.h>
#include <libsolidity/experimental/ast/TypeSystemHelper.h>
#include <libsolidity/ast/AST.h>
#include <liblangutil/Exceptions.h>
#include <libsolutil/Visitor.h>
#include <range/v3/to_container.hpp>
#include <range/v3/view/drop_exactly.hpp>
#include <range/v3/view/drop_last.hpp>
#include <range/v3/view/reverse.hpp>
#include <range/v3/view/zip.hpp>
#include <fmt/format.h>
using namespace std;
using namespace solidity;
using namespace solidity::frontend;
using namespace solidity::frontend::experimental;
vector<TypeEnvironment::UnificationFailure> TypeEnvironment::unify(Type _a, Type _b)
{
vector<UnificationFailure> failures;
auto unificationFailure = [&]() {
failures.emplace_back(UnificationFailure{TypeMismatch{_a, _b}});
};
_a = resolve(_a);
_b = resolve(_b);
std::visit(util::GenericVisitor{
[&](TypeVariable _left, TypeVariable _right) {
if (_left.index() == _right.index())
{
if (_left.sort() != _right.sort())
unificationFailure();
}
else
{
if (_left.sort() <= _right.sort())
failures += instantiate(_left, _right);
else if (_right.sort() <= _left.sort())
failures += instantiate(_right, _left);
else
{
Type newVar = m_typeSystem.freshVariable(_left.sort() + _right.sort());
failures += instantiate(_left, newVar);
failures += instantiate(_right, newVar);
}
}
},
[&](TypeVariable _var, auto) {
failures += instantiate(_var, _b);
},
[&](auto, TypeVariable _var) {
failures += instantiate(_var, _a);
},
[&](TypeConstant _left, TypeConstant _right) {
if(_left.constructor != _right.constructor)
return unificationFailure();
if (_left.arguments.size() != _right.arguments.size())
return unificationFailure();
for (auto&& [left, right]: ranges::zip_view(_left.arguments, _right.arguments))
failures += unify(left, right);
},
[&](auto, auto) {
unificationFailure();
}
}, _a, _b);
return failures;
}
bool TypeEnvironment::typeEquals(Type _lhs, Type _rhs) const
{
return std::visit(util::GenericVisitor{
[&](TypeVariable _left, TypeVariable _right) {
if (_left.index() == _right.index())
{
solAssert(_left.sort() == _right.sort());
return true;
}
return false;
},
[&](TypeConstant _left, TypeConstant _right) {
if(_left.constructor != _right.constructor)
return false;
if (_left.arguments.size() != _right.arguments.size())
return false;
for (auto&& [left, right]: ranges::zip_view(_left.arguments, _right.arguments))
if (!typeEquals(left, right))
return false;
return true;
},
[&](auto, auto) {
return false;
}
}, resolve(_lhs), resolve(_rhs));
}
TypeEnvironment TypeEnvironment::clone() const
{
TypeEnvironment result{m_typeSystem};
result.m_typeVariables = m_typeVariables;
return result;
}
TypeSystem::TypeSystem()
{
auto declarePrimitiveClass = [&](std::string _name) {
return std::visit(util::GenericVisitor{
[](std::string _error) -> TypeClass {
solAssert(false, _error);
},
[](TypeClass _class) -> TypeClass { return _class; }
}, declareTypeClass(freshVariable({}), _name, nullptr));
};
m_primitiveTypeClasses.emplace(PrimitiveClass::Type, declarePrimitiveClass("type"));
for (auto [type, name, arity]: std::initializer_list<std::tuple<PrimitiveType, const char*, uint64_t>> {
{PrimitiveType::TypeFunction, "tfun", 2},
{PrimitiveType::Function, "fun", 2},
{PrimitiveType::Function, "itself", 1},
{PrimitiveType::Void, "void", 0},
{PrimitiveType::Unit, "unit", 0},
{PrimitiveType::Pair, "pair", 2},
{PrimitiveType::Sum, "sum", 2},
{PrimitiveType::Word, "word", 0},
{PrimitiveType::Integer, "integer", 0},
{PrimitiveType::Bool, "bool", 0},
})
m_primitiveTypeConstructors.emplace(type, declareTypeConstructor(name, name, arity, nullptr));
TypeClass classType = primitiveClass(PrimitiveClass::Type);
//TypeClass classKind = primitiveClass(PrimitiveClass::Kind);
Sort typeSort{{classType}};
m_typeConstructors.at(m_primitiveTypeConstructors.at(PrimitiveType::TypeFunction).m_index).arities = {Arity{vector<Sort>{{typeSort},{typeSort}}, classType}};
m_typeConstructors.at(m_primitiveTypeConstructors.at(PrimitiveType::Function).m_index).arities = {Arity{vector<Sort>{{typeSort, typeSort}}, classType}};
m_typeConstructors.at(m_primitiveTypeConstructors.at(PrimitiveType::Function).m_index).arities = {Arity{vector<Sort>{{typeSort, typeSort}}, classType}};
}
experimental::Type TypeSystem::freshVariable(Sort _sort)
{
uint64_t index = m_numTypeVariables++;
return TypeVariable(index, std::move(_sort));
}
experimental::Type TypeSystem::freshTypeVariable(Sort _sort)
{
_sort.classes.emplace(primitiveClass(PrimitiveClass::Type));
return freshVariable(_sort);
}
vector<TypeEnvironment::UnificationFailure> TypeEnvironment::instantiate(TypeVariable _variable, Type _type)
{
for (auto typeVar: TypeEnvironmentHelpers{*this}.typeVars(_type))
if (typeVar.index() == _variable.index())
return {UnificationFailure{RecursiveUnification{_variable, _type}}};
Sort typeSort = sort(_type);
if (!(_variable.sort() <= typeSort))
{
return {UnificationFailure{SortMismatch{_type, _variable.sort() - typeSort}}};
}
solAssert(m_typeVariables.emplace(_variable.index(), _type).second);
return {};
}
experimental::Type TypeEnvironment::resolve(Type _type) const
{
Type result = _type;
while(auto const* var = std::get_if<TypeVariable>(&result))
if (Type const* resolvedType = util::valueOrNullptr(m_typeVariables, var->index()))
result = *resolvedType;
else
break;
return result;
}
experimental::Type TypeEnvironment::resolveRecursive(Type _type) const
{
return std::visit(util::GenericVisitor{
[&](TypeConstant const& _type) -> Type {
return TypeConstant{
_type.constructor,
_type.arguments | ranges::views::transform([&](Type _argType) {
return resolveRecursive(_argType);
}) | ranges::to<vector<Type>>
};
},
[&](TypeVariable const&) -> Type {
return _type;
},
[&](std::monostate) -> Type {
return _type;
}
}, resolve(_type));
}
Sort TypeEnvironment::sort(Type _type) const
{
return std::visit(util::GenericVisitor{
[&](TypeConstant const& _expression) -> Sort
{
auto const& constructorInfo = m_typeSystem.constructorInfo(_expression.constructor);
auto argumentSorts = _expression.arguments | ranges::views::transform([&](Type _argumentType) {
return sort(resolve(_argumentType));
}) | ranges::to<vector<Sort>>;
Sort sort;
for (auto const& arity: constructorInfo.arities)
{
solAssert(arity.argumentSorts.size() == argumentSorts.size());
bool hasArity = true;
for (auto&& [argumentSort, arityArgumentSort]: ranges::zip_view(argumentSorts, arity.argumentSorts))
{
if (!(arityArgumentSort <= argumentSort))
{
hasArity = false;
break;
}
}
if (hasArity)
sort.classes.insert(arity.typeClass);
}
return sort;
},
[](TypeVariable const& _variable) -> Sort { return _variable.sort(); },
[](std::monostate) -> Sort { solAssert(false); }
}, _type);
}
TypeConstructor TypeSystem::declareTypeConstructor(string _name, string _canonicalName, size_t _arguments, Declaration const* _declaration)
{
solAssert(m_canonicalTypeNames.insert(_canonicalName).second, "Duplicate canonical type name.");
Sort baseSort{{primitiveClass(PrimitiveClass::Type)}};
size_t index = m_typeConstructors.size();
m_typeConstructors.emplace_back(TypeConstructorInfo{
_name,
_canonicalName,
{Arity{vector<Sort>{_arguments, baseSort}, primitiveClass(PrimitiveClass::Type)}},
_declaration
});
TypeConstructor constructor{index};
if (_arguments)
{
std::vector<Sort> argumentSorts;
std::generate_n(std::back_inserter(argumentSorts), _arguments, [&](){ return Sort{{primitiveClass(PrimitiveClass::Type)}}; });
std::vector<Type> argumentTypes;
std::generate_n(std::back_inserter(argumentTypes), _arguments, [&](){ return freshVariable({}); });
auto error = instantiateClass(type(constructor, argumentTypes), Arity{argumentSorts, primitiveClass(PrimitiveClass::Type)});
solAssert(!error, *error);
}
else
{
auto error = instantiateClass(type(constructor, {}), Arity{{}, primitiveClass(PrimitiveClass::Type)});
solAssert(!error, *error);
}
return constructor;
}
std::variant<TypeClass, std::string> TypeSystem::declareTypeClass(Type _typeVariable, std::string _name, Declaration const* _declaration)
{
TypeVariable const* typeVariable = get_if<TypeVariable>(&_typeVariable);
if (!typeVariable)
return "Invalid type variable.";
size_t index = m_typeClasses.size();
m_typeClasses.emplace_back(TypeClassInfo{
_typeVariable,
_name,
_declaration
});
TypeClass typeClass{index};
return typeClass;
}
experimental::Type TypeSystem::type(TypeConstructor _constructor, std::vector<Type> _arguments) const
{
// TODO: proper error handling
auto const& info = m_typeConstructors.at(_constructor.m_index);
solAssert(info.arguments() == _arguments.size(), "Invalid arity.");
return TypeConstant{_constructor, _arguments};
}
experimental::Type TypeEnvironment::fresh(Type _type)
{
std::unordered_map<uint64_t, Type> mapping;
auto freshImpl = [&](Type _type, auto _recurse) -> Type {
return std::visit(util::GenericVisitor{
[&](TypeConstant const& _type) -> Type {
return TypeConstant{
_type.constructor,
_type.arguments | ranges::views::transform([&](Type _argType) {
return _recurse(_argType, _recurse);
}) | ranges::to<vector<Type>>
};
},
[&](TypeVariable const& _var) -> Type {
if (auto* mapped = util::valueOrNullptr(mapping, _var.index()))
{
auto* typeVariable = get_if<TypeVariable>(mapped);
solAssert(typeVariable);
// TODO: can there be a mismatch?
solAssert(typeVariable->sort() == _var.sort());
return *mapped;
}
return mapping[_var.index()] = m_typeSystem.freshTypeVariable(_var.sort());
},
[](std::monostate) -> Type { solAssert(false); }
}, resolve(_type));
};
return freshImpl(_type, freshImpl);
}
std::optional<std::string> TypeSystem::instantiateClass(Type _instanceVariable, Arity _arity)
{
if (!TypeSystemHelpers{*this}.isTypeConstant(_instanceVariable))
return "Invalid instance variable.";
auto [typeConstructor, typeArguments] = TypeSystemHelpers{*this}.destTypeConstant(_instanceVariable);
auto& typeConstructorInfo = m_typeConstructors.at(typeConstructor.m_index);
if (_arity.argumentSorts.size() != typeConstructorInfo.arguments())
return "Invalid arity.";
if (typeArguments.size() != typeConstructorInfo.arguments())
return "Invalid arity.";
typeConstructorInfo.arities.emplace_back(_arity);
return nullopt;
}
+154
View File
@@ -0,0 +1,154 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/experimental/ast/Type.h>
#include <liblangutil/Exceptions.h>
#include <optional>
#include <string>
#include <variant>
#include <vector>
namespace solidity::frontend
{
class Declaration;
}
namespace solidity::frontend::experimental
{
class TypeEnvironment
{
public:
TypeEnvironment(TypeSystem& _typeSystem): m_typeSystem(_typeSystem) {}
TypeEnvironment(TypeEnvironment const&) = delete;
TypeEnvironment& operator=(TypeEnvironment const&) = delete;
TypeEnvironment clone() const;
Type resolve(Type _type) const;
Type resolveRecursive(Type _type) const;
Type fresh(Type _type);
struct TypeMismatch { Type a; Type b; };
struct SortMismatch { Type type; Sort sort; };
struct RecursiveUnification { Type var; Type type; };
using UnificationFailure = std::variant<TypeMismatch, SortMismatch, RecursiveUnification>;
[[nodiscard]] std::vector<UnificationFailure> unify(Type _a, Type _b);
Sort sort(Type _type) const;
bool typeEquals(Type _lhs, Type _rhs) const;
TypeSystem& typeSystem() { return m_typeSystem; }
TypeSystem const& typeSystem() const { return m_typeSystem; }
private:
TypeEnvironment(TypeEnvironment&& _env): m_typeSystem(_env.m_typeSystem), m_typeVariables(std::move(_env.m_typeVariables)) {}
[[nodiscard]] std::vector<TypeEnvironment::UnificationFailure> instantiate(TypeVariable _variable, Type _type);
TypeSystem& m_typeSystem;
std::map<size_t, Type> m_typeVariables;
};
class TypeSystem
{
public:
struct TypeConstructorInfo
{
std::string name;
std::string canonicalName;
std::vector<Arity> arities;
Declaration const* typeDeclaration = nullptr;
size_t arguments() const
{
solAssert(!arities.empty());
return arities.front().argumentSorts.size();
}
};
struct TypeClassInfo
{
Type typeVariable;
std::string name;
Declaration const* classDeclaration = nullptr;
};
TypeSystem();
TypeSystem(TypeSystem const&) = delete;
TypeSystem const& operator=(TypeSystem const&) = delete;
Type type(PrimitiveType _typeConstructor, std::vector<Type> _arguments) const
{
return type(m_primitiveTypeConstructors.at(_typeConstructor), std::move(_arguments));
}
Type type(TypeConstructor _typeConstructor, std::vector<Type> _arguments) const;
std::string typeName(TypeConstructor _typeConstructor) const
{
// TODO: proper error handling
return m_typeConstructors.at(_typeConstructor.m_index).name;
}
std::string canonicalName(TypeConstructor _typeConstructor) const
{
// TODO: proper error handling
return m_typeConstructors.at(_typeConstructor.m_index).canonicalName;
}
TypeConstructor declareTypeConstructor(std::string _name, std::string _canonicalName, size_t _arguments, Declaration const* _declaration);
TypeConstructor constructor(PrimitiveType _type) const
{
return m_primitiveTypeConstructors.at(_type);
}
TypeClass primitiveClass(PrimitiveClass _class) const
{
return m_primitiveTypeClasses.at(_class);
}
size_t constructorArguments(TypeConstructor _typeConstructor) const
{
// TODO: error handling
return m_typeConstructors.at(_typeConstructor.m_index).arguments();
}
TypeConstructorInfo const& constructorInfo(TypeConstructor _typeConstructor) const
{
// TODO: error handling
return m_typeConstructors.at(_typeConstructor.m_index);
}
TypeConstructorInfo const& constructorInfo(PrimitiveType _typeConstructor) const
{
return constructorInfo(constructor(_typeConstructor));
}
std::variant<TypeClass, std::string> declareTypeClass(Type _typeVariable, std::string _name, Declaration const* _declaration);
[[nodiscard]] std::optional<std::string> instantiateClass(Type _instanceVariable, Arity _arity);
Type freshTypeVariable(Sort _sort);
TypeEnvironment const& env() const { return m_globalTypeEnvironment; }
TypeEnvironment& env() { return m_globalTypeEnvironment; }
Type freshVariable(Sort _sort);
std::string typeClassName(TypeClass _class) const { return m_typeClasses.at(_class.m_index).name; }
Declaration const* typeClassDeclaration(TypeClass _class) const { return m_typeClasses.at(_class.m_index).classDeclaration; }
Type typeClassVariable(TypeClass _class) const
{
return m_typeClasses.at(_class.m_index).typeVariable;
}
private:
friend class TypeEnvironment;
TypeClassInfo const& typeClassInfo(TypeClass _class) const
{
return m_typeClasses.at(_class.m_index);
}
size_t m_numTypeVariables = 0;
std::map<PrimitiveType, TypeConstructor> m_primitiveTypeConstructors;
std::map<PrimitiveClass, TypeClass> m_primitiveTypeClasses;
std::set<std::string> m_canonicalTypeNames;
std::vector<TypeConstructorInfo> m_typeConstructors;
std::vector<TypeClassInfo> m_typeClasses;
TypeEnvironment m_globalTypeEnvironment{*this};
};
}
@@ -0,0 +1,403 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/experimental/ast/TypeSystemHelper.h>
#include <libsolidity/ast/AST.h>
#include <libsolidity/experimental/analysis/Analysis.h>
#include <libsolidity/experimental/analysis/TypeRegistration.h>
#include <libsolutil/Visitor.h>
#include <range/v3/to_container.hpp>
#include <range/v3/view/drop_exactly.hpp>
#include <range/v3/view/drop_last.hpp>
#include <range/v3/view/reverse.hpp>
#include <fmt/format.h>
using namespace std;
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::frontend::experimental;
/*std::optional<TypeConstructor> experimental::typeConstructorFromTypeName(Analysis const& _analysis, TypeName const& _typeName)
{
if (auto const* elementaryTypeName = dynamic_cast<ElementaryTypeName const*>(&_typeName))
{
if (auto constructor = typeConstructorFromToken(_analysis, elementaryTypeName->typeName().token()))
return *constructor;
}
else if (auto const* userDefinedType = dynamic_cast<UserDefinedTypeName const*>(&_typeName))
{
if (auto const* referencedDeclaration = userDefinedType->pathNode().annotation().referencedDeclaration)
return _analysis.annotation<TypeRegistration>(*referencedDeclaration).typeConstructor;
}
return nullopt;
}*/
/*
std::optional<TypeConstructor> experimental::typeConstructorFromToken(Analysis const& _analysis, langutil::Token _token)
{
TypeSystem const& typeSystem = _analysis.typeSystem();
switch(_token)
{
case Token::Void:
return typeSystem.builtinConstructor(BuiltinType::Void);
case Token::Fun:
return typeSystem.builtinConstructor(BuiltinType::Function);
case Token::Unit:
return typeSystem.builtinConstructor(BuiltinType::Unit);
case Token::Pair:
return typeSystem.builtinConstructor(BuiltinType::Pair);
case Token::Word:
return typeSystem.builtinConstructor(BuiltinType::Word);
case Token::Integer:
return typeSystem.builtinConstructor(BuiltinType::Integer);
case Token::Bool:
return typeSystem.builtinConstructor(BuiltinType::Bool);
default:
return nullopt;
}
}*/
std::optional<BuiltinClass> experimental::builtinClassFromToken(langutil::Token _token)
{
switch (_token)
{
case Token::Integer:
return BuiltinClass::Integer;
case Token::Mul:
return BuiltinClass::Mul;
case Token::Add:
return BuiltinClass::Add;
case Token::Equal:
return BuiltinClass::Equal;
case Token::LessThan:
return BuiltinClass::Less;
case Token::LessThanOrEqual:
return BuiltinClass::LessOrEqual;
case Token::GreaterThan:
return BuiltinClass::Greater;
case Token::GreaterThanOrEqual:
return BuiltinClass::GreaterOrEqual;
default:
return nullopt;
}
}
/*
std::optional<TypeClass> experimental::typeClassFromTypeClassName(TypeClassName const& _typeClass)
{
return std::visit(util::GenericVisitor{
[&](ASTPointer<IdentifierPath> _path) -> optional<TypeClass> {
auto classDefinition = dynamic_cast<TypeClassDefinition const*>(_path->annotation().referencedDeclaration);
if (!classDefinition)
return nullopt;
return TypeClass{classDefinition};
},
[&](Token _token) -> optional<TypeClass> {
return typeClassFromToken(_token);
}
}, _typeClass.name());
}
*/
experimental::Type TypeSystemHelpers::tupleType(vector<Type> _elements) const
{
if (_elements.empty())
return typeSystem.type(PrimitiveType::Unit, {});
if (_elements.size() == 1)
return _elements.front();
Type result = _elements.back();
for (Type type: _elements | ranges::views::reverse | ranges::views::drop_exactly(1))
result = typeSystem.type(PrimitiveType::Pair, {type, result});
return result;
}
vector<experimental::Type> TypeSystemHelpers::destTupleType(Type _tupleType) const
{
if (!isTypeConstant(_tupleType))
return {_tupleType};
TypeConstructor pairConstructor = typeSystem.constructor(PrimitiveType::Pair);
auto [constructor, arguments] = destTypeConstant(_tupleType);
if (constructor == typeSystem.constructor(PrimitiveType::Unit))
return {};
if (constructor != pairConstructor)
return {_tupleType};
solAssert(arguments.size() == 2);
vector<Type> result;
result.emplace_back(arguments.front());
Type tail = arguments.back();
while(true)
{
if (!isTypeConstant(tail))
break;
auto [tailConstructor, tailArguments] = destTypeConstant(tail);
if (tailConstructor != pairConstructor)
break;
solAssert(tailArguments.size() == 2);
result.emplace_back(tailArguments.front());
tail = tailArguments.back();
}
result.emplace_back(tail);
return result;
}
experimental::Type TypeSystemHelpers::sumType(vector<Type> _elements) const
{
if (_elements.empty())
return typeSystem.type(PrimitiveType::Void, {});
if (_elements.size() == 1)
return _elements.front();
Type result = _elements.back();
for (Type type: _elements | ranges::views::reverse | ranges::views::drop_exactly(1))
result = typeSystem.type(PrimitiveType::Sum, {type, result});
return result;
}
vector<experimental::Type> TypeSystemHelpers::destSumType(Type _tupleType) const
{
if (!isTypeConstant(_tupleType))
return {_tupleType};
TypeConstructor sumConstructor = typeSystem.constructor(PrimitiveType::Sum);
auto [constructor, arguments] = destTypeConstant(_tupleType);
if (constructor == typeSystem.constructor(PrimitiveType::Void))
return {};
if (constructor != sumConstructor)
return {_tupleType};
solAssert(arguments.size() == 2);
vector<Type> result;
result.emplace_back(arguments.front());
Type tail = arguments.back();
while(true)
{
if (!isTypeConstant(tail))
break;
auto [tailConstructor, tailArguments] = destTypeConstant(tail);
if (tailConstructor != sumConstructor)
break;
solAssert(tailArguments.size() == 2);
result.emplace_back(tailArguments.front());
tail = tailArguments.back();
}
result.emplace_back(tail);
return result;
}
tuple<TypeConstructor, vector<experimental::Type>> TypeSystemHelpers::destTypeConstant(Type _type) const
{
using ResultType = tuple<TypeConstructor, vector<Type>>;
return std::visit(util::GenericVisitor{
[&](TypeConstant const& _type) -> ResultType {
return std::make_tuple(_type.constructor, _type.arguments);
},
[](auto const&) -> ResultType {
solAssert(false);
}
}, _type);
}
bool TypeSystemHelpers::isTypeConstant(Type _type) const
{
return std::visit(util::GenericVisitor{
[&](TypeConstant const&) -> bool {
return true;
},
[](auto const&) -> bool {
return false;
}
}, _type);
}
experimental::Type TypeSystemHelpers::functionType(experimental::Type _argType, experimental::Type _resultType) const
{
return typeSystem.type(PrimitiveType::Function, {_argType, _resultType});
}
tuple<experimental::Type, experimental::Type> TypeSystemHelpers::destFunctionType(Type _functionType) const
{
auto [constructor, arguments] = destTypeConstant(_functionType);
solAssert(constructor == typeSystem.constructor(PrimitiveType::Function));
solAssert(arguments.size() == 2);
return make_tuple(arguments.front(), arguments.back());
}
bool TypeSystemHelpers::isFunctionType(Type _type) const
{
if (!isTypeConstant(_type))
return false;
auto constructor = get<0>(destTypeConstant(_type));
return constructor == typeSystem.constructor(PrimitiveType::Function);
}
experimental::Type TypeSystemHelpers::typeFunctionType(experimental::Type _argType, experimental::Type _resultType) const
{
return typeSystem.type(PrimitiveType::TypeFunction, {_argType, _resultType});
}
tuple<experimental::Type, experimental::Type> TypeSystemHelpers::destTypeFunctionType(Type _functionType) const
{
auto [constructor, arguments] = destTypeConstant(_functionType);
solAssert(constructor == typeSystem.constructor(PrimitiveType::TypeFunction));
solAssert(arguments.size() == 2);
return make_tuple(arguments.front(), arguments.back());
}
bool TypeSystemHelpers::isTypeFunctionType(Type _type) const
{
if (!isTypeConstant(_type))
return false;
auto constructor = get<0>(destTypeConstant(_type));
return constructor == typeSystem.constructor(PrimitiveType::TypeFunction);
}
vector<experimental::Type> TypeEnvironmentHelpers::typeVars(Type _type) const
{
set<size_t> indices;
vector<Type> typeVars;
auto typeVarsImpl = [&](Type _type, auto _recurse) -> void {
std::visit(util::GenericVisitor{
[&](TypeConstant const& _type) {
for (auto arg: _type.arguments)
_recurse(arg, _recurse);
},
[&](TypeVariable const& _var) {
if (indices.emplace(_var.index()).second)
typeVars.emplace_back(_var);
},
[](std::monostate) { solAssert(false); }
}, env.resolve(_type));
};
typeVarsImpl(_type, typeVarsImpl);
return typeVars;
}
std::string TypeSystemHelpers::sortToString(Sort _sort) const
{
switch (_sort.classes.size())
{
case 0:
return "()";
case 1:
return typeSystem.typeClassName(*_sort.classes.begin());
default:
{
std::stringstream stream;
stream << "(";
for (auto typeClass: _sort.classes | ranges::views::drop_last(1))
stream << typeSystem.typeClassName(typeClass) << ", ";
stream << typeSystem.typeClassName(*_sort.classes.rbegin()) << ")";
return stream.str();
}
}
}
string TypeEnvironmentHelpers::canonicalTypeName(Type _type) const
{
return visit(util::GenericVisitor{
[&](TypeConstant _type) -> string {
std::stringstream stream;
stream << env.typeSystem().constructorInfo(_type.constructor).canonicalName;
if (!_type.arguments.empty())
{
stream << "$";
for (auto type: _type.arguments | ranges::views::drop_last(1))
stream << canonicalTypeName(type) << "$";
stream << canonicalTypeName(_type.arguments.back());
stream << "$";
}
return stream.str();
},
[](TypeVariable) -> string {
solAssert(false);
},
[](std::monostate) -> string {
solAssert(false);
},
}, env.resolve(_type));
}
std::string TypeEnvironmentHelpers::typeToString(Type const& _type) const
{
std::map<TypeConstructor, std::function<string(std::vector<Type>)>> formatters{
{env.typeSystem().constructor(PrimitiveType::Function), [&](auto const& _args) {
solAssert(_args.size() == 2);
return fmt::format("{} -> {}", typeToString(_args.front()), typeToString(_args.back()));
}},
{env.typeSystem().constructor(PrimitiveType::Unit), [&](auto const& _args) {
solAssert(_args.size() == 0);
return "()";
}},
{env.typeSystem().constructor(PrimitiveType::Pair), [&](auto const& _arguments) {
auto tupleTypes = TypeSystemHelpers{env.typeSystem()}.destTupleType(_arguments.back());
string result = "(";
result += typeToString(_arguments.front());
for (auto type: tupleTypes)
result += ", " + typeToString(type);
result += ")";
return result;
}},
};
return std::visit(util::GenericVisitor{
[&](TypeConstant const& _type) {
if (auto* formatter = util::valueOrNullptr(formatters, _type.constructor))
return (*formatter)(_type.arguments);
std::stringstream stream;
stream << env.typeSystem().constructorInfo(_type.constructor).name;
if (!_type.arguments.empty())
{
stream << "(";
for (auto type: _type.arguments | ranges::views::drop_last(1))
stream << typeToString(type) << ", ";
stream << typeToString(_type.arguments.back());
stream << ")";
}
return stream.str();
},
[&](TypeVariable const& _type) {
std::stringstream stream;
std::string varName;
size_t index = _type.index();
varName += 'a' + static_cast<char>(index%26);
while (index /= 26)
varName += 'a' + static_cast<char>(index%26);
reverse(varName.begin(), varName.end());
stream << '\'' << varName;
switch (_type.sort().classes.size())
{
case 0:
break;
case 1:
stream << ":" << env.typeSystem().typeClassName(*_type.sort().classes.begin());
break;
default:
stream << ":(";
for (auto typeClass: _type.sort().classes | ranges::views::drop_last(1))
stream << env.typeSystem().typeClassName(typeClass) << ", ";
stream << env.typeSystem().typeClassName(*_type.sort().classes.rbegin());
stream << ")";
break;
}
return stream.str();
},
[](std::monostate) -> string { solAssert(false); }
}, env.resolve(_type));
}
@@ -0,0 +1,59 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/experimental/ast/TypeSystem.h>
#include <libsolidity/ast/ASTForward.h>
#include <liblangutil/Token.h>
namespace solidity::frontend::experimental
{
class Analysis;
enum class BuiltinClass;
//std::optional<TypeConstructor> typeConstructorFromTypeName(Analysis const& _analysis, TypeName const& _typeName);
//std::optional<TypeConstructor> typeConstructorFromToken(Analysis const& _analysis, langutil::Token _token);
//std::optional<TypeClass> typeClassFromTypeClassName(TypeClassName const& _typeClass);
std::optional<BuiltinClass> builtinClassFromToken(langutil::Token _token);
struct TypeSystemHelpers
{
TypeSystem const& typeSystem;
std::tuple<TypeConstructor, std::vector<Type>> destTypeConstant(Type _type) const;
bool isTypeConstant(Type _type) const;
Type tupleType(std::vector<Type> _elements) const;
std::vector<Type> destTupleType(Type _tupleType) const;
Type sumType(std::vector<Type> _elements) const;
std::vector<Type> destSumType(Type _tupleType) const;
Type functionType(Type _argType, Type _resultType) const;
std::tuple<Type, Type> destFunctionType(Type _functionType) const;
bool isFunctionType(Type _type) const;
Type typeFunctionType(Type _argType, Type _resultType) const;
std::tuple<Type, Type> destTypeFunctionType(Type _functionType) const;
bool isTypeFunctionType(Type _type) const;
std::string sortToString(Sort _sort) const;
};
struct TypeEnvironmentHelpers
{
TypeEnvironment const& env;
std::string typeToString(Type const& _type) const;
std::string canonicalTypeName(Type _type) const;
std::vector<Type> typeVars(Type _type) const;
};
}
@@ -0,0 +1,74 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/experimental/codegen/Common.h>
#include <libsolidity/experimental/ast/TypeSystem.h>
#include <libsolidity/experimental/ast/TypeSystemHelper.h>
#include <libsolutil/CommonIO.h>
#include <libyul/AsmPrinter.h>
using namespace std;
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::util;
using namespace solidity::yul;
namespace solidity::frontend::experimental
{
string IRNames::function(TypeEnvironment const& _env, FunctionDefinition const& _function, Type _type)
{
if (_function.isConstructor())
return constructor(*_function.annotation().contract);
return "fun_" + _function.name() + "_" + to_string(_function.id()) + "$" + TypeEnvironmentHelpers{_env}.canonicalTypeName(_type) + "$";
}
string IRNames::function(VariableDeclaration const& _varDecl)
{
return "getter_fun_" + _varDecl.name() + "_" + to_string(_varDecl.id());
}
string IRNames::creationObject(ContractDefinition const& _contract)
{
return _contract.name() + "_" + toString(_contract.id());
}
string IRNames::deployedObject(ContractDefinition const& _contract)
{
return _contract.name() + "_" + toString(_contract.id()) + "_deployed";
}
string IRNames::constructor(ContractDefinition const& _contract)
{
return "constructor_" + _contract.name() + "_" + to_string(_contract.id());
}
string IRNames::localVariable(VariableDeclaration const& _declaration)
{
return "var_" + _declaration.name() + '_' + std::to_string(_declaration.id());
}
string IRNames::localVariable(Expression const& _expression)
{
return "expr_" + to_string(_expression.id());
}
}
+41
View File
@@ -0,0 +1,41 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/ast/AST.h>
#include <libsolidity/experimental/ast/TypeSystem.h>
#include <algorithm>
#include <string>
namespace solidity::frontend::experimental
{
struct IRNames
{
static std::string function(TypeEnvironment const& _env, FunctionDefinition const& _function, Type _type);
static std::string function(VariableDeclaration const& _varDecl);
static std::string creationObject(ContractDefinition const& _contract);
static std::string deployedObject(ContractDefinition const& _contract);
static std::string constructor(ContractDefinition const& _contract);
static std::string localVariable(VariableDeclaration const& _declaration);
static std::string localVariable(Expression const& _expression);
};
}
@@ -0,0 +1,55 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/ast/ASTForward.h>
#include <libsolidity/experimental/analysis/Analysis.h>
#include <libsolidity/experimental/ast/TypeSystem.h>
#include <list>
#include <set>
namespace solidity::frontend::experimental
{
class Analysis;
struct IRGenerationContext
{
Analysis const& analysis;
TypeEnvironment const* env = nullptr;
void enqueueFunctionDefinition(FunctionDefinition const* _functionDefinition, Type _type)
{
QueuedFunction queue{_functionDefinition, env->resolve(_type)};
for (auto type: generatedFunctions[_functionDefinition])
if (env->typeEquals(type, _type))
return;
functionQueue.emplace_back(queue);
}
struct QueuedFunction
{
FunctionDefinition const* function = nullptr;
Type type = std::monostate{};
};
std::list<QueuedFunction> functionQueue;
std::map<FunctionDefinition const*, std::vector<Type>> generatedFunctions;
};
}
@@ -0,0 +1,161 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/experimental/codegen/IRGenerator.h>
#include <libsolidity/experimental/codegen/IRGenerationContext.h>
#include <libsolidity/experimental/codegen/IRGeneratorForStatements.h>
#include <libsolidity/experimental/codegen/Common.h>
#include <libsolidity/experimental/analysis/Analysis.h>
#include <libsolidity/experimental/analysis/TypeInference.h>
#include <libsolidity/experimental/ast/TypeSystemHelper.h>
#include <libyul/YulStack.h>
#include <libyul/AsmPrinter.h>
#include <libyul/AST.h>
#include <libyul/optimiser/ASTCopier.h>
#include <liblangutil/SourceReferenceFormatter.h>
#include <libsolutil/Whiskers.h>
#include <range/v3/view/drop_last.hpp>
#include <variant>
using namespace std;
using namespace solidity;
using namespace solidity::frontend::experimental;
using namespace solidity::langutil;
using namespace solidity::util;
IRGenerator::IRGenerator(
EVMVersion _evmVersion,
std::optional<uint8_t> _eofVersion,
frontend::RevertStrings, std::map<std::string, unsigned int>,
DebugInfoSelection const&,
CharStreamProvider const*,
Analysis const& _analysis
)
:
m_evmVersion(_evmVersion),
m_eofVersion(_eofVersion),
// m_debugInfoSelection(_debugInfoSelection),
// m_soliditySourceProvider(_soliditySourceProvider),
m_env(_analysis.typeSystem().env().clone()),
m_context{_analysis, &m_env, {}, {}}
{
}
string IRGenerator::run(
ContractDefinition const& _contract,
bytes const& /*_cborMetadata*/,
map<ContractDefinition const*, string_view const> const& /*_otherYulSources*/
)
{
Whiskers t(R"(
object "<CreationObject>" {
code {
codecopy(0, dataoffset("<DeployedObject>"), datasize("<DeployedObject>"))
return(0, datasize("<DeployedObject>"))
}
object "<DeployedObject>" {
code {
<code>
}
}
}
)");
t("CreationObject", IRNames::creationObject(_contract));
t("DeployedObject", IRNames::deployedObject(_contract));
t("code", generate(_contract));
return t.render();
}
string IRGenerator::generate(ContractDefinition const& _contract)
{
std::stringstream code;
code << "{\n";
if (_contract.fallbackFunction())
{
auto type = m_context.analysis.annotation<TypeInference>(*_contract.fallbackFunction()).type;
solAssert(type);
type = m_context.env->resolve(*type);
code << IRNames::function(*m_context.env, *_contract.fallbackFunction(), *type) << "()\n";
m_context.enqueueFunctionDefinition(_contract.fallbackFunction(), *type);
}
code << "revert(0,0)\n";
code << "}\n";
while (!m_context.functionQueue.empty())
{
auto queueEntry = m_context.functionQueue.front();
m_context.functionQueue.pop_front();
auto& generatedTypes = m_context.generatedFunctions.insert(std::make_pair(queueEntry.function, vector<Type>{})).first->second;
if (!util::contains_if(generatedTypes, [&](auto const& _generatedType) { return m_context.env->typeEquals(_generatedType, queueEntry.type); }))
{
generatedTypes.emplace_back(queueEntry.type);
code << generate(*queueEntry.function, queueEntry.type);
}
}
return code.str();
}
string IRGenerator::generate(FunctionDefinition const& _function, Type _type)
{
TypeEnvironment newEnv = m_context.env->clone();
ScopedSaveAndRestore envRestore{m_context.env, &newEnv};
auto type = m_context.analysis.annotation<TypeInference>(_function).type;
solAssert(type);
for (auto err: newEnv.unify(*type, _type))
{
TypeEnvironmentHelpers helper{newEnv};
solAssert(false, helper.typeToString(*type) + " <-> " + helper.typeToString(_type));
}
std::stringstream code;
code << "function " << IRNames::function(newEnv, _function, _type) << "(";
if (_function.parameters().size() > 1)
for (auto const& arg: _function.parameters() | ranges::views::drop_last(1))
code << IRNames::localVariable(*arg) << ", ";
if (!_function.parameters().empty())
code << IRNames::localVariable(*_function.parameters().back());
code << ")";
if (_function.experimentalReturnExpression())
{
auto returnType = m_context.analysis.annotation<TypeInference>(*_function.experimentalReturnExpression()).type;
solAssert(returnType);
if (!m_env.typeEquals(*returnType, m_context.analysis.typeSystem().type(PrimitiveType::Unit, {})))
{
// TODO: destructure tuples.
code << " -> " << IRNames::localVariable(*_function.experimentalReturnExpression()) << " ";
}
}
code << "{\n";
for (auto _statement: _function.body().statements())
{
IRGeneratorForStatements statementGenerator{m_context};
code << statementGenerator.generate(*_statement);
}
code << "}\n";
return code.str();
}
@@ -0,0 +1,72 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/experimental/codegen/IRGenerationContext.h>
#include <libsolidity/interface/DebugSettings.h>
#include <libsolidity/interface/OptimiserSettings.h>
#include <libsolidity/ast/ASTForward.h>
#include <libsolidity/ast/CallGraph.h>
#include <libsolidity/experimental/ast/TypeSystem.h>
#include <liblangutil/CharStreamProvider.h>
#include <liblangutil/DebugInfoSelection.h>
#include <liblangutil/EVMVersion.h>
#include <json/json.h>
#include <string>
namespace solidity::frontend::experimental
{
class Analysis;
class IRGenerator
{
public:
IRGenerator(
langutil::EVMVersion _evmVersion,
std::optional<uint8_t> _eofVersion,
RevertStrings /*_revertStrings*/,
std::map<std::string, unsigned> /*_sourceIndices*/,
langutil::DebugInfoSelection const& /*_debugInfoSelection*/,
langutil::CharStreamProvider const* /*_soliditySourceProvider*/,
Analysis const& _analysis
);
std::string run(
ContractDefinition const& _contract,
bytes const& _cborMetadata,
std::map<ContractDefinition const*, std::string_view const> const& _otherYulSources
);
std::string generate(ContractDefinition const& _contract);
std::string generate(FunctionDefinition const& _function, Type _type);
private:
langutil::EVMVersion const m_evmVersion;
std::optional<uint8_t> const m_eofVersion;
OptimiserSettings const m_optimiserSettings;
// langutil::DebugInfoSelection m_debugInfoSelection = {};
// langutil::CharStreamProvider const* m_soliditySourceProvider = nullptr;
TypeEnvironment m_env;
IRGenerationContext m_context;
};
}
@@ -0,0 +1,388 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/experimental/codegen/IRGeneratorForStatements.h>
#include <libsolidity/experimental/analysis/Analysis.h>
#include <libsolidity/experimental/analysis/TypeInference.h>
#include <libsolidity/experimental/analysis/TypeRegistration.h>
#include <libsolidity/experimental/ast/TypeSystemHelper.h>
#include <libyul/YulStack.h>
#include <libyul/AsmPrinter.h>
#include <libyul/AST.h>
#include <libyul/optimiser/ASTCopier.h>
#include <libsolidity/experimental/codegen/Common.h>
#include <range/v3/view/drop_last.hpp>
using namespace std;
using namespace solidity;
using namespace solidity::util;
using namespace solidity::frontend;
using namespace solidity::frontend::experimental;
using namespace std::string_literals;
std::string IRGeneratorForStatements::generate(ASTNode const& _node)
{
_node.accept(*this);
return m_code.str();
}
namespace {
struct CopyTranslate: public yul::ASTCopier
{
CopyTranslate(
IRGenerationContext const& _context,
yul::Dialect const& _dialect,
map<yul::Identifier const*, InlineAssemblyAnnotation::ExternalIdentifierInfo> _references
): m_context(_context), m_dialect(_dialect), m_references(std::move(_references)) {}
using ASTCopier::operator();
yul::Expression operator()(yul::Identifier const& _identifier) override
{
// The operator() function is only called in lvalue context. In rvalue context,
// only translate(yul::Identifier) is called.
if (m_references.count(&_identifier))
return translateReference(_identifier);
else
return ASTCopier::operator()(_identifier);
}
yul::YulString translateIdentifier(yul::YulString _name) override
{
if (m_dialect.builtin(_name))
return _name;
else
return yul::YulString{"usr$" + _name.str()};
}
yul::Identifier translate(yul::Identifier const& _identifier) override
{
if (!m_references.count(&_identifier))
return ASTCopier::translate(_identifier);
yul::Expression translated = translateReference(_identifier);
solAssert(holds_alternative<yul::Identifier>(translated));
return get<yul::Identifier>(std::move(translated));
}
private:
/// Translates a reference to a local variable, potentially including
/// a suffix. Might return a literal, which causes this to be invalid in
/// lvalue-context.
yul::Expression translateReference(yul::Identifier const& _identifier)
{
auto const& reference = m_references.at(&_identifier);
auto const varDecl = dynamic_cast<VariableDeclaration const*>(reference.declaration);
solAssert(varDecl, "External reference in inline assembly to something that is not a variable declaration.");
auto type = m_context.analysis.annotation<TypeInference>(*varDecl).type;
solAssert(type);
solAssert(m_context.env->typeEquals(*type, m_context.analysis.typeSystem().type(PrimitiveType::Word, {})));
string value = IRNames::localVariable(*varDecl);
return yul::Identifier{_identifier.debugData, yul::YulString{value}};
}
IRGenerationContext const& m_context;
yul::Dialect const& m_dialect;
map<yul::Identifier const*, InlineAssemblyAnnotation::ExternalIdentifierInfo> m_references;
};
}
bool IRGeneratorForStatements::visit(TupleExpression const& _tupleExpression)
{
std::vector<string> components;
for (auto const& component: _tupleExpression.components())
{
solUnimplementedAssert(component);
component->accept(*this);
components.emplace_back(IRNames::localVariable(*component));
}
solUnimplementedAssert(false, "No support for tuples.");
return false;
}
bool IRGeneratorForStatements::visit(InlineAssembly const& _assembly)
{
CopyTranslate bodyCopier{m_context, _assembly.dialect(), _assembly.annotation().externalReferences};
yul::Statement modified = bodyCopier(_assembly.operations());
solAssert(holds_alternative<yul::Block>(modified));
m_code << yul::AsmPrinter()(std::get<yul::Block>(modified)) << "\n";
return false;
}
bool IRGeneratorForStatements::visit(VariableDeclarationStatement const& _variableDeclarationStatement)
{
if (_variableDeclarationStatement.initialValue())
_variableDeclarationStatement.initialValue()->accept(*this);
solAssert(_variableDeclarationStatement.declarations().size() == 1, "multi variable declarations not supported");
VariableDeclaration const* variableDeclaration = _variableDeclarationStatement.declarations().front().get();
solAssert(variableDeclaration);
// TODO: check the type of the variable; register local variable; initialize
m_code << "let " << IRNames::localVariable(*variableDeclaration);
if (_variableDeclarationStatement.initialValue())
m_code << " := " << IRNames::localVariable(*_variableDeclarationStatement.initialValue());
m_code << "\n";
return false;
}
bool IRGeneratorForStatements::visit(ExpressionStatement const&)
{
return true;
}
bool IRGeneratorForStatements::visit(Identifier const& _identifier)
{
if (auto const* var = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration))
{
m_code << "let " << IRNames::localVariable(_identifier) << " := " << IRNames::localVariable(*var) << "\n";
}
else if (auto const* function = dynamic_cast<FunctionDefinition const*>(_identifier.annotation().referencedDeclaration))
solAssert(m_expressionDeclaration.emplace(&_identifier, function).second);
else if (auto const* typeClass = dynamic_cast<TypeClassDefinition const*>(_identifier.annotation().referencedDeclaration))
solAssert(m_expressionDeclaration.emplace(&_identifier, typeClass).second);
else if (auto const* typeDefinition = dynamic_cast<TypeDefinition const*>(_identifier.annotation().referencedDeclaration))
solAssert(m_expressionDeclaration.emplace(&_identifier, typeDefinition).second);
else
solAssert(false, "Unsupported Identifier");
return false;
}
void IRGeneratorForStatements::endVisit(Return const& _return)
{
if (Expression const* value = _return.expression())
{
solAssert(_return.annotation().function, "Invalid return.");
solAssert(_return.annotation().function->experimentalReturnExpression(), "Invalid return.");
m_code << IRNames::localVariable(*_return.annotation().function->experimentalReturnExpression()) << " := " << IRNames::localVariable(*value) << "\n";
}
m_code << "leave\n";
}
experimental::Type IRGeneratorForStatements::type(ASTNode const& _node) const
{
auto type = m_context.analysis.annotation<TypeInference>(_node).type;
solAssert(type);
return *type;
}
void IRGeneratorForStatements::endVisit(BinaryOperation const& _binaryOperation)
{
TypeSystemHelpers helper{m_context.analysis.typeSystem()};
Type leftType = type(_binaryOperation.leftExpression());
Type rightType = type(_binaryOperation.rightExpression());
Type resultType = type(_binaryOperation);
Type functionType = helper.functionType(helper.tupleType({leftType, rightType}), resultType);
auto [typeClass, memberName] = m_context.analysis.annotation<TypeInference>().operators.at(_binaryOperation.getOperator());
auto const& functionDefinition = resolveTypeClassFunction(typeClass, memberName, functionType);
// TODO: deduplicate with FunctionCall
// TODO: get around resolveRecursive by passing the environment further down?
functionType = m_context.env->resolveRecursive(functionType);
m_context.enqueueFunctionDefinition(&functionDefinition, functionType);
// TODO: account for return stack size
m_code << "let " << IRNames::localVariable(_binaryOperation) << " := " << IRNames::function(*m_context.env, functionDefinition, functionType) << "("
<< IRNames::localVariable(_binaryOperation.leftExpression()) << ", " << IRNames::localVariable(_binaryOperation.rightExpression()) << ")\n";
}
namespace
{
TypeRegistration::TypeClassInstantiations const& typeClassInstantiations(IRGenerationContext const& _context, TypeClass _class)
{
auto const* typeClassDeclaration = _context.analysis.typeSystem().typeClassDeclaration(_class);
if (typeClassDeclaration)
return _context.analysis.annotation<TypeRegistration>(*typeClassDeclaration).instantiations;
// TODO: better mechanism than fetching by name.
auto& instantiations = _context.analysis.annotation<TypeRegistration>().builtinClassInstantiations;
auto& builtinClassesByName = _context.analysis.annotation<TypeInference>().builtinClassesByName;
return instantiations.at(builtinClassesByName.at(_context.analysis.typeSystem().typeClassName(_class)));
}
}
FunctionDefinition const& IRGeneratorForStatements::resolveTypeClassFunction(TypeClass _class, string _name, Type _type)
{
TypeSystemHelpers helper{m_context.analysis.typeSystem()};
TypeEnvironment env = m_context.env->clone();
Type genericFunctionType = env.fresh(m_context.analysis.annotation<TypeInference>().typeClassFunctions.at(_class).at(_name));
auto typeVars = TypeEnvironmentHelpers{env}.typeVars(genericFunctionType);
solAssert(typeVars.size() == 1);
solAssert(env.unify(genericFunctionType, _type).empty());
auto typeClassInstantiation = get<0>(helper.destTypeConstant(env.resolve(typeVars.front())));
auto const& instantiations = typeClassInstantiations(m_context, _class);
TypeClassInstantiation const* instantiation = instantiations.at(typeClassInstantiation);
FunctionDefinition const* functionDefinition = nullptr;
for (auto const& node: instantiation->subNodes())
{
auto const* def = dynamic_cast<FunctionDefinition const*>(node.get());
solAssert(def);
if (def->name() == _name)
{
functionDefinition = def;
break;
}
}
solAssert(functionDefinition);
return *functionDefinition;
}
void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
{
TypeSystemHelpers helper{m_context.analysis.typeSystem()};
// TODO: avoid resolve?
auto expressionType = m_context.env->resolve(type(_memberAccess.expression()));
auto constructor = std::get<0>(helper.destTypeConstant(expressionType));
auto memberAccessType = type(_memberAccess);
// TODO: better mechanism
if (constructor == m_context.analysis.typeSystem().constructor(PrimitiveType::Bool))
{
if (_memberAccess.memberName() == "abs")
solAssert(m_expressionDeclaration.emplace(&_memberAccess, Builtins::ToBool).second);
else if (_memberAccess.memberName() == "rep")
solAssert(m_expressionDeclaration.emplace(&_memberAccess, Builtins::FromBool).second);
return;
}
auto const* declaration = m_context.analysis.typeSystem().constructorInfo(constructor).typeDeclaration;
solAssert(declaration);
if (auto const* typeClassDefinition = dynamic_cast<TypeClassDefinition const*>(declaration))
{
optional<TypeClass> typeClass = m_context.analysis.annotation<TypeInference>(*typeClassDefinition).typeClass;
solAssert(typeClass);
solAssert(m_expressionDeclaration.emplace(
&_memberAccess,
&resolveTypeClassFunction(*typeClass, _memberAccess.memberName(), memberAccessType)
).second);
}
else if (dynamic_cast<TypeDefinition const*>(declaration))
{
if (_memberAccess.memberName() == "abs" || _memberAccess.memberName() == "rep")
solAssert(m_expressionDeclaration.emplace(&_memberAccess, Builtins::Identity).second);
else
solAssert(false);
}
else
solAssert(false);
}
bool IRGeneratorForStatements::visit(ElementaryTypeNameExpression const&)
{
// TODO: is this always a no-op?
return false;
}
void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
{
Type functionType = type(_functionCall.expression());
auto declaration = m_expressionDeclaration.at(&_functionCall.expression());
if (auto builtin = get_if<Builtins>(&declaration))
{
switch(*builtin)
{
case Builtins::FromBool:
case Builtins::Identity:
solAssert(_functionCall.arguments().size() == 1);
m_code << "let " << IRNames::localVariable(_functionCall) << " := " << IRNames::localVariable(*_functionCall.arguments().front()) << "\n";
return;
case Builtins::ToBool:
solAssert(_functionCall.arguments().size() == 1);
m_code << "let " << IRNames::localVariable(_functionCall) << " := iszero(iszero(" << IRNames::localVariable(*_functionCall.arguments().front()) << "))\n";
return;
}
solAssert(false);
}
FunctionDefinition const* functionDefinition = dynamic_cast<FunctionDefinition const*>(get<Declaration const*>(declaration));
solAssert(functionDefinition);
// TODO: get around resolveRecursive by passing the environment further down?
functionType = m_context.env->resolveRecursive(functionType);
m_context.enqueueFunctionDefinition(functionDefinition, functionType);
// TODO: account for return stack size
m_code << "let " << IRNames::localVariable(_functionCall) << " := " << IRNames::function(*m_context.env, *functionDefinition, functionType) << "(";
auto const& arguments = _functionCall.arguments();
if (arguments.size() > 1)
for (auto arg: arguments | ranges::views::drop_last(1))
m_code << IRNames::localVariable(*arg) << ", ";
if (!arguments.empty())
m_code << IRNames::localVariable(*arguments.back());
m_code << ")\n";
}
bool IRGeneratorForStatements::visit(FunctionCall const&)
{
return true;
}
bool IRGeneratorForStatements::visit(Block const& _block)
{
m_code << "{\n";
solAssert(!_block.unchecked());
for (auto const& statement: _block.statements())
statement->accept(*this);
m_code << "}\n";
return false;
}
bool IRGeneratorForStatements::visit(IfStatement const& _ifStatement)
{
_ifStatement.condition().accept(*this);
if (_ifStatement.falseStatement())
{
m_code << "switch " << IRNames::localVariable(_ifStatement.condition()) << " {\n";
m_code << "case 0 {\n";
_ifStatement.falseStatement()->accept(*this);
m_code << "}\n";
m_code << "default {\n";
_ifStatement.trueStatement().accept(*this);
m_code << "}\n";
}
else
{
m_code << "if " << IRNames::localVariable(_ifStatement.condition()) << " {\n";
_ifStatement.trueStatement().accept(*this);
m_code << "}\n";
}
return false;
}
bool IRGeneratorForStatements::visit(Assignment const& _assignment)
{
_assignment.rightHandSide().accept(*this);
auto const* lhs = dynamic_cast<Identifier const*>(&_assignment.leftHandSide());
solAssert(lhs, "Can only assign to identifiers.");
auto const* lhsVar = dynamic_cast<VariableDeclaration const*>(lhs->annotation().referencedDeclaration);
solAssert(lhsVar, "Can only assign to identifiers referring to variables.");
m_code << IRNames::localVariable(*lhsVar) << " := " << IRNames::localVariable(_assignment.rightHandSide()) << "\n";
m_code << "let " << IRNames::localVariable(_assignment) << " := " << IRNames::localVariable(*lhsVar) << "\n";
return false;
}
bool IRGeneratorForStatements::visitNode(ASTNode const&)
{
solAssert(false, "Unsupported AST node during statement code generation.");
}
@@ -0,0 +1,71 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/experimental/codegen/IRGenerationContext.h>
#include <libsolidity/ast/ASTVisitor.h>
#include <functional>
#include <sstream>
namespace solidity::frontend::experimental
{
class Analysis;
class IRGeneratorForStatements: public ASTConstVisitor
{
public:
IRGeneratorForStatements(IRGenerationContext& _context): m_context(_context) {}
std::string generate(ASTNode const& _node);
private:
bool visit(ExpressionStatement const& _expressionStatement) override;
bool visit(Block const& _block) override;
bool visit(IfStatement const& _ifStatement) override;
bool visit(Assignment const& _assignment) override;
bool visit(Identifier const& _identifier) override;
bool visit(FunctionCall const& _functionCall) override;
void endVisit(FunctionCall const& _functionCall) override;
bool visit(ElementaryTypeNameExpression const& _elementaryTypeNameExpression) override;
bool visit(MemberAccess const&) override { return true; }
bool visit(TupleExpression const&) override;
void endVisit(MemberAccess const& _memberAccess) override;
bool visit(InlineAssembly const& _inlineAssembly) override;
bool visit(BinaryOperation const&) override { return true; }
void endVisit(BinaryOperation const& _binaryOperation) override;
bool visit(VariableDeclarationStatement const& _variableDeclarationStatement) override;
bool visit(Return const&) override { return true; }
void endVisit(Return const& _return) override;
/// Default visit will reject all AST nodes that are not explicitly supported.
bool visitNode(ASTNode const& _node) override;
IRGenerationContext& m_context;
std::stringstream m_code;
enum class Builtins
{
Identity,
FromBool,
ToBool
};
std::map<Expression const*, std::variant<Declaration const*, Builtins>> m_expressionDeclaration;
Type type(ASTNode const& _node) const;
FunctionDefinition const& resolveTypeClassFunction(TypeClass _class, std::string _name, Type _type);
};
}
@@ -0,0 +1,135 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/experimental/codegen/Common.h>
#include <libsolidity/experimental/codegen/IRGenerationContext.h>
#include <libsolidity/experimental/codegen/IRVariable.h>
#include <libsolidity/experimental/analysis/Analysis.h>
#include <libsolidity/experimental/analysis/TypeInference.h>
#include <libsolidity/ast/AST.h>
#include <libsolutil/StringUtils.h>
using namespace std;
using namespace solidity;
using namespace solidity::frontend::experimental;
using namespace solidity::util;
template<typename Node>
Type getType(IRGenerationContext const& _context, Node const& _node)
{
auto& annotation = _context.analysis.annotation<TypeInference>(_node);
solAssert(annotation.type);
return _context.env->resolve(*annotation.type);
}
namespace
{
size_t getTypeStackSlots(IRGenerationContext const& _context, Type _type)
{
}
}
IRVariable::IRVariable(IRGenerationContext const& _context, std::string _baseName, Type _type):
m_baseName(std::move(_baseName)), m_type(_type)
{
}
IRVariable::IRVariable(IRGenerationContext const& _context, VariableDeclaration const& _declaration):
IRVariable(_context, IRNames::localVariable(_declaration), getType(_context, _declaration))
{
}
IRVariable::IRVariable(IRGenerationContext const& _context, Expression const& _expression):
IRVariable(_context, IRNames::localVariable(_expression), getType(_context, _expression))
{
}
IRVariable IRVariable::part(string const& _name) const
{
for (auto const& [itemName, itemType]: m_type.stackItems())
if (itemName == _name)
{
solAssert(itemName.empty() || itemType, "");
return IRVariable{suffixedName(itemName), itemType ? *itemType : m_type};
}
solAssert(false, "Invalid stack item name: " + _name);
}
bool IRVariable::hasPart(std::string const& _name) const
{
for (auto const& [itemName, itemType]: m_type.stackItems())
if (itemName == _name)
{
solAssert(itemName.empty() || itemType, "");
return true;
}
return false;
}
vector<string> IRVariable::stackSlots() const
{
vector<string> result;
for (auto const& [itemName, itemType]: m_type.stackItems())
if (itemType)
{
solAssert(!itemName.empty(), "");
solAssert(m_type != *itemType, "");
result += IRVariable{suffixedName(itemName), *itemType}.stackSlots();
}
else
{
solAssert(itemName.empty(), "");
result.emplace_back(m_baseName);
}
return result;
}
string IRVariable::commaSeparatedList() const
{
return joinHumanReadable(stackSlots());
}
string IRVariable::commaSeparatedListPrefixed() const
{
return joinHumanReadablePrefixed(stackSlots());
}
string IRVariable::name() const
{
solAssert(m_type.sizeOnStack() == 1, "");
auto const& [itemName, type] = m_type.stackItems().front();
solAssert(!type, "Expected null type for name " + itemName);
return suffixedName(itemName);
}
IRVariable IRVariable::tupleComponent(size_t _i) const
{
solAssert(
m_type.category() == Type::Category::Tuple,
"Requested tuple component of non-tuple IR variable."
);
return part(IRNames::tupleComponent(_i));
}
string IRVariable::suffixedName(string const& _suffix) const
{
if (_suffix.empty())
return m_baseName;
else
return m_baseName + '_' + _suffix;
}
@@ -0,0 +1,85 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/experimental/ast/TypeSystem.h>
#include <optional>
#include <string>
#include <vector>
namespace solidity::frontend
{
class VariableDeclaration;
class Expression;
}
namespace solidity::frontend::experimental
{
class IRGenerationContext;
class IRVariable
{
public:
/// IR variable with explicit base name @a _baseName and type @a _type.
IRVariable(IRGenerationContext const& _analysis, std::string _baseName, Type _type);
/// IR variable referring to the declaration @a _decl.
IRVariable(IRGenerationContext const& _analysis, VariableDeclaration const& _decl);
/// IR variable referring to the expression @a _expr.
IRVariable(IRGenerationContext const& _analysis, Expression const& _expression);
/// @returns the name of the variable, if it occupies a single stack slot (otherwise throws).
std::string name() const;
/// @returns a comma-separated list of the stack slots of the variable.
std::string commaSeparatedList() const;
/// @returns a comma-separated list of the stack slots of the variable that is
/// prefixed with a comma, unless it is empty.
std::string commaSeparatedListPrefixed() const;
/// @returns an IRVariable referring to the tuple component @a _i of a tuple variable.
IRVariable tupleComponent(std::size_t _i) const;
/// @returns the type of the variable.
Type const& type() const { return m_type; }
/// @returns an IRVariable referring to the stack component @a _slot of the variable.
/// @a _slot must be among the stack slots in ``m_type.stackItems()``.
/// The returned IRVariable is itself typed with the type of the stack slot as defined
/// in ``m_type.stackItems()`` and may again occupy multiple stack slots.
IRVariable part(std::string const& _slot) const;
/// @returns true if variable contains @a _name component
/// @a _name name of the component that is being checked
bool hasPart(std::string const& _name) const;
/// @returns a vector containing the names of the stack slots of the variable.
std::vector<std::string> stackSlots() const;
private:
/// @returns a name consisting of the base name appended with an underscore and @æ _suffix,
/// unless @a _suffix is empty, in which case the base name itself is returned.
std::string suffixedName(std::string const& _suffix) const;
std::string m_baseName;
Type m_type;
};
}