diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index a0807f44f8bb..d90226d4e0dd 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -70,6 +70,7 @@ GRS_OBJS = \ rust/rust-ast-full-test.o \ rust/rust-session-manager.o \ rust/rust-resolution.o \ + rust/rust-scan.o \ rust/rust-compile.o \ $(END) # removed object files from here diff --git a/gcc/rust/analysis/rust-resolution.cc b/gcc/rust/analysis/rust-resolution.cc index 1c0fad1c5ece..6061dfa7db4b 100644 --- a/gcc/rust/analysis/rust-resolution.cc +++ b/gcc/rust/analysis/rust-resolution.cc @@ -19,7 +19,8 @@ namespace Rust { namespace Analysis { -TypeResolution::TypeResolution (AST::Crate &crate) : crate (crate) +TypeResolution::TypeResolution (AST::Crate &crate, TopLevelScan &toplevel) + : crate (crate), toplevel (toplevel) { typeScope.Push (); scope.Push (); @@ -50,9 +51,9 @@ TypeResolution::~TypeResolution () } bool -TypeResolution::ResolveNamesAndTypes (AST::Crate &crate) +TypeResolution::ResolveNamesAndTypes (AST::Crate &crate, TopLevelScan &toplevel) { - TypeResolution resolver (crate); + TypeResolution resolver (crate, toplevel); return resolver.go (); } @@ -65,6 +66,30 @@ TypeResolution::go () return true; } +bool +TypeResolution::typesAreCompatible (AST::Type *lhs, AST::Type *rhs, + Location locus) +{ + lhs->accept_vis (*this); + rhs->accept_vis (*this); + + auto rhsTypeStr = typeComparisonBuffer.back (); + typeComparisonBuffer.pop_back (); + auto lhsTypeStr = typeComparisonBuffer.back (); + typeComparisonBuffer.pop_back (); + + // FIXME this needs to handle the cases of an i8 going into an i32 which is + // compatible + if (lhsTypeStr.compare (rhsTypeStr)) + { + rust_error_at (locus, "E0308: expected: %s, found %s", + lhsTypeStr.c_str (), rhsTypeStr.c_str ()); + return false; + } + + return true; +} + void TypeResolution::visit (AST::Token &tok) {} @@ -124,18 +149,21 @@ TypeResolution::visit (AST::TypePathSegmentFunction &segment) void TypeResolution::visit (AST::TypePath &path) { - printf ("TypePath: %s\n", path.as_string ().c_str ()); + // this may not be robust enough for type comparisons but lets try it for now + typeComparisonBuffer.push_back (path.as_string ()); } void TypeResolution::visit (AST::QualifiedPathInExpression &path) { - printf ("QualifiedPathInExpression: %s\n", path.as_string ().c_str ()); + typeComparisonBuffer.push_back (path.as_string ()); } void TypeResolution::visit (AST::QualifiedPathInType &path) -{} +{ + typeComparisonBuffer.push_back (path.as_string ()); +} // rust-expr.h void @@ -245,7 +273,7 @@ TypeResolution::visit (AST::ArithmeticOrLogicalExpr &expr) // scope will require knowledge of the type // do the lhsType and the rhsType match - // TODO + typesAreCompatible (lhsType, rhsType, expr.right_expr->get_locus_slow ()); } void @@ -254,9 +282,7 @@ TypeResolution::visit (AST::ComparisonExpr &expr) void TypeResolution::visit (AST::LazyBooleanExpr &expr) -{ - printf ("LazyBooleanExpr: %s\n", expr.as_string ().c_str ()); -} +{} void TypeResolution::visit (AST::TypeCastExpr &expr) @@ -265,14 +291,41 @@ TypeResolution::visit (AST::TypeCastExpr &expr) void TypeResolution::visit (AST::AssignmentExpr &expr) { - printf ("AssignmentExpr: %s\n", expr.as_string ().c_str ()); + size_t before; + before = typeBuffer.size (); + expr.visit_lhs (*this); + if (typeBuffer.size () <= before) + { + rust_error_at (expr.locus, "unable to determine lhs type"); + return; + } + + auto lhsType = typeBuffer.back (); + typeBuffer.pop_back (); + + before = typeBuffer.size (); + expr.visit_rhs (*this); + if (typeBuffer.size () <= before) + { + rust_error_at (expr.locus, "unable to determine rhs type"); + return; + } + + auto rhsType = typeBuffer.back (); + // not poping because we will be checking they match and the + // scope will require knowledge of the type + + // do the lhsType and the rhsType match + if (!typesAreCompatible (lhsType, rhsType, + expr.right_expr->get_locus_slow ())) + return; + + // is the lhs mutable? } void TypeResolution::visit (AST::CompoundAssignmentExpr &expr) -{ - printf ("CompoundAssignmentExpr: %s\n", expr.as_string ().c_str ()); -} +{} void TypeResolution::visit (AST::GroupedExpr &expr) @@ -340,9 +393,13 @@ TypeResolution::visit (AST::EnumExprTuple &expr) void TypeResolution::visit (AST::EnumExprFieldless &expr) {} + void TypeResolution::visit (AST::CallExpr &expr) -{} +{ + printf ("CallExpr: %s\n", expr.as_string ().c_str ()); +} + void TypeResolution::visit (AST::MethodCallExpr &expr) {} @@ -481,18 +538,25 @@ TypeResolution::visit (AST::UseDeclaration &use_decl) void TypeResolution::visit (AST::Function &function) { + // always emit the function with return type in the event of nil return type + // its a marker for a void function scope.Insert (function.function_name, function.return_type.get ()); scope.Push (); - printf ("INSIDE FUNCTION: %s\n", function.function_name.c_str ()); - for (auto ¶m : function.function_params) { - printf ("FUNC PARAM: %s\n", param.as_string ().c_str ()); - } + auto before = letPatternBuffer.size (); + param.param_name->accept_vis (*this); + if (letPatternBuffer.size () <= before) + { + rust_error_at (param.locus, "failed to analyse parameter name"); + return; + } - // ensure return types - // TODO + auto paramName = letPatternBuffer.back (); + letPatternBuffer.pop_back (); + scope.Insert (paramName.variable_ident, param.type.get ()); + } // walk the expression body for (auto &stmt : function.function_body->statements) @@ -713,8 +777,11 @@ TypeResolution::visit (AST::LetStmt &stmt) if (stmt.has_type () && stmt.has_init_expr ()) { - auto declaredTyped = stmt.type.get (); - // TODO compare this type to the inferred type to ensure they match + if (!typesAreCompatible (stmt.type.get (), inferedType, + stmt.init_expr->get_locus_slow ())) + { + return; + } } else if (stmt.has_type () && !stmt.has_init_expr ()) { @@ -727,8 +794,7 @@ TypeResolution::visit (AST::LetStmt &stmt) // ensure the decl has the type set for compilation later on if (!stmt.has_type ()) { - // FIXME - // stmt.type = inferedType; + stmt.inferedType = inferedType; } // get all the names part of this declaration and add the types to the scope @@ -743,16 +809,12 @@ TypeResolution::visit (AST::LetStmt &stmt) void TypeResolution::visit (AST::ExprStmtWithoutBlock &stmt) { - printf ("ExprStmtWithoutBlock: %s\n", stmt.as_string ().c_str ()); stmt.expr->accept_vis (*this); } void TypeResolution::visit (AST::ExprStmtWithBlock &stmt) -{ - printf ("ExprStmtWithBlock: %s\n", stmt.as_string ().c_str ()); - stmt.expr->accept_vis (*this); -} +{} // rust-type.h void diff --git a/gcc/rust/analysis/rust-resolution.h b/gcc/rust/analysis/rust-resolution.h index 8a16b0df9e9a..44b5db528c57 100644 --- a/gcc/rust/analysis/rust-resolution.h +++ b/gcc/rust/analysis/rust-resolution.h @@ -3,6 +3,7 @@ #include "rust-system.h" #include "rust-ast-full.h" #include "rust-ast-visitor.h" +#include "rust-scan.h" #include "scope.h" namespace Rust { @@ -11,7 +12,7 @@ namespace Analysis { class TypeResolution : public AST::ASTVisitor { public: - static bool ResolveNamesAndTypes (AST::Crate &crate); + static bool ResolveNamesAndTypes (AST::Crate &crate, TopLevelScan &toplevel); ~TypeResolution (); @@ -221,16 +222,20 @@ class TypeResolution : public AST::ASTVisitor virtual void visit (AST::BareFunctionType &type); private: - TypeResolution (AST::Crate &crate); + TypeResolution (AST::Crate &crate, TopLevelScan &toplevel); bool go (); - Scope scope; - Scope typeScope; + bool typesAreCompatible (AST::Type *lhs, AST::Type *rhs, Location locus); + + Scope scope; + Scope typeScope; AST::Crate &crate; + TopLevelScan &toplevel; std::vector letPatternBuffer; std::vector typeBuffer; + std::vector typeComparisonBuffer; }; } // namespace Analysis diff --git a/gcc/rust/analysis/rust-scan.cc b/gcc/rust/analysis/rust-scan.cc new file mode 100644 index 000000000000..d579f0dfa075 --- /dev/null +++ b/gcc/rust/analysis/rust-scan.cc @@ -0,0 +1,571 @@ +#include "rust-scan.h" +#include "rust-diagnostics.h" + +namespace Rust { +namespace Analysis { + +TopLevelScan::TopLevelScan (AST::Crate &crate) : crate (crate) +{ + for (auto &item : crate.items) + item->accept_vis (*this); +} + +TopLevelScan::~TopLevelScan () {} + +void +TopLevelScan::visit (AST::Token &tok) +{} + +void +TopLevelScan::visit (AST::DelimTokenTree &delim_tok_tree) +{} + +void +TopLevelScan::visit (AST::AttrInputMetaItemContainer &input) +{} + +void +TopLevelScan::visit (AST::IdentifierExpr &ident_expr) +{} + +void +TopLevelScan::visit (AST::Lifetime &lifetime) +{} + +void +TopLevelScan::visit (AST::LifetimeParam &lifetime_param) +{} + +void +TopLevelScan::visit (AST::MacroInvocationSemi ¯o) +{} + +// rust-path.h +void +TopLevelScan::visit (AST::PathInExpression &path) +{} + +void +TopLevelScan::visit (AST::TypePathSegment &segment) +{} +void +TopLevelScan::visit (AST::TypePathSegmentGeneric &segment) +{} + +void +TopLevelScan::visit (AST::TypePathSegmentFunction &segment) +{} + +void +TopLevelScan::visit (AST::TypePath &path) +{} + +void +TopLevelScan::visit (AST::QualifiedPathInExpression &path) +{} + +void +TopLevelScan::visit (AST::QualifiedPathInType &path) +{} + +// rust-expr.h +void +TopLevelScan::visit (AST::LiteralExpr &expr) +{} + +void +TopLevelScan::visit (AST::AttrInputLiteral &attr_input) +{} + +void +TopLevelScan::visit (AST::MetaItemLitExpr &meta_item) +{} + +void +TopLevelScan::visit (AST::MetaItemPathLit &meta_item) +{} + +void +TopLevelScan::visit (AST::BorrowExpr &expr) +{} +void +TopLevelScan::visit (AST::DereferenceExpr &expr) +{} +void +TopLevelScan::visit (AST::ErrorPropagationExpr &expr) +{} +void +TopLevelScan::visit (AST::NegationExpr &expr) +{} + +void +TopLevelScan::visit (AST::ArithmeticOrLogicalExpr &expr) +{} + +void +TopLevelScan::visit (AST::ComparisonExpr &expr) +{} + +void +TopLevelScan::visit (AST::LazyBooleanExpr &expr) +{} + +void +TopLevelScan::visit (AST::TypeCastExpr &expr) +{} + +void +TopLevelScan::visit (AST::AssignmentExpr &expr) +{} + +void +TopLevelScan::visit (AST::CompoundAssignmentExpr &expr) +{} + +void +TopLevelScan::visit (AST::GroupedExpr &expr) +{} +// void TopLevelScan::visit(ArrayElems& elems) {} +void +TopLevelScan::visit (AST::ArrayElemsValues &elems) +{} +void +TopLevelScan::visit (AST::ArrayElemsCopied &elems) +{} +void +TopLevelScan::visit (AST::ArrayExpr &expr) +{} +void +TopLevelScan::visit (AST::ArrayIndexExpr &expr) +{} +void +TopLevelScan::visit (AST::TupleExpr &expr) +{} +void +TopLevelScan::visit (AST::TupleIndexExpr &expr) +{} +void +TopLevelScan::visit (AST::StructExprStruct &expr) +{} +// void TopLevelScan::visit(StructExprField& field) {} +void +TopLevelScan::visit (AST::StructExprFieldIdentifier &field) +{} +void +TopLevelScan::visit (AST::StructExprFieldIdentifierValue &field) +{} +void +TopLevelScan::visit (AST::StructExprFieldIndexValue &field) +{} +void +TopLevelScan::visit (AST::StructExprStructFields &expr) +{} +void +TopLevelScan::visit (AST::StructExprStructBase &expr) +{} +void +TopLevelScan::visit (AST::StructExprTuple &expr) +{} +void +TopLevelScan::visit (AST::StructExprUnit &expr) +{} +// void TopLevelScan::visit(EnumExprField& field) {} +void +TopLevelScan::visit (AST::EnumExprFieldIdentifier &field) +{} +void +TopLevelScan::visit (AST::EnumExprFieldIdentifierValue &field) +{} +void +TopLevelScan::visit (AST::EnumExprFieldIndexValue &field) +{} +void +TopLevelScan::visit (AST::EnumExprStruct &expr) +{} +void +TopLevelScan::visit (AST::EnumExprTuple &expr) +{} +void +TopLevelScan::visit (AST::EnumExprFieldless &expr) +{} + +void +TopLevelScan::visit (AST::CallExpr &expr) +{} + +void +TopLevelScan::visit (AST::MethodCallExpr &expr) +{} +void +TopLevelScan::visit (AST::FieldAccessExpr &expr) +{} +void +TopLevelScan::visit (AST::ClosureExprInner &expr) +{} +void +TopLevelScan::visit (AST::BlockExpr &expr) +{} +void +TopLevelScan::visit (AST::ClosureExprInnerTyped &expr) +{} +void +TopLevelScan::visit (AST::ContinueExpr &expr) +{} +void +TopLevelScan::visit (AST::BreakExpr &expr) +{} +void +TopLevelScan::visit (AST::RangeFromToExpr &expr) +{} +void +TopLevelScan::visit (AST::RangeFromExpr &expr) +{} +void +TopLevelScan::visit (AST::RangeToExpr &expr) +{} +void +TopLevelScan::visit (AST::RangeFullExpr &expr) +{} +void +TopLevelScan::visit (AST::RangeFromToInclExpr &expr) +{} +void +TopLevelScan::visit (AST::RangeToInclExpr &expr) +{} +void +TopLevelScan::visit (AST::ReturnExpr &expr) +{} +void +TopLevelScan::visit (AST::UnsafeBlockExpr &expr) +{} +void +TopLevelScan::visit (AST::LoopExpr &expr) +{} +void +TopLevelScan::visit (AST::WhileLoopExpr &expr) +{} +void +TopLevelScan::visit (AST::WhileLetLoopExpr &expr) +{} +void +TopLevelScan::visit (AST::ForLoopExpr &expr) +{} +void +TopLevelScan::visit (AST::IfExpr &expr) +{} +void +TopLevelScan::visit (AST::IfExprConseqElse &expr) +{} +void +TopLevelScan::visit (AST::IfExprConseqIf &expr) +{} +void +TopLevelScan::visit (AST::IfExprConseqIfLet &expr) +{} +void +TopLevelScan::visit (AST::IfLetExpr &expr) +{} +void +TopLevelScan::visit (AST::IfLetExprConseqElse &expr) +{} +void +TopLevelScan::visit (AST::IfLetExprConseqIf &expr) +{} +void +TopLevelScan::visit (AST::IfLetExprConseqIfLet &expr) +{} +// void TopLevelScan::visit(MatchCase& match_case) {} +void +TopLevelScan::visit (AST::MatchCaseBlockExpr &match_case) +{} +void +TopLevelScan::visit (AST::MatchCaseExpr &match_case) +{} +void +TopLevelScan::visit (AST::MatchExpr &expr) +{} +void +TopLevelScan::visit (AST::AwaitExpr &expr) +{} +void +TopLevelScan::visit (AST::AsyncBlockExpr &expr) +{} + +// rust-item.h +void +TopLevelScan::visit (AST::TypeParam ¶m) +{} +// void TopLevelScan::visit(WhereClauseItem& item) {} +void +TopLevelScan::visit (AST::LifetimeWhereClauseItem &item) +{} +void +TopLevelScan::visit (AST::TypeBoundWhereClauseItem &item) +{} +void +TopLevelScan::visit (AST::Method &method) +{} +void +TopLevelScan::visit (AST::ModuleBodied &module) +{} +void +TopLevelScan::visit (AST::ModuleNoBody &module) +{} +void +TopLevelScan::visit (AST::ExternCrate &crate) +{} +// void TopLevelScan::visit(UseTree& use_tree) {} +void +TopLevelScan::visit (AST::UseTreeGlob &use_tree) +{} +void +TopLevelScan::visit (AST::UseTreeList &use_tree) +{} +void +TopLevelScan::visit (AST::UseTreeRebind &use_tree) +{} +void +TopLevelScan::visit (AST::UseDeclaration &use_decl) +{} + +void +TopLevelScan::visit (AST::Function &function) +{ + functions[function.function_name] = &function; +} + +void +TopLevelScan::visit (AST::TypeAlias &type_alias) +{} +void +TopLevelScan::visit (AST::StructStruct &struct_item) +{} +void +TopLevelScan::visit (AST::TupleStruct &tuple_struct) +{} +void +TopLevelScan::visit (AST::EnumItem &item) +{} +void +TopLevelScan::visit (AST::EnumItemTuple &item) +{} +void +TopLevelScan::visit (AST::EnumItemStruct &item) +{} +void +TopLevelScan::visit (AST::EnumItemDiscriminant &item) +{} +void +TopLevelScan::visit (AST::Enum &enum_item) +{} +void +TopLevelScan::visit (AST::Union &union_item) +{} + +void +TopLevelScan::visit (AST::ConstantItem &const_item) +{} + +void +TopLevelScan::visit (AST::StaticItem &static_item) +{} +void +TopLevelScan::visit (AST::TraitItemFunc &item) +{} +void +TopLevelScan::visit (AST::TraitItemMethod &item) +{} +void +TopLevelScan::visit (AST::TraitItemConst &item) +{} +void +TopLevelScan::visit (AST::TraitItemType &item) +{} +void +TopLevelScan::visit (AST::Trait &trait) +{} +void +TopLevelScan::visit (AST::InherentImpl &impl) +{} +void +TopLevelScan::visit (AST::TraitImpl &impl) +{} +// void TopLevelScan::visit(ExternalItem& item) {} +void +TopLevelScan::visit (AST::ExternalStaticItem &item) +{} +void +TopLevelScan::visit (AST::ExternalFunctionItem &item) +{} +void +TopLevelScan::visit (AST::ExternBlock &block) +{} + +// rust-macro.h +void +TopLevelScan::visit (AST::MacroMatchFragment &match) +{} +void +TopLevelScan::visit (AST::MacroMatchRepetition &match) +{} +void +TopLevelScan::visit (AST::MacroMatcher &matcher) +{} +void +TopLevelScan::visit (AST::MacroRulesDefinition &rules_def) +{} +void +TopLevelScan::visit (AST::MacroInvocation ¯o_invoc) +{} +void +TopLevelScan::visit (AST::MetaItemPath &meta_item) +{} +void +TopLevelScan::visit (AST::MetaItemSeq &meta_item) +{} +void +TopLevelScan::visit (AST::MetaWord &meta_item) +{} +void +TopLevelScan::visit (AST::MetaNameValueStr &meta_item) +{} +void +TopLevelScan::visit (AST::MetaListPaths &meta_item) +{} +void +TopLevelScan::visit (AST::MetaListNameValueStr &meta_item) +{} + +// rust-pattern.h +void +TopLevelScan::visit (AST::LiteralPattern &pattern) +{} + +void +TopLevelScan::visit (AST::IdentifierPattern &pattern) +{} + +void +TopLevelScan::visit (AST::WildcardPattern &pattern) +{} +// void TopLevelScan::visit(RangePatternBound& bound) {} +void +TopLevelScan::visit (AST::RangePatternBoundLiteral &bound) +{} +void +TopLevelScan::visit (AST::RangePatternBoundPath &bound) +{} +void +TopLevelScan::visit (AST::RangePatternBoundQualPath &bound) +{} +void +TopLevelScan::visit (AST::RangePattern &pattern) +{} +void +TopLevelScan::visit (AST::ReferencePattern &pattern) +{} +// void TopLevelScan::visit(StructPatternField& field) {} +void +TopLevelScan::visit (AST::StructPatternFieldTuplePat &field) +{} +void +TopLevelScan::visit (AST::StructPatternFieldIdentPat &field) +{} +void +TopLevelScan::visit (AST::StructPatternFieldIdent &field) +{} +void +TopLevelScan::visit (AST::StructPattern &pattern) +{} +// void TopLevelScan::visit(TupleStructItems& tuple_items) {} +void +TopLevelScan::visit (AST::TupleStructItemsNoRange &tuple_items) +{} +void +TopLevelScan::visit (AST::TupleStructItemsRange &tuple_items) +{} +void +TopLevelScan::visit (AST::TupleStructPattern &pattern) +{} +// void TopLevelScan::visit(TuplePatternItems& tuple_items) {} +void +TopLevelScan::visit (AST::TuplePatternItemsMultiple &tuple_items) +{} +void +TopLevelScan::visit (AST::TuplePatternItemsRanged &tuple_items) +{} +void +TopLevelScan::visit (AST::TuplePattern &pattern) +{} +void +TopLevelScan::visit (AST::GroupedPattern &pattern) +{} +void +TopLevelScan::visit (AST::SlicePattern &pattern) +{} + +// rust-stmt.h +void +TopLevelScan::visit (AST::EmptyStmt &stmt) +{} + +void +TopLevelScan::visit (AST::LetStmt &stmt) +{} + +void +TopLevelScan::visit (AST::ExprStmtWithoutBlock &stmt) +{} + +void +TopLevelScan::visit (AST::ExprStmtWithBlock &stmt) +{} + +// rust-type.h +void +TopLevelScan::visit (AST::TraitBound &bound) +{} + +void +TopLevelScan::visit (AST::ImplTraitType &type) +{} + +void +TopLevelScan::visit (AST::TraitObjectType &type) +{} +void +TopLevelScan::visit (AST::ParenthesisedType &type) +{} +void +TopLevelScan::visit (AST::ImplTraitTypeOneBound &type) +{} +void +TopLevelScan::visit (AST::TraitObjectTypeOneBound &type) +{} +void +TopLevelScan::visit (AST::TupleType &type) +{} +void +TopLevelScan::visit (AST::NeverType &type) +{} +void +TopLevelScan::visit (AST::RawPointerType &type) +{} +void +TopLevelScan::visit (AST::ReferenceType &type) +{} +void +TopLevelScan::visit (AST::ArrayType &type) +{} +void +TopLevelScan::visit (AST::SliceType &type) +{} +void +TopLevelScan::visit (AST::InferredType &type) +{} +void +TopLevelScan::visit (AST::BareFunctionType &type) +{} + +} // namespace Analysis +} // namespace Rust diff --git a/gcc/rust/analysis/rust-scan.h b/gcc/rust/analysis/rust-scan.h new file mode 100644 index 000000000000..ccd41fed8dd0 --- /dev/null +++ b/gcc/rust/analysis/rust-scan.h @@ -0,0 +1,229 @@ +#pragma once + +#include "rust-system.h" +#include "rust-ast-full.h" +#include "rust-ast-visitor.h" +#include "scope.h" + +namespace Rust { +namespace Analysis { + +class TopLevelScan : public AST::ASTVisitor +{ +public: + TopLevelScan (AST::Crate &crate); + + ~TopLevelScan (); + + // visitor impl + // rust-ast.h + // virtual void visit(AttrInput& attr_input); + // virtual void visit(TokenTree& token_tree); + // virtual void visit(MacroMatch& macro_match); + virtual void visit (AST::Token &tok); + virtual void visit (AST::DelimTokenTree &delim_tok_tree); + virtual void visit (AST::AttrInputMetaItemContainer &input); + // virtual void visit(MetaItem& meta_item); + // virtual void vsit(Stmt& stmt); + // virtual void visit(Expr& expr); + virtual void visit (AST::IdentifierExpr &ident_expr); + // virtual void visit(Pattern& pattern); + // virtual void visit(Type& type); + // virtual void visit(TypeParamBound& type_param_bound); + virtual void visit (AST::Lifetime &lifetime); + // virtual void visit(GenericParam& generic_param); + virtual void visit (AST::LifetimeParam &lifetime_param); + // virtual void visit(TraitItem& trait_item); + // virtual void visit(InherentImplItem& inherent_impl_item); + // virtual void visit(TraitImplItem& trait_impl_item); + virtual void visit (AST::MacroInvocationSemi ¯o); + + // rust-path.h + virtual void visit (AST::PathInExpression &path); + virtual void visit (AST::TypePathSegment &segment); + virtual void visit (AST::TypePathSegmentGeneric &segment); + virtual void visit (AST::TypePathSegmentFunction &segment); + virtual void visit (AST::TypePath &path); + virtual void visit (AST::QualifiedPathInExpression &path); + virtual void visit (AST::QualifiedPathInType &path); + + // rust-expr.h + virtual void visit (AST::LiteralExpr &expr); + virtual void visit (AST::AttrInputLiteral &attr_input); + virtual void visit (AST::MetaItemLitExpr &meta_item); + virtual void visit (AST::MetaItemPathLit &meta_item); + virtual void visit (AST::BorrowExpr &expr); + virtual void visit (AST::DereferenceExpr &expr); + virtual void visit (AST::ErrorPropagationExpr &expr); + virtual void visit (AST::NegationExpr &expr); + virtual void visit (AST::ArithmeticOrLogicalExpr &expr); + virtual void visit (AST::ComparisonExpr &expr); + virtual void visit (AST::LazyBooleanExpr &expr); + virtual void visit (AST::TypeCastExpr &expr); + virtual void visit (AST::AssignmentExpr &expr); + virtual void visit (AST::CompoundAssignmentExpr &expr); + virtual void visit (AST::GroupedExpr &expr); + // virtual void visit(ArrayElems& elems); + virtual void visit (AST::ArrayElemsValues &elems); + virtual void visit (AST::ArrayElemsCopied &elems); + virtual void visit (AST::ArrayExpr &expr); + virtual void visit (AST::ArrayIndexExpr &expr); + virtual void visit (AST::TupleExpr &expr); + virtual void visit (AST::TupleIndexExpr &expr); + virtual void visit (AST::StructExprStruct &expr); + // virtual void visit(StructExprField& field); + virtual void visit (AST::StructExprFieldIdentifier &field); + virtual void visit (AST::StructExprFieldIdentifierValue &field); + virtual void visit (AST::StructExprFieldIndexValue &field); + virtual void visit (AST::StructExprStructFields &expr); + virtual void visit (AST::StructExprStructBase &expr); + virtual void visit (AST::StructExprTuple &expr); + virtual void visit (AST::StructExprUnit &expr); + // virtual void visit(EnumExprField& field); + virtual void visit (AST::EnumExprFieldIdentifier &field); + virtual void visit (AST::EnumExprFieldIdentifierValue &field); + virtual void visit (AST::EnumExprFieldIndexValue &field); + virtual void visit (AST::EnumExprStruct &expr); + virtual void visit (AST::EnumExprTuple &expr); + virtual void visit (AST::EnumExprFieldless &expr); + virtual void visit (AST::CallExpr &expr); + virtual void visit (AST::MethodCallExpr &expr); + virtual void visit (AST::FieldAccessExpr &expr); + virtual void visit (AST::ClosureExprInner &expr); + virtual void visit (AST::BlockExpr &expr); + virtual void visit (AST::ClosureExprInnerTyped &expr); + virtual void visit (AST::ContinueExpr &expr); + virtual void visit (AST::BreakExpr &expr); + virtual void visit (AST::RangeFromToExpr &expr); + virtual void visit (AST::RangeFromExpr &expr); + virtual void visit (AST::RangeToExpr &expr); + virtual void visit (AST::RangeFullExpr &expr); + virtual void visit (AST::RangeFromToInclExpr &expr); + virtual void visit (AST::RangeToInclExpr &expr); + virtual void visit (AST::ReturnExpr &expr); + virtual void visit (AST::UnsafeBlockExpr &expr); + virtual void visit (AST::LoopExpr &expr); + virtual void visit (AST::WhileLoopExpr &expr); + virtual void visit (AST::WhileLetLoopExpr &expr); + virtual void visit (AST::ForLoopExpr &expr); + virtual void visit (AST::IfExpr &expr); + virtual void visit (AST::IfExprConseqElse &expr); + virtual void visit (AST::IfExprConseqIf &expr); + virtual void visit (AST::IfExprConseqIfLet &expr); + virtual void visit (AST::IfLetExpr &expr); + virtual void visit (AST::IfLetExprConseqElse &expr); + virtual void visit (AST::IfLetExprConseqIf &expr); + virtual void visit (AST::IfLetExprConseqIfLet &expr); + // virtual void visit(MatchCase& match_case); + virtual void visit (AST::MatchCaseBlockExpr &match_case); + virtual void visit (AST::MatchCaseExpr &match_case); + virtual void visit (AST::MatchExpr &expr); + virtual void visit (AST::AwaitExpr &expr); + virtual void visit (AST::AsyncBlockExpr &expr); + + // rust-item.h + virtual void visit (AST::TypeParam ¶m); + // virtual void visit(WhereClauseItem& item); + virtual void visit (AST::LifetimeWhereClauseItem &item); + virtual void visit (AST::TypeBoundWhereClauseItem &item); + virtual void visit (AST::Method &method); + virtual void visit (AST::ModuleBodied &module); + virtual void visit (AST::ModuleNoBody &module); + virtual void visit (AST::ExternCrate &crate); + // virtual void visit(UseTree& use_tree); + virtual void visit (AST::UseTreeGlob &use_tree); + virtual void visit (AST::UseTreeList &use_tree); + virtual void visit (AST::UseTreeRebind &use_tree); + virtual void visit (AST::UseDeclaration &use_decl); + virtual void visit (AST::Function &function); + virtual void visit (AST::TypeAlias &type_alias); + virtual void visit (AST::StructStruct &struct_item); + virtual void visit (AST::TupleStruct &tuple_struct); + virtual void visit (AST::EnumItem &item); + virtual void visit (AST::EnumItemTuple &item); + virtual void visit (AST::EnumItemStruct &item); + virtual void visit (AST::EnumItemDiscriminant &item); + virtual void visit (AST::Enum &enum_item); + virtual void visit (AST::Union &union_item); + virtual void visit (AST::ConstantItem &const_item); + virtual void visit (AST::StaticItem &static_item); + virtual void visit (AST::TraitItemFunc &item); + virtual void visit (AST::TraitItemMethod &item); + virtual void visit (AST::TraitItemConst &item); + virtual void visit (AST::TraitItemType &item); + virtual void visit (AST::Trait &trait); + virtual void visit (AST::InherentImpl &impl); + virtual void visit (AST::TraitImpl &impl); + // virtual void visit(ExternalItem& item); + virtual void visit (AST::ExternalStaticItem &item); + virtual void visit (AST::ExternalFunctionItem &item); + virtual void visit (AST::ExternBlock &block); + + // rust-macro.h + virtual void visit (AST::MacroMatchFragment &match); + virtual void visit (AST::MacroMatchRepetition &match); + virtual void visit (AST::MacroMatcher &matcher); + virtual void visit (AST::MacroRulesDefinition &rules_def); + virtual void visit (AST::MacroInvocation ¯o_invoc); + virtual void visit (AST::MetaItemPath &meta_item); + virtual void visit (AST::MetaItemSeq &meta_item); + virtual void visit (AST::MetaWord &meta_item); + virtual void visit (AST::MetaNameValueStr &meta_item); + virtual void visit (AST::MetaListPaths &meta_item); + virtual void visit (AST::MetaListNameValueStr &meta_item); + + // rust-pattern.h + virtual void visit (AST::LiteralPattern &pattern); + virtual void visit (AST::IdentifierPattern &pattern); + virtual void visit (AST::WildcardPattern &pattern); + // virtual void visit(RangePatternBound& bound); + virtual void visit (AST::RangePatternBoundLiteral &bound); + virtual void visit (AST::RangePatternBoundPath &bound); + virtual void visit (AST::RangePatternBoundQualPath &bound); + virtual void visit (AST::RangePattern &pattern); + virtual void visit (AST::ReferencePattern &pattern); + // virtual void visit(StructPatternField& field); + virtual void visit (AST::StructPatternFieldTuplePat &field); + virtual void visit (AST::StructPatternFieldIdentPat &field); + virtual void visit (AST::StructPatternFieldIdent &field); + virtual void visit (AST::StructPattern &pattern); + // virtual void visit(TupleStructItems& tuple_items); + virtual void visit (AST::TupleStructItemsNoRange &tuple_items); + virtual void visit (AST::TupleStructItemsRange &tuple_items); + virtual void visit (AST::TupleStructPattern &pattern); + // virtual void visit(TuplePatternItems& tuple_items); + virtual void visit (AST::TuplePatternItemsMultiple &tuple_items); + virtual void visit (AST::TuplePatternItemsRanged &tuple_items); + virtual void visit (AST::TuplePattern &pattern); + virtual void visit (AST::GroupedPattern &pattern); + virtual void visit (AST::SlicePattern &pattern); + + // rust-stmt.h + virtual void visit (AST::EmptyStmt &stmt); + virtual void visit (AST::LetStmt &stmt); + virtual void visit (AST::ExprStmtWithoutBlock &stmt); + virtual void visit (AST::ExprStmtWithBlock &stmt); + + // rust-type.h + virtual void visit (AST::TraitBound &bound); + virtual void visit (AST::ImplTraitType &type); + virtual void visit (AST::TraitObjectType &type); + virtual void visit (AST::ParenthesisedType &type); + virtual void visit (AST::ImplTraitTypeOneBound &type); + virtual void visit (AST::TraitObjectTypeOneBound &type); + virtual void visit (AST::TupleType &type); + virtual void visit (AST::NeverType &type); + virtual void visit (AST::RawPointerType &type); + virtual void visit (AST::ReferenceType &type); + virtual void visit (AST::ArrayType &type); + virtual void visit (AST::SliceType &type); + virtual void visit (AST::InferredType &type); + virtual void visit (AST::BareFunctionType &type); + +private: + std::map functions; + AST::Crate &crate; +}; + +} // namespace Analysis +} // namespace Rust diff --git a/gcc/rust/analysis/scope.h b/gcc/rust/analysis/scope.h index 0374251c068d..231e667fae10 100644 --- a/gcc/rust/analysis/scope.h +++ b/gcc/rust/analysis/scope.h @@ -6,14 +6,14 @@ namespace Rust { namespace Analysis { -class Scope +template class Scope { public: Scope () : scopeStack () {} ~Scope () {} - bool Insert (std::string key, AST::Type *val) + bool Insert (std::string key, T val) { if (scopeStack.back ().find (key) != scopeStack.back ().end ()) { @@ -24,7 +24,7 @@ class Scope return true; } - bool Lookup (std::string key, AST::Type **result) + bool Lookup (std::string key, T *result) { for (auto it = scopeStack.rbegin (); it != scopeStack.rend (); ++it) { @@ -40,7 +40,7 @@ class Scope void Push () { scopeStack.push_back ({}); } - std ::map Pop () + std ::map Pop () { auto toplevel = scopeStack.back (); scopeStack.pop_back (); @@ -48,7 +48,7 @@ class Scope } private: - std::vector > scopeStack; + std::vector > scopeStack; }; } // namespace Analysis diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index 45cb14c6b4fb..e0f95363a21d 100644 --- a/gcc/rust/ast/rust-expr.h +++ b/gcc/rust/ast/rust-expr.h @@ -568,7 +568,6 @@ class ArithmeticOrLogicalExpr : public OperatorExpr RIGHT_SHIFT // std::ops::Shr }; -private: // Note: overloading trait specified in comments ExprType expr_type; @@ -880,10 +879,10 @@ class TypeCastExpr : public OperatorExpr // Binary assignment expression. class AssignmentExpr : public OperatorExpr { +public: // Expr* right_expr; ::std::unique_ptr right_expr; -public: /*~AssignmentExpr() { delete right_expr; }*/ @@ -941,6 +940,10 @@ class AssignmentExpr : public OperatorExpr virtual void accept_vis (ASTVisitor &vis) OVERRIDE; + void visit_lhs (ASTVisitor &vis) { main_or_left_expr->accept_vis (vis); } + + void visit_rhs (ASTVisitor &vis) { right_expr->accept_vis (vis); } + protected: // Use covariance to implement clone function as returning this object rather // than base @@ -2523,6 +2526,7 @@ class EnumExprFieldless : public EnumVariantExpr // Function call expression AST node class CallExpr : public ExprWithoutBlock { +public: // Expr* function; ::std::unique_ptr function; //::std::vector params; // inlined form of CallParams @@ -2530,7 +2534,6 @@ class CallExpr : public ExprWithoutBlock Location locus; -public: /*~CallExpr() { delete function; }*/ diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 45f58427db25..cfcb5ee71b08 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -496,7 +496,7 @@ struct FunctionQualifiers // A function parameter struct FunctionParam { -private: +public: // Pattern* param_name; ::std::unique_ptr param_name; // Type type; @@ -504,7 +504,6 @@ struct FunctionParam Location locus; -public: FunctionParam (::std::unique_ptr param_name, ::std::unique_ptr param_type, Location locus) : param_name (::std::move (param_name)), type (::std::move (param_type)), diff --git a/gcc/rust/ast/rust-stmt.h b/gcc/rust/ast/rust-stmt.h index f1b09a72a097..f19ba2c86a56 100644 --- a/gcc/rust/ast/rust-stmt.h +++ b/gcc/rust/ast/rust-stmt.h @@ -63,6 +63,8 @@ class LetStmt : public Stmt Location locus; + Type *inferedType; + // Returns whether let statement has outer attributes. inline bool has_outer_attrs () const { return !outer_attrs.empty (); } diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index 38217f0a500d..19f50252b4d2 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -361,7 +361,6 @@ Compilation::visit (AST::Function &function) printf ("FUNC PARAM: %s\n", param.as_string ().c_str ()); // TODO } - if (parameters.size () != function.function_params.size ()) { rust_error_at (function.locus, diff --git a/gcc/rust/backend/rust-compile.h b/gcc/rust/backend/rust-compile.h index 85658477bc54..77826f9f9c5c 100644 --- a/gcc/rust/backend/rust-compile.h +++ b/gcc/rust/backend/rust-compile.h @@ -226,7 +226,7 @@ class Compilation : public AST::ASTVisitor bool go (); - Analysis::Scope scope; + Analysis::Scope scope; AST::Crate &crate; Backend *backend; diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index 44866faa045a..a0b9e5ab8f16 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -5,6 +5,7 @@ #include "rust-lex.h" #include "rust-parse.h" +#include "rust-scan.h" #include "rust-resolution.h" #include "rust-compile.h" @@ -700,7 +701,8 @@ void Session::name_resolution (AST::Crate &crate) { fprintf (stderr, "started name resolution\n"); - Analysis::TypeResolution::ResolveNamesAndTypes (crate); + Analysis::TopLevelScan toplevel (crate); + Analysis::TypeResolution::ResolveNamesAndTypes (crate, toplevel); fprintf (stderr, "finished name resolution\n"); }