diff options
author | Philip Herron <herron.philip@googlemail.com> | 2020-06-13 20:28:28 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2020-11-28 21:13:16 +0000 |
commit | 15280699c13f799f25bfc95656415c0ec2c1b901 (patch) | |
tree | ab67df319d15d12a9c626a03a0cd9827b54c2a77 | |
parent | 84ed897a9b5dcb28cc0cf8a85296e3628b2f0e17 (diff) | |
download | gcc-15280699c13f799f25bfc95656415c0ec2c1b901.zip gcc-15280699c13f799f25bfc95656415c0ec2c1b901.tar.gz gcc-15280699c13f799f25bfc95656415c0ec2c1b901.tar.bz2 |
Struct record compilation and type resolution
-rw-r--r-- | gcc/rust/analysis/rust-type-resolution.cc | 17 | ||||
-rw-r--r-- | gcc/rust/analysis/rust-type-resolution.h | 2 | ||||
-rw-r--r-- | gcc/rust/ast/rust-item.h | 8 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile.cc | 35 |
4 files changed, 55 insertions, 7 deletions
diff --git a/gcc/rust/analysis/rust-type-resolution.cc b/gcc/rust/analysis/rust-type-resolution.cc index 03f3d27..b8baabb 100644 --- a/gcc/rust/analysis/rust-type-resolution.cc +++ b/gcc/rust/analysis/rust-type-resolution.cc @@ -690,9 +690,24 @@ TypeResolution::visit (AST::Function &function) void TypeResolution::visit (AST::TypeAlias &type_alias) {} + void TypeResolution::visit (AST::StructStruct &struct_item) -{} +{ + for (auto &field : struct_item.fields) + { + if (!isTypeInScope (field.field_type.get (), + Linemap::unknown_location ())) + { + rust_fatal_error (Linemap::unknown_location (), + "unknown type in struct field"); + return; + } + } + + structsPerBlock.Insert (struct_item.struct_name, &struct_item); +} + void TypeResolution::visit (AST::TupleStruct &tuple_struct) {} diff --git a/gcc/rust/analysis/rust-type-resolution.h b/gcc/rust/analysis/rust-type-resolution.h index 1155267..e9fec83 100644 --- a/gcc/rust/analysis/rust-type-resolution.h +++ b/gcc/rust/analysis/rust-type-resolution.h @@ -224,6 +224,8 @@ private: bool isTypeInScope (AST::Type *type, Location locus); Scope<AST::Function *> functionScope; + Scope<AST::LetStmt *> localsPerBlock; + Scope<AST::StructStruct *> structsPerBlock; }; } // namespace Analysis diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index e8eb215..07d9a6c 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -1577,7 +1577,7 @@ protected: // Rust base struct declaration AST node - abstract base class class Struct : public VisItem { -protected: +public: // protected to enable access by derived classes - allows better as_string Identifier struct_name; @@ -1590,7 +1590,6 @@ protected: Location locus; -public: // Returns whether struct has generic parameters. inline bool has_generics () const { return !generic_params.empty (); } @@ -1653,7 +1652,7 @@ protected: // A single field in a struct struct StructField { -private: +public: // bool has_outer_attributes; ::std::vector<Attribute> outer_attrs; @@ -1666,7 +1665,6 @@ private: // should this store location info? -public: // Returns whether struct field has any outer attributes. inline bool has_outer_attributes () const { return !outer_attrs.empty (); } @@ -1725,10 +1723,10 @@ public: // Rust struct declaration with true struct type AST node class StructStruct : public Struct { +public: ::std::vector<StructField> fields; bool is_unit; -public: ::std::string as_string () const; // Mega-constructor with all possible fields diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index 2630c0f..3435f8f 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -1004,9 +1004,42 @@ Compilation::visit (AST::Function &function) void Compilation::visit (AST::TypeAlias &type_alias) {} + void Compilation::visit (AST::StructStruct &struct_item) -{} +{ + std::vector<Backend::Btyped_identifier> fields; + for (auto &field : struct_item.fields) + { + translatedType = NULL; + field.field_type->accept_vis (*this); + if (translatedType == NULL) + { + rust_fatal_error ( + struct_item.locus /* StructField is mi sing locus */, + "failed to compile struct field"); + return; + } + + fields.push_back (Backend::Btyped_identifier ( + field.field_name, translatedType, + struct_item.locus /* StructField is mi sing locus */)); + } + + auto compiledStruct + = backend->placeholder_struct_type (struct_item.struct_name, + struct_item.locus); + bool ok = backend->set_placeholder_struct_type (compiledStruct, fields); + if (!ok) + { + rust_fatal_error (struct_item.locus, "failed to compile struct"); + return; + } + + type_decls.push_back (compiledStruct); + scope.InsertType (struct_item.struct_name, compiledStruct); +} + void Compilation::visit (AST::TupleStruct &tuple_struct) {} |