diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ast/ast.cpp | 2 | ||||
| -rw-r--r-- | src/ast/ast.hpp | 5 | ||||
| -rw-r--r-- | src/ast/expr.cpp | 21 | ||||
| -rw-r--r-- | src/ast/expr.hpp | 16 | ||||
| -rw-r--r-- | src/ast/path.cpp | 27 | ||||
| -rw-r--r-- | src/ast/path.hpp | 3 | ||||
| -rw-r--r-- | src/convert/resolve.cpp | 17 | ||||
| -rw-r--r-- | src/types.cpp | 15 | 
8 files changed, 99 insertions, 7 deletions
| diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index cf88ed26..4bafb357 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -230,6 +230,7 @@ SERIALISE_TYPE(Module::, "AST_Module", {      s << m_imports;
      s << m_type_aliases;
 +    s << m_traits;
      s << m_enums;
      s << m_structs;
      s << m_statics;
 @@ -246,6 +247,7 @@ SERIALISE_TYPE(Module::, "AST_Module", {      s.item(m_imports);
      s.item(m_type_aliases);
 +    s.item(m_traits);
      s.item(m_enums);
      s.item(m_structs);
      s.item(m_statics);
 diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index 31cab495..4cc8f807 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -408,7 +408,10 @@ public:      Module& get_root_module(const ::std::string& name);
      const Module& get_root_module(const ::std::string& name) const;
 -    
 +
 +    ::std::map< ::std::string, ExternCrate>& extern_crates() { return m_extern_crates; }   
 +    const ::std::map< ::std::string, ExternCrate>& extern_crates() const { return m_extern_crates; }   
 + 
      void load_extern_crate(::std::string name);
      void iterate_functions( fcn_visitor_t* visitor );
 diff --git a/src/ast/expr.cpp b/src/ast/expr.cpp index 92a189e1..7cde198f 100644 --- a/src/ast/expr.cpp +++ b/src/ast/expr.cpp @@ -29,10 +29,27 @@ SERIALISE_TYPE(Expr::, "Expr", {      DEBUG("tag = " << tag);      ExprNode*   ptr = nullptr; -         if(tag == "ExprNode_Block")    ptr = new ExprNode_Block; -    else if(tag == "ExprNode_Macro")    ptr = new ExprNode_Macro; +    #define _(x)    if(tag == #x) ptr = new x; +         _(ExprNode_Block) +    else _(ExprNode_Macro) +    else _(ExprNode_Return) +    else _(ExprNode_LetBinding) +    else _(ExprNode_Assign) +    else _(ExprNode_CallPath) +    else _(ExprNode_CallMethod) +    else _(ExprNode_CallObject) +    else _(ExprNode_Match) +    else _(ExprNode_If) +    else _(ExprNode_Integer) +    else _(ExprNode_StructLiteral) +    else _(ExprNode_Tuple) +    else _(ExprNode_NamedValue) +    else _(ExprNode_Field) +    else _(ExprNode_CallPath) +    else _(ExprNode_BinOp)      else          throw ::std::runtime_error("Unknown node type " + tag); +    #undef _      ptr->deserialise(d);      d.end_object(tag.c_str()); diff --git a/src/ast/expr.hpp b/src/ast/expr.hpp index f6de9b99..d7cdb791 100644 --- a/src/ast/expr.hpp +++ b/src/ast/expr.hpp @@ -68,6 +68,7 @@ struct ExprNode_Return:  {      unique_ptr<ExprNode>    m_value; +    ExprNode_Return() {}      ExprNode_Return(unique_ptr<ExprNode>&& value):          m_value( move(value) )      { @@ -83,6 +84,7 @@ struct ExprNode_LetBinding:      Pattern m_pat;      unique_ptr<ExprNode>    m_value; +    ExprNode_LetBinding() {}      ExprNode_LetBinding(Pattern pat, unique_ptr<ExprNode>&& value):          m_pat( move(pat) ),          m_value( move(value) ) @@ -99,6 +101,7 @@ struct ExprNode_Assign:      unique_ptr<ExprNode>    m_slot;      unique_ptr<ExprNode>    m_value; +    ExprNode_Assign() {}      ExprNode_Assign(unique_ptr<ExprNode>&& slot, unique_ptr<ExprNode>&& value):          m_slot( move(slot) ),          m_value( move(value) ) @@ -115,6 +118,7 @@ struct ExprNode_CallPath:      Path    m_path;      ::std::vector<unique_ptr<ExprNode>> m_args; +    ExprNode_CallPath() {}      ExprNode_CallPath(Path&& path, ::std::vector<unique_ptr<ExprNode>>&& args):          m_path( move(path) ),          m_args( move(args) ) @@ -132,6 +136,7 @@ struct ExprNode_CallMethod:      PathNode    m_method;      ::std::vector<unique_ptr<ExprNode>> m_args; +    ExprNode_CallMethod() {}      ExprNode_CallMethod(unique_ptr<ExprNode>&& obj, PathNode&& method, ::std::vector<unique_ptr<ExprNode>>&& args):          m_val( move(obj) ),          m_method( move(method) ), @@ -150,6 +155,7 @@ struct ExprNode_CallObject:      unique_ptr<ExprNode>    m_val;      ::std::vector<unique_ptr<ExprNode>> m_args; +    ExprNode_CallObject() {}      ExprNode_CallObject(unique_ptr<ExprNode>&& val, ::std::vector< unique_ptr<ExprNode> >&& args):          m_val( move(val) ),          m_args( move(args) ) @@ -167,6 +173,7 @@ struct ExprNode_Match:      unique_ptr<ExprNode>    m_val;      arm_t   m_arms; +    ExprNode_Match() {}      ExprNode_Match(unique_ptr<ExprNode>&& val, arm_t&& arms):          m_val( ::std::move(val) ),          m_arms( ::std::move(arms) ) @@ -184,6 +191,7 @@ struct ExprNode_If:      unique_ptr<ExprNode>    m_true;      unique_ptr<ExprNode>    m_false; +    ExprNode_If() {}      ExprNode_If(unique_ptr<ExprNode>&& cond, unique_ptr<ExprNode>&& true_code, unique_ptr<ExprNode>&& false_code):          m_cond( ::std::move(cond) ),          m_true( ::std::move(true_code) ), @@ -201,6 +209,7 @@ struct ExprNode_Integer:      enum eCoreType  m_datatype;      uint64_t    m_value; +    ExprNode_Integer() {}      ExprNode_Integer(uint64_t value, enum eCoreType datatype):          m_datatype(datatype),          m_value(value) @@ -220,6 +229,7 @@ struct ExprNode_StructLiteral:      unique_ptr<ExprNode>    m_base_value;      t_values    m_values; +    ExprNode_StructLiteral() {}      ExprNode_StructLiteral(Path path, unique_ptr<ExprNode>&& base_value, t_values&& values ):          m_path( move(path) ),          m_base_value( move(base_value) ), @@ -236,6 +246,7 @@ struct ExprNode_Tuple:  {      ::std::vector< unique_ptr<ExprNode> >   m_values; +    ExprNode_Tuple() {}      ExprNode_Tuple(::std::vector< unique_ptr<ExprNode> > vals):          m_values( ::std::move(vals) )      {} @@ -249,6 +260,8 @@ struct ExprNode_NamedValue:      public ExprNode  {      Path    m_path; + +    ExprNode_NamedValue() {}      ExprNode_NamedValue(Path&& path):          m_path( ::std::move(path) )      { @@ -264,6 +277,7 @@ struct ExprNode_Field:      ::std::unique_ptr<ExprNode> m_obj;      ::std::string   m_name; +    ExprNode_Field() {}      ExprNode_Field(::std::unique_ptr<ExprNode>&& obj, ::std::string&& name):          m_obj( ::std::move(obj) ),          m_name( ::std::move(name) ) @@ -281,6 +295,7 @@ struct ExprNode_Cast:      unique_ptr<ExprNode>    m_value;      TypeRef m_type; +    ExprNode_Cast() {}      ExprNode_Cast(unique_ptr<ExprNode>&& value, TypeRef&& dst_type):          m_value( move(value) ),          m_type( move(dst_type) ) @@ -311,6 +326,7 @@ struct ExprNode_BinOp:      ::std::unique_ptr<ExprNode> m_left;      ::std::unique_ptr<ExprNode> m_right; +    ExprNode_BinOp() {}      ExprNode_BinOp(Type type, ::std::unique_ptr<ExprNode> left, ::std::unique_ptr<ExprNode> right):          m_type(type),          m_left( ::std::move(left) ), diff --git a/src/ast/path.cpp b/src/ast/path.cpp index 80c9bbdb..56d06eaf 100644 --- a/src/ast/path.cpp +++ b/src/ast/path.cpp @@ -102,8 +102,8 @@ void Path::resolve(const Crate& root_crate)              if( it != items.end() )              {                  DEBUG("Type alias <"<<it->data.params()<<"> " << it->data.type()); -                if( node.args().size() != it->data.params().size() ) -                    throw ParseError::Generic("Param count mismatch when referencing type alias"); +                //if( node.args().size() != it->data.params().size() ) +                //    throw ParseError::Generic("Param count mismatch when referencing type alias");                  // Make a copy of the path, replace params with it, then replace *this?                  // - Maybe leave that up to other code?                  if( is_last ) { @@ -132,6 +132,27 @@ void Path::resolve(const Crate& root_crate)                  }              }          } + +        // - Traits +        { +            auto& items = mod->traits(); +            auto it = find_named(items, node.name()); +            if( it != items.end() ) +            { +                DEBUG("Found trait"); +                if( is_last ) { +                    m_binding_type = TRAIT; +                    m_binding.trait = &it->data; +                    return; +                } +                else if( is_sec_last ) { +                    throw ParseError::Todo("Path::resolve() trait method"); +                } +                else { +                    throw ParseError::Generic("Import of trait, too many extra nodes"); +                } +            } +        }          // - Structs          {              auto& items = mod->structs(); @@ -272,7 +293,7 @@ Path& Path::operator+=(const Path& other)          os << "Path({" << path.m_nodes << "})";          break;      case Path::ABSOLUTE: -        os << "Path(TagAbsolute, {" << path.m_nodes << "})"; +        os << "Path(TagAbsolute, \""<<path.m_crate<<"\", {" << path.m_nodes << "})";          break;      case Path::LOCAL:          os << "Path(TagLocal, " << path.m_nodes[0].name() << ")"; diff --git a/src/ast/path.hpp b/src/ast/path.hpp index 6370580f..7b47bc37 100644 --- a/src/ast/path.hpp +++ b/src/ast/path.hpp @@ -20,6 +20,7 @@ class Module;  class TypeAlias;  class Enum;  class Struct; +class Trait;  class Static;  class Function; @@ -60,6 +61,7 @@ public:          ALIAS,          ENUM,          STRUCT, +        TRAIT,          STRUCT_METHOD,          ENUM_VAR, @@ -88,6 +90,7 @@ private:          const Module* module_;          const Enum* enum_;          const Struct*   struct_; +        const Trait*    trait;          const Static*   static_;          const Function* func_;          struct { diff --git a/src/convert/resolve.cpp b/src/convert/resolve.cpp index 1ff0daab..02b3ea78 100644 --- a/src/convert/resolve.cpp +++ b/src/convert/resolve.cpp @@ -479,8 +479,25 @@ void ResolvePaths_HandleModule(const AST::Crate& crate, const AST::Path& modpath      }
  }
 +void SetCrateName_Mod(::std::string name, AST::Module& mod)
 +{
 +    for(auto& submod : mod.submods())
 +        SetCrateName_Mod(name, submod.first);
 +    // Imports 'use' statements
 +    for(auto& imp : mod.imports())
 +        imp.data.set_crate(name);
 +    
 +    // TODO: All other types
 +}
 +
  void ResolvePaths(AST::Crate& crate)
  {
 +    // Pre-process external crates to tag all paths
 +    for(auto& ec : crate.extern_crates())
 +    {
 +        SetCrateName_Mod(ec.first, ec.second.root_module());
 +    }
 +    
      // Handle 'use' statements in an initial parss
      ResolvePaths_HandleModule_Use(crate, AST::Path(AST::Path::TagAbsolute()), crate.root_module());
 diff --git a/src/types.cpp b/src/types.cpp index b2dcd582..907bffc0 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -89,8 +89,21 @@ void operator% (::Deserialiser& d, eCoreType& ct) {      d.item(n);      /* */if(n == "-")   ct = CORETYPE_INVAL;      else if(n == "_")   ct = CORETYPE_ANY; +    else if(n == "char")  ct = CORETYPE_CHAR; +    else if(n == "usize") ct = CORETYPE_UINT; +    else if(n == "isize") ct = CORETYPE_INT; +    else if(n == "u8")    ct = CORETYPE_U8; +    else if(n == "i8")    ct = CORETYPE_I8; +    else if(n == "u16")   ct = CORETYPE_U16; +    else if(n == "i16")   ct = CORETYPE_I16; +    else if(n == "u32")   ct = CORETYPE_U32; +    else if(n == "i32")   ct = CORETYPE_I32; +    else if(n == "u64")   ct = CORETYPE_U64; +    else if(n == "i64")   ct = CORETYPE_I64; +    else if(n == "f32")   ct = CORETYPE_F32; +    else if(n == "f64")   ct = CORETYPE_F64;      else -        throw ::std::runtime_error("Deserialise failure - " + n); +        throw ::std::runtime_error("Deserialise failure - coretype " + n);  }  const char* TypeRef::class_name(TypeRef::Class c) {      switch(c) | 
