Merge: Contract refactoring
authorJean Privat <jean@pryen.org>
Tue, 26 May 2020 19:26:30 +0000 (15:26 -0400)
committerJean Privat <jean@pryen.org>
Tue, 26 May 2020 19:26:30 +0000 (15:26 -0400)
This pr integrate:

- Minor documentation changes
- Addition of the `in_contract` attribute to disable verification of contracts when we are already in verification.
- Improved signature management

This pr depends of #2815 #2816 #2817 #2820

Pull-Request: #2822

src/astbuilder.nit
src/modelbuilder_base.nit
src/modelize/modelize_property.nit
src/semantize/typing.nit

index f3b35ea..8644213 100644 (file)
@@ -774,6 +774,17 @@ redef class AParam
        end
 end
 
+redef class AFormaldef
+
+       private init make(mparameter: MParameterType, t: AType)
+       do
+               _n_id = new TClassid
+               _n_id.text = mparameter.name
+               _n_type = t
+               _mtype = mparameter
+       end
+end
+
 redef class ABlockExpr
        private init make(t: nullable MType)
        do
@@ -934,3 +945,214 @@ redef class AAnnotation
                _n_args = n_args
        end
 end
+
+redef class MEntity
+       # Build a ANode from `self`
+       #
+       # Allows the creation of an AST node from a model entity.
+       fun create_ast_representation(astbuilder: nullable ASTBuilder): ANode is abstract
+end
+
+redef class MPropDef
+       redef fun create_ast_representation(astbuilder: nullable ASTBuilder): APropdef is abstract
+end
+
+redef class MClassDef
+       redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AStdClassdef do
+               if astbuilder == null then astbuilder = new ASTBuilder(mmodule)
+               var n_propdefs = new Array[APropdef]
+               for mpropdef in self.mpropdefs do
+                       n_propdefs.add(mpropdef.create_ast_representation(astbuilder))
+               end
+               var n_formaldefs = new Array[AFormaldef]
+               for mparameter in self.mclass.mparameters do n_formaldefs.add(mparameter.create_ast_representation(astbuilder))
+
+               return astbuilder.make_class(self, visibility.create_ast_representation(astbuilder), n_formaldefs, null, n_propdefs, null)
+       end
+end
+
+redef class MAttributeDef
+       redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AAttrPropdef do
+               if astbuilder == null then astbuilder = new ASTBuilder(mclassdef.mmodule)
+               var ntype = null
+               if self.static_mtype != null then ntype = static_mtype.create_ast_representation(astbuilder)
+               return astbuilder.make_attribute("_" + self.name, ntype, self.visibility.create_ast_representation(astbuilder), null, null, self, null, null)
+       end
+end
+
+redef class MMethodDef
+       redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AMethPropdef do
+               if astbuilder == null then astbuilder = new ASTBuilder(mclassdef.mmodule)
+               var tk_redef = null
+               if self.mproperty.intro != self then tk_redef = new TKwredef
+               var  n_signature = if self.msignature == null then new ASignature else self.msignature.create_ast_representation(astbuilder)
+               return astbuilder.make_method(visibility.create_ast_representation(astbuilder), tk_redef, self, n_signature)
+       end
+end
+
+redef class MVisibility
+       fun create_ast_representation(astbuilder: nullable ASTBuilder): AVisibility do
+               if self.to_s == "public" then
+                       return new APublicVisibility
+               else if self.to_s == "private" then
+                       return new APrivateVisibility
+               else if self.to_s == "protected" then
+                       return new AProtectedVisibility
+               else
+                       return new AIntrudeVisibility
+               end
+       end
+end
+
+redef class MSignature
+       redef fun create_ast_representation(astbuilder: nullable ASTBuilder): ASignature do
+               var nparams = new Array[AParam]
+               for mparam in mparameters do nparams.add(mparam.create_ast_representation(astbuilder))
+               var return_type = null
+               if self.return_mtype != null then return_type = self.return_mtype.create_ast_representation(astbuilder)
+               return new ASignature.init_asignature(null, nparams, null, return_type)
+       end
+end
+
+redef class MParameter
+       redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AParam do
+               var variable = new Variable(self.name)
+               variable.declared_type = self.mtype
+               return new AParam.make(variable, self.mtype.create_ast_representation(astbuilder))
+       end
+end
+
+redef class MParameterType
+       redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AFormaldef do
+               var n_type = super
+               return new AFormaldef.make(self, n_type)
+       end
+end
+
+redef class MType
+       redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AType do
+               return new AType.make(self)
+       end
+end
+
+redef class ModelBuilder
+       # Try to get MMethod property if exist in the given mclassdef. return new `MMethod` if not exist.
+       private fun get_mmethod(name: String, mclassdef: MClassDef, visibility: nullable MVisibility): MMethod do
+               visibility = visibility or else public_visibility
+               var mproperty = try_get_mproperty_by_name(null, mclassdef, name).as(nullable MMethod)
+               if mproperty == null then mproperty = new MMethod(mclassdef, name, mclassdef.location, visibility)
+               return mproperty
+       end
+
+       # Creation of a new method (AST and model representation) with the given name.
+       # See `create_method_from_property` for more information.
+       fun create_method_from_name(name: String, mclassdef: MClassDef, is_abstract: Bool, msignature: nullable MSignature, visibility: nullable MVisibility): AMethPropdef do
+               var mproperty = get_mmethod(name, mclassdef, visibility)
+               return create_method_from_property(mproperty, mclassdef, is_abstract, msignature)
+       end
+
+       # Creation of a new method (AST and model representation) with the given MMethod.
+       # Take care, if `is_abstract == false` the AMethPropdef returned has an empty body (potential error if the given signature has an return type).
+       fun create_method_from_property(mproperty: MMethod,  mclassdef: MClassDef, is_abstract: Bool, msignature: nullable MSignature): AMethPropdef do
+               var m_def = new MMethodDef(mclassdef, mproperty, mclassdef.location)
+
+               if msignature == null then msignature = new MSignature(new Array[MParameter])
+
+               m_def.msignature = msignature
+               m_def.is_abstract = true
+               var n_def = m_def.create_ast_representation
+               # Association new npropdef to mpropdef
+               unsafe_add_mpropdef2npropdef(m_def,n_def)
+
+               if not is_abstract then
+                       n_def.mpropdef.is_abstract = false
+                       n_def.n_block = new ABlockExpr.make
+               end
+
+               return n_def
+       end
+
+       # Creation of a new attribute (AST and model representation) with the given name.
+       # See `create_attribute_from_property` for more information.
+       fun create_attribute_from_name(name: String, mclassdef: MClassDef, mtype: MType, visibility: nullable MVisibility): AAttrPropdef do
+               if visibility == null then visibility = public_visibility
+               var mattribute = try_get_mproperty_by_name(null, mclassdef, name)
+               if mattribute == null then mattribute = new MAttribute(mclassdef, name, mclassdef.location, visibility)
+               return create_attribute_from_property(mattribute.as(MAttribute), mclassdef, mtype)
+       end
+
+       # Creation of a new attribute (AST and model representation) with the given MAttribute.
+       # See `create_attribute_from_propdef` for more information.
+       fun create_attribute_from_property(mattribute: MAttribute, mclassdef: MClassDef, mtype: MType): AAttrPropdef do
+               var attribut_def = new MAttributeDef(mclassdef, mattribute, mclassdef.location)
+               attribut_def.static_mtype = mtype
+               return create_attribute_from_propdef(attribut_def)
+       end
+
+       # Creation of a new attribute (AST representation) with the given MAttributeDef.
+       fun create_attribute_from_propdef(mattribut_def: MAttributeDef): AAttrPropdef
+       is
+               expect(mclassdef2node(mattribut_def.mclassdef) != null)
+       do
+               var n_attribute = mattribut_def.create_ast_representation
+
+               var nclass = mclassdef2node(mattribut_def.mclassdef)
+
+               n_attribute.location = mattribut_def.location
+               n_attribute.validate
+
+               nclass.n_propdefs.unsafe_add_all([n_attribute])
+               nclass.validate
+
+               n_attribute.build_read_property(self, mattribut_def.mclassdef)
+               n_attribute.build_read_signature
+
+               mpropdef2npropdef[mattribut_def] = n_attribute
+               return n_attribute
+       end
+
+       # Creation of a new class (AST and model representation) with the given name.
+       # `visibility` : Define the visibility of the method. If it's `null` the default is `public_visibility`
+       # See `create_class_from_mclass` for more information.
+       fun create_class_from_name(name: String, super_type: Array[MClassType], mmodule: MModule, visibility: nullable MVisibility): AStdClassdef do
+               if visibility == null then visibility = public_visibility
+               var mclass = try_get_mclass_by_name(null, mmodule, name)
+               if mclass == null then mclass = new MClass(mmodule, name, mmodule.location, new Array[String], concrete_kind, visibility)
+               return create_class_from_mclass(mclass, super_type, mmodule)
+       end
+
+       # Creation of a new class (AST and model representation) with the given MClass.
+       # This method creates a new concrete class definition `MClassDef`, and adds it to the class hierarchy.
+       # See `create_class_from_mclassdef` for more information.
+       fun create_class_from_mclass(mclass: MClass, super_type: Array[MClassType], mmodule: MModule): AStdClassdef do
+               var mclassdef = new MClassDef(mmodule, mclass.mclass_type, mmodule.location)
+               mclassdef.set_supertypes(super_type)
+               mclassdef.add_in_hierarchy
+
+               return create_class_from_mclassdef(mclassdef, mmodule)
+       end
+
+       # Creation of a new class (AST representation) with the given MClassDef.
+       # Note all the properties of our MClassDef will also generate an AST representation.
+       # Make an error if the attribute already has a representation in the modelbuilder.
+       # This method also create the default constructor.
+       fun create_class_from_mclassdef(mclassdef: MClassDef, mmodule: MModule): AStdClassdef do
+               var n_classdef = mclassdef.create_ast_representation
+               n_classdef.location = mclassdef.location
+               n_classdef.validate
+
+               for n_propdef in n_classdef.n_propdefs do
+                       var mpropdef = n_propdef.mpropdef
+                       assert mpropdef != null
+
+                       var p_npropdef = mpropdef2node(mpropdef)
+                       if  p_npropdef != null then error(null, "The property `{mpropdef.name}` already has a representation in the AST.")
+                       unsafe_add_mpropdef2npropdef(mpropdef, n_propdef)
+               end
+
+               process_default_constructors(n_classdef)
+               unsafe_add_mclassdef2nclassdef(mclassdef, n_classdef)
+
+               return n_classdef
+       end
+end
index 0db4b4b..0d75563 100644 (file)
@@ -60,7 +60,7 @@ class ModelBuilder
        # If no such a class exists, then null is returned.
        # If more than one class exists, then an error on `anode` is displayed and null is returned.
        # FIXME: add a way to handle class name conflict
