neo_doxygen: Add types to the model.
authorJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Sun, 2 Nov 2014 22:12:11 +0000 (17:12 -0500)
committerJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Tue, 4 Nov 2014 17:14:12 +0000 (12:14 -0500)
Signed-off-by: Jean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>

contrib/neo_doxygen/src/model/class_compound.nit
contrib/neo_doxygen/src/model/type_entity.nit [new file with mode: 0644]

index 5729a14..7f2f0ab 100644 (file)
@@ -16,6 +16,7 @@
 module model::class_compound
 
 import graph
+import type_entity
 
 # A class.
 class ClassCompound
@@ -31,14 +32,17 @@ class ClassCompound
 
        init do
                super
-               class_type = new ClassType(graph, self)
+               class_type = new ClassType(graph)
+               class_type.class_compound = self
                class_def = new ClassDef(graph, self)
                self.labels.add("MClass")
-               self["arity"] = 0 # TODO
                kind = "class"
                visibility = "public"
        end
 
+       # Return the number of type parameters.
+       fun arity: Int do return class_type.arity
+
        redef fun name=(name: String) do
                super
                class_type.name = name
@@ -80,6 +84,14 @@ class ClassCompound
        redef fun put_edges do
                super
                graph.add_edge(self, "CLASSTYPE", class_type)
+               if arity > 0 then
+                       var names = new JsonArray
+
+                       for p in class_type.arguments do
+                               names.add(p.name)
+                       end
+                       self["parameter_names"] = names
+               end
        end
 end
 
@@ -114,17 +126,61 @@ end
 
 # A type defined by a class.
 class ClassType
-       super Entity
+       super TypeEntity
 
-       var class_compound: ClassCompound
+       # The associated class.
+       #
+       # You may use this attribute or `class_compound_id` to specify the class.
+       var class_compound: nullable ClassCompound = null is writable
+
+       # The `model_id` of the associated class.
+       #
+       # You may use this attribute or `class_compound` to specify the class.
+       var class_compound_id: String = "" is writable
+
+       var arguments = new Array[TypeEntity]
 
        init do
                super
-               self.labels.add("MType")
                self.labels.add("MClassType")
        end
 
+       # Return the number of arguments.
+       fun arity: Int do return 0 # TODO
+
+       fun is_generic: Bool do return arity > 0
+
+       redef fun put_in_graph do
+               super
+               if is_generic then
+                       self.labels.add("MGenericType")
+               else
+                       var i = self.labels.index_of("MGenericType")
+                       if i >= 0 then self.labels.remove_at(i)
+               end
+       end
+
        redef fun put_edges do
-               graph.add_edge(self, "CLASS", class_compound)
+               var cls = class_compound
+
+               if cls == null and class_compound_id != "" then
+                       cls = graph.by_id[class_compound_id].as(ClassCompound)
+               end
+               assert cls != null
+
+               super
+               graph.add_edge(self, "CLASS", cls)
+               assert cls.arity == self.arity
+               for i in [0..arguments.length[ do
+                       var a = arguments[i]
+                       if cls.class_type != self then
+                               a.name = cls.class_type.arguments[i].name
+                       end
+                       if a isa TypeParameter then
+                               a.rank = i
+                               graph.add_edge(a, "CLASS", cls)
+                       end
+                       graph.add_edge(self, "ARGUMENT", a)
+               end
        end
 end
diff --git a/contrib/neo_doxygen/src/model/type_entity.nit b/contrib/neo_doxygen/src/model/type_entity.nit
new file mode 100644 (file)
index 0000000..e1a6af5
--- /dev/null
@@ -0,0 +1,140 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Typing and parameters.
+module model::type_entity
+
+import graph
+
+# Base class of all types and signatures.
+abstract class TypeEntity
+       super Entity
+
+       init do
+               super
+               self.labels.add("MType")
+       end
+end
+
+# A type parameter or a type argument.
+#
+# Note : The class relationship and the rank are set by `MClassType.put_edges`.
+class TypeParameter
+       super TypeEntity
+       super Parameter
+
+       init do
+               super
+               self.labels.add("MParameterType")
+       end
+end
+
+# A signature of a method.
+class Signature
+       super TypeEntity
+
+       # The parameters.
+       var parameters = new Array[MemberParameter]
+
+       # The static type of the returned value.
+       var return_type: nullable TypeEntity = null is writable
+
+       init do
+               super
+               self.labels.add("MSignature")
+       end
+
+       redef fun put_in_graph do
+               super
+               if return_type isa TypeEntity then
+                       return_type.as(TypeEntity).put_in_graph
+               end
+               for p in parameters do
+                       p.put_in_graph
+               end
+       end
+
+       redef fun put_edges do
+               super
+               if parameters.length > 0 then
+                       var names = new JsonArray
+
+                       for i in [0..parameters.length[ do
+                               var p = parameters[i]
+                               p.rank = i
+                               names.add(p.name)
+                               graph.add_edge(self, "PARAMETER", p)
+                       end
+                       self["parameter_names"] = names
+               end
+               if return_type != null then
+                       graph.add_edge(self, "RETURNTYPE", return_type.as(not null))
+               end
+       end
+end
+
+# A parameter or an argument.
+abstract class Parameter
+       super Entity
+
+       # The static type of the parameter.
+       var static_type: nullable TypeEntity = null is writable
+
+       init do
+               super
+               self["is_vararg"] = false
+               self["rank"] = -1
+       end
+
+       # Is the parameter a “vararg”?
+       fun is_vararg=(is_vararg: Bool) do
+               self["is_vararg"] = is_vararg
+       end
+
+       # Is the parameter a “vararg”?
+       fun is_vararg: Bool do
+               var value = self["is_vararg"]
+               assert value isa Bool
+               return value
+       end
+
+       # Set the rank (index) of the parameter in the signature.
+       fun rank=(rank: Int) do
+               self["rank"] = rank
+       end
+
+       redef fun put_in_graph do
+               super
+               if static_type != null then
+                       static_type.as(not null).put_in_graph
+               end
+       end
+
+       redef fun put_edges do
+               super
+               graph.add_edge(self, "TYPE", static_type.as(not null))
+       end
+end
+
+# A parameter of a member.
+#
+# Note : The rank are set by `Signature.put_edges`.
+class MemberParameter
+       super Parameter
+
+       init do
+               super
+               self.labels.add("MParameter")
+       end
+end