modelbuilder: easy attribute typing
[nit.git] / src / modelbuilder.nit
index f37a940..1386781 100644 (file)
@@ -67,12 +67,12 @@ class ModelBuilder
                # Setup the paths value
                paths.append(toolcontext.opt_path.value)
 
-               var path_env = once ("NIT_PATH".to_symbol).environ
+               var path_env = "NIT_PATH".environ
                if not path_env.is_empty then
                        paths.append(path_env.split_with(':'))
                end
 
-               path_env = once ("NIT_DIR".to_symbol).environ
+               path_env = "NIT_DIR".environ
                if not path_env.is_empty then
                        var libname = "{path_env}/lib"
                        if libname.file_exists then paths.add(libname)
@@ -1000,6 +1000,7 @@ redef class AMethPropdef
                        var mvisibility = new_property_visibility(modelbuilder, nclassdef, self.n_visibility)
                        mprop = new MMethod(mclassdef, name, mvisibility)
                        mprop.is_init = is_init
+                       mprop.is_new = self isa AExternInitPropdef
                        self.check_redef_keyword(modelbuilder, nclassdef, n_kwredef, false, mprop)
                else
                        if n_kwredef == null then
@@ -1288,10 +1289,39 @@ redef class AAttrPropdef
                end
 
                if mtype == null then
-                       modelbuilder.warning(self, "Error: Untyped attribute {mpropdef}")
-                       return
+                       var nexpr = self.n_expr
+                       if nexpr != null then
+                               if nexpr isa ANewExpr then
+                                       mtype = modelbuilder.resolve_mtype(nclassdef, nexpr.n_type)
+                               else if nexpr isa AIntExpr then
+                                       var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Int")
+                                       if cla != null then mtype = cla.mclass_type
+                               else if nexpr isa AFloatExpr then
+                                       var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Float")
+                                       if cla != null then mtype = cla.mclass_type
+                               else if nexpr isa ACharExpr then
+                                       var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Char")
+                                       if cla != null then mtype = cla.mclass_type
+                               else if nexpr isa ABoolExpr then
+                                       var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Bool")
+                                       if cla != null then mtype = cla.mclass_type
+                               else if nexpr isa ASuperstringExpr then
+                                       var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "String")
+                                       if cla != null then mtype = cla.mclass_type
+                               else if nexpr isa AStringFormExpr then
+                                       var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "String")
+                                       if cla != null then mtype = cla.mclass_type
+                               else
+                                       modelbuilder.error(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.")
+                               end
+
+                       else
+                               modelbuilder.error(self, "Error: Untyped attribute {mpropdef}")
+                       end
                end
 
+               if mtype == null then return
+
                mpropdef.static_mtype = mtype
 
                var mreadpropdef = self.mreadpropdef