-       fun try_get_mclass_by_name(anode: ANode, mmodule: MModule, name: String): nullable MClass
+       fun try_get_mclass_by_name(anode: nullable ANode, mmodule: MModule, name: String): nullable MClass
        do
                var classes = model.get_mclasses_by_name(name)
                if classes == null then
@@ -112,7 +112,7 @@ class ModelBuilder
        end
 
        # Like `try_get_mclass_by_name` but display an error message when the class is not found
-       fun get_mclass_by_name(node: ANode, mmodule: MModule, name: String): nullable MClass
+       fun get_mclass_by_name(node: nullable ANode, mmodule: MModule, name: String): nullable MClass
        do
                var mclass = try_get_mclass_by_name(node, mmodule, name)
                if mclass == null then
@@ -127,7 +127,7 @@ class ModelBuilder
        # If no such a property exists, then null is returned.
        # If more than one property exists, then an error on `anode` is displayed and null is returned.
        # FIXME: add a way to handle property name conflict
-       fun try_get_mproperty_by_name2(anode: ANode, mmodule: MModule, mtype: MType, name: String): nullable MProperty
+       fun try_get_mproperty_by_name2(anode: nullable ANode, mmodule: MModule, mtype: MType, name: String): nullable MProperty
        do
                var props = self.model.get_mproperties_by_name(name)
                if props == null then
@@ -209,7 +209,7 @@ class ModelBuilder
 
 
        # Alias for try_get_mproperty_by_name2(anode, mclassdef.mmodule, mclassdef.mtype, name)
-       fun try_get_mproperty_by_name(anode: ANode, mclassdef: MClassDef, name: String): nullable MProperty
+       fun try_get_mproperty_by_name(anode: nullable ANode, mclassdef: MClassDef, name: String): nullable MProperty
        do
                return try_get_mproperty_by_name2(anode, mclassdef.mmodule, mclassdef.bound_mtype, name)
        end
index 7aeffe8..6f7f209 100644 (file)
@@ -53,6 +53,17 @@ redef class ModelBuilder
                mpropdef2npropdef[mpropdef] = npropdef
        end
 
+       # Associate a `nclassdef` with its `mclassdef`
+       #
+       # Be careful, this method is unsafe, no checking is done when it's used.
+       # The safe way to add mclass it's to use the `build_property`
+       #
+       # See `mclassdef2nclassdef`
+       fun unsafe_add_mclassdef2nclassdef(mclassdef: MClassDef, nclassdef: AClassdef)
+       do
+               mclassdef2nclassdef[mclassdef] = nclassdef
+       end
+
        # Retrieve the associated AST node of a mpropertydef.
        # This method is used to associate model entity with syntactic entities.
        #
@@ -1405,12 +1416,7 @@ redef class AAttrPropdef
                                has_value = true
                                return false
                        end
-                       is_lazy = true
-                       var mlazyprop = new MAttribute(mclassdef, "lazy _" + name, self.location, none_visibility)
-                       mlazyprop.is_fictive = true
-                       var mlazypropdef = new MAttributeDef(mclassdef, mlazyprop, self.location)
-                       mlazypropdef.is_fictive = true
-                       self.mlazypropdef = mlazypropdef
+                       create_lazy
                end
                return true
        end
@@ -1508,6 +1514,64 @@ redef class AAttrPropdef
                mwritepropdef.msignature = msignature
        end
 
+       # Create a new setter for the attribute.
+       #
+       # `modelbuilder`: It's used to link the new `mwritepropdef` and `self`
+       # `visibility`: Is the setter has the same visibilty of the `mreadpropdef`.
+       #       If `not is_same_visibility and mreadpropdef.mproperty.visibility > protected_visibility` the `mwritepropdef` visibility will be set to protected.
+       fun create_setter(modelbuilder: ModelBuilder, is_same_visibility: nullable Bool): AAttrPropdef
+       is
+               expect(mreadpropdef != null) # Use to define the visibility, the mclassdef and the doc of the `mwritepropdef`
+       do
+               if mwritepropdef != null then return self # Self already has a `mwritepropdef`
+               var same_visibility = false
+               if is_same_visibility != null then same_visibility = is_same_visibility
+
+               self.build_write_property(modelbuilder, mreadpropdef.mclassdef, same_visibility)
+               self.build_write_signature
+               return self
+       end
+
+       # Set the default `self` value
+       #
+       # `expr`: Represents the default value of the attribute. If `expr isa ABlockExpr` `self.n_block` will be set.
+       fun define_default(expr: AExpr): AAttrPropdef
+       do
+               self.has_value = true
+               if expr isa ABlockExpr then
+                       self.n_block = expr
+               else
+                       self.n_expr = expr
+               end
+               return self
+       end
+
+       # Set `self` as optional
+       fun define_as_optional: AAttrPropdef
+       is
+               expect(has_value)
+       do
+               is_optional = true
+               return self
+       end
+
+       # Create the lazy attribute.
+       #
+       # see `mlazypropdef` for more information about this property.
+       fun create_lazy: AAttrPropdef
+       is
+               expect(has_value and mpropdef != null) # The only way to get a null `mpropdef` is when the attribute is defined as `abstract`. But if the attribute has a value, it cannot be abstract.
+       do
+               if self.mlazypropdef != null then return self # Self already has a `mlazypropdef`
+               is_lazy = true
+               var mlazyprop = new MAttribute(mpropdef.mclassdef, "lazy _" + name, self.location, none_visibility)
+               mlazyprop.is_fictive = true
+               var mlazypropdef = new MAttributeDef(mpropdef.mclassdef, mlazyprop, self.location)
+               mlazypropdef.is_fictive = true
+               self.mlazypropdef = mlazypropdef
+               return self
+       end
+
        # Detect the static type from the value assigned to the attribute `self`
        #
        # Return the static type if it can be safely inferred.
index 5d6b5da..98aba77 100644 (file)
@@ -76,6 +76,12 @@ private class TypeVisitor
                end
        end
 
+       # Display a warning on `node` if `not mpropdef.is_fictive`
+       fun display_warning(node: ANode, tag: String, message: String)
+       do
+               if not mpropdef.is_fictive then self.modelbuilder.warning(node, tag, message)
+       end
+
        fun anchor_to(mtype: MType): MType
        do
                return mtype.anchor_to(mmodule, anchor)
@@ -190,9 +196,9 @@ private class TypeVisitor
                if sup == null then return null # Forward error
 
                if sup == sub then
-                       self.modelbuilder.warning(node, "useless-type-test", "Warning: expression is already a `{sup}`.")
+                       display_warning(node, "useless-type-test", "Warning: expression is already a `{sup}`.")
                else if self.is_subtype(sub, sup) then
-                       self.modelbuilder.warning(node, "useless-type-test", "Warning: expression is already a `{sup}` since it is a `{sub}`.")
+                       display_warning(node, "useless-type-test", "Warning: expression is already a `{sup}` since it is a `{sub}`.")
                end
                return sup
        end
@@ -215,16 +221,16 @@ private class TypeVisitor
        fun check_can_be_null(anode: ANode, mtype: MType): Bool
        do
                if mtype isa MNullType then
-                       modelbuilder.warning(anode, "useless-null-test", "Warning: expression is always `null`.")
+                       display_warning(anode, "useless-null-test", "Warning: expression is always `null`.")
                        return true
                end
                if can_be_null(mtype) then return true
 
                if mtype isa MFormalType then
                        var res = anchor_to(mtype)
-                       modelbuilder.warning(anode, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}: {res}`.")
+                       display_warning(anode, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}: {res}`.")
                else
-                       modelbuilder.warning(anode, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}`.")
+                       display_warning(anode, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}`.")
                end
                return false
        end
@@ -379,9 +385,9 @@ private class TypeVisitor
                if info != null and self.mpropdef.mproperty.deprecation == null then
                        var mdoc = info.mdoc
                        if mdoc != null then
-                               self.modelbuilder.warning(node, "deprecated-method", "Deprecation Warning: method `{mproperty.name}` is deprecated: {mdoc.content.first}")
+                               display_warning(node, "deprecated-method", "Deprecation Warning: method `{mproperty.name}` is deprecated: {mdoc.content.first}")
                        else
-                               self.modelbuilder.warning(node, "deprecated-method", "Deprecation Warning: method `{mproperty.name}` is deprecated.")
+                               display_warning(node, "deprecated-method", "Deprecation Warning: method `{mproperty.name}` is deprecated.")
                        end
                end
 
@@ -394,7 +400,7 @@ private class TypeVisitor
                else if propdefs.length == 1 then
                        mpropdef = propdefs.first
                else
-                       self.modelbuilder.warning(node, "property-conflict", "Warning: conflicting property definitions for property `{mproperty.name}` in `{unsafe_type}`: {propdefs.join(" ")}")
+                       display_warning(node, "property-conflict", "Warning: conflicting property definitions for property `{mproperty.name}` in `{unsafe_type}`: {propdefs.join(" ")}")
                        mpropdef = mproperty.intro
                end
 
@@ -1032,6 +1038,18 @@ redef class AExpr
                end
                return res
        end
+
+       # Type the expression as if located in `visited_mpropdef`
+       # `TypeVisitor` and `PostTypingVisitor` will be used to do the typing, see them for more information.
+       #
+       # `visited_mpropdef`: Correspond to the evaluation context in which the expression is located.
+       fun do_typing(modelbuilder: ModelBuilder, visited_mpropdef: MPropDef)
+       do
+               var type_visitor = new TypeVisitor(modelbuilder, visited_mpropdef)
+               type_visitor.visit_stmt(self)
+               var post_visitor = new PostTypingVisitor(type_visitor)
+               post_visitor.enter_visit(self)
+       end
 end
 
 redef class ABlockExpr
@@ -1739,7 +1757,7 @@ redef class AArrayExpr
                end
                if useless then
                        assert ntype != null
-                       v.modelbuilder.warning(ntype, "useless-type", "Warning: useless type declaration `{mtype}` in literal Array since it can be inferred from the elements type.")
+                       v.display_warning(ntype, "useless-type", "Warning: useless type declaration `{mtype}` in literal Array since it can be inferred from the elements type.")
                end
 
                self.element_mtype = mtype
@@ -1993,9 +2011,7 @@ redef class ASendExpr
 
                var args = compute_raw_arguments
 
-                if not self isa ACallrefExpr then
-                       callsite.check_signature(v, node, args)
-                end
+               if not self isa ACallrefExpr then callsite.check_signature(v, node, args)
 
                if callsite.mproperty.is_init then
                        var vmpropdef = v.mpropdef
@@ -2064,7 +2080,7 @@ redef class AEqFormExpr
                if mtype == null or mtype2 == null then return
 
                if mtype == v.type_bool(self) and (n_expr2 isa AFalseExpr or n_expr2 isa ATrueExpr) then
-                       v.modelbuilder.warning(self, "useless-truism", "Warning: useless comparison to a Bool literal.")
+                       v.display_warning(self, "useless-truism", "Warning: useless comparison to a Bool literal.")
                end
 
                if not mtype2 isa MNullType then return
@@ -2581,7 +2597,7 @@ redef class ASafeExpr
                self.mtype = mtype.as_notnull
 
                if not v.can_be_null(mtype) then
-                       v.modelbuilder.warning(self, "useless-safe", "Warning: useless safe operator `?` on non-nullable value.")
+                       v.display_warning(self, "useless-safe", "Warning: useless safe operator `?` on non-nullable value.")
                        return
                end
        end
@@ -2609,7 +2625,7 @@ redef class ADebugTypeExpr
                var mtype = v.resolve_mtype(ntype)
                if mtype != null and mtype != expr then
                        var umtype = v.anchor_to(mtype)
-                       v.modelbuilder.warning(self, "debug", "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
+                       v.display_warning(self, "debug", "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
                end
                self.is_typed = true
        end