nullable: convert lib, tools and tests
[nit.git] / src / nitdoc.nit
index 2bfb3ad..4c98a84 100644 (file)
@@ -26,7 +26,7 @@ import abstracttool
 class DocContext
 special AbstractCompiler
        # Destination directory
 class DocContext
 special AbstractCompiler
        # Destination directory
-       readable writable attr _dir: String
+       readable writable attr _dir: String = "."
 
        # Content of a generated file
        attr _stage_context: StageContext = new StageContext(null)
 
        # Content of a generated file
        attr _stage_context: StageContext = new StageContext(null)
@@ -51,6 +51,7 @@ special AbstractCompiler
                        s.content.add_all(_stage_context.content)
                        s.validate = true
                end
                        s.content.add_all(_stage_context.content)
                        s.validate = true
                end
+               assert s != null
                _stage_context = s
        end
 
                _stage_context = s
        end
 
@@ -66,13 +67,13 @@ special AbstractCompiler
        end
 
        # Currently computed module
        end
 
        # Currently computed module
-       readable attr _module: MMSrcModule
+       readable attr _module: nullable MMSrcModule
 
        # Is the current directory module computed as a simple modude ?
 
        # Is the current directory module computed as a simple modude ?
-       readable writable attr _inside_mode: Bool
+       readable writable attr _inside_mode: Bool = false
 
        # Is the current module computed as a intruded one ?
 
        # Is the current module computed as a intruded one ?
-       readable writable attr _intrude_mode: Bool
+       readable writable attr _intrude_mode: Bool = false
 
        # Compued introducing entities (for the index)
        attr _entities: Array[MMEntity] = new Array[MMEntity]
 
        # Compued introducing entities (for the index)
        attr _entities: Array[MMEntity] = new Array[MMEntity]
@@ -215,12 +216,13 @@ special AbstractCompiler
                _sorter.sort(array)
        end
 
                _sorter.sort(array)
        end
 
-       readable writable attr _owned_modules: Array[MMModule]
+       readable writable attr _owned_modules: Array[MMModule] = new Array[MMModule]
 
        # Return the known_owner for current module
        # if inside_mode is set, it could be a different result
        meth known_owner_of(m: MMModule): MMModule
        do
 
        # Return the known_owner for current module
        # if inside_mode is set, it could be a different result
        meth known_owner_of(m: MMModule): MMModule
        do
+               var module = module
                if module == null then return m
                var res = module.known_owner_of(m)
                if not inside_mode and not intrude_mode and res.directory.owner == module then
                if module == null then return m
                var res = module.known_owner_of(m)
                if not inside_mode and not intrude_mode and res.directory.owner == module then
@@ -243,7 +245,7 @@ special AbstractCompiler
                self.extract_other_doc
        end
 
                self.extract_other_doc
        end
 
-       redef init
+       init
        do
                super
                option_context.add_option(opt_dir)
        do
                super
                option_context.add_option(opt_dir)
@@ -252,8 +254,8 @@ special AbstractCompiler
        redef meth process_options
        do
                super
        redef meth process_options
        do
                super
-               dir = opt_dir.value
-               if dir == null then dir = "."
+               var d = opt_dir.value
+               if d != null then dir = d
        end
 end
 
        end
 end
 
@@ -263,12 +265,12 @@ class StageContext
        readable attr _content: Array[String] = new Array[String]
 
        # Is a normal string already added?
        readable attr _content: Array[String] = new Array[String]
 
        # Is a normal string already added?
-       readable writable attr _validate: Bool
+       readable writable attr _validate: Bool = false
 
        # Parent stage is any
 
        # Parent stage is any
-       readable attr _parent: StageContext
+       readable attr _parent: nullable StageContext = null
 
 
-       init(parent: StageContext) do _parent = parent
+       init(parent: nullable StageContext) do _parent = parent
 end
 
 
 end
 
 
@@ -314,7 +316,7 @@ class MMEntity
 
        # The doc node from the AST
        # Return null is none
 
        # The doc node from the AST
        # Return null is none
-       meth doc: ADoc do return null
+       meth doc: nullable ADoc do return null
 
        # Human redable location of the entity (module/class/property)
        meth locate(dctx: DocContext): String do return ""
 
        # Human redable location of the entity (module/class/property)
        meth locate(dctx: DocContext): String do return ""
@@ -356,7 +358,7 @@ special MMEntity
        meth owner(from: MMModule): MMModule
        do
                var res = self
        meth owner(from: MMModule): MMModule
        do
                var res = self
-               var d = directory
+               var d: nullable MMDirectory = directory
                while d != null and d != from.directory do
                        var o = d.owner
                        if o != null and o.mhe <= res then res = o
                while d != null and d != from.directory do
                        var o = d.owner
                        if o != null and o.mhe <= res then res = o
@@ -408,12 +410,6 @@ special MMEntity
                end
        end
        
                end
        end
        
-       redef meth short_doc do return concrete_property.short_doc
-
-       redef meth doc do return concrete_property.doc
-
-       redef meth need_doc(dctx) do return false
-
        # Kind of property (meth, attr, etc.)
        meth kind: String is abstract
 
        # Kind of property (meth, attr, etc.)
        meth kind: String is abstract
 
@@ -431,7 +427,7 @@ special MMEntity
 
        redef meth prototype_head(dctx)
        do
 
        redef meth prototype_head(dctx)
        do
-               var res = ""
+               var res = new Buffer
                var intro_class = known_intro_class(dctx)
                var is_redef = local_class != intro_class
 
                var intro_class = known_intro_class(dctx)
                var is_redef = local_class != intro_class
 
@@ -450,21 +446,65 @@ special MMEntity
                                res.append(" {intro_class.module.html_link(dctx)}::")
                        end
                end
                                res.append(" {intro_class.module.html_link(dctx)}::")
                        end
                end
-               return res
+               return res.to_s
        end
 
        redef meth prototype_body(dctx)
        do
        end
 
        redef meth prototype_body(dctx)
        do
-               var res = signature.to_html(dctx)
+               var res = new Buffer
+               res.append(signature.to_html(dctx))
                var s = self
                var s = self
-               if s isa MMConcreteProperty then
+               if s.node != null then
                        if s.node isa ADeferredMethPropdef then
                                res.append(" is abstract")
                        else if s.node isa AInternMethPropdef then
                                res.append(" is intern")
                        end
                end
                        if s.node isa ADeferredMethPropdef then
                                res.append(" is abstract")
                        else if s.node isa AInternMethPropdef then
                                res.append(" is intern")
                        end
                end
-               return res
+               return res.to_s
+       end
+
+       redef meth need_doc(dctx)
+       do
+               if global.visibility_level >= 3 or self isa MMAttribute then
+                       if not dctx.intrude_mode then return false
+                       if dctx.module.visibility_for(module) == 0 then return false
+               end
+               if global.intro == self then
+                       return true
+               end
+               return doc != null
+       end
+
+       redef meth short_doc
+       do
+               var d = doc
+               if d != null then
+                       return d.short
+               else if global.intro == self then
+                       return "&nbsp;"
+               else
+                       return global.intro.short_doc
+               end
+       end
+       
+       redef meth doc
+       do
+               var n = node
+               if n == null or not node isa PPropdef then
+                       return null
+               end
+               assert n isa PPropdef
+               var d = n.n_doc
+               if d == null then
+                       return null
+               end
+               assert d isa ADoc
+               if d.n_comment.is_empty then
+                       return null
+               else
+                       return d
+               end
        end
 end
 redef class MMMethod
        end
 end
 redef class MMMethod
@@ -507,7 +547,7 @@ redef class MMSrcModule
                dctx.add_header("Module {self}")
                dctx.add("<h1>Module {self}</h1>\n<dl>")
                var s = ""
                dctx.add_header("Module {self}")
                dctx.add("<h1>Module {self}</h1>\n<dl>")
                var s = ""
-               var d = directory
+               var d: nullable MMDirectory = directory
                while d == null do
                        if d.owner != null and (d.owner != self or dctx.inside_mode or dctx.intrude_mode) then
                                s = "{d.owner.html_link(dctx)}::{s}"
                while d == null do
                        if d.owner != null and (d.owner != self or dctx.inside_mode or dctx.intrude_mode) then
                                s = "{d.owner.html_link(dctx)}::{s}"
@@ -520,8 +560,8 @@ redef class MMSrcModule
                var intrude_modules = new Array[MMModule]
                var public_modules = new Array[MMModule]
                var private_modules = new Array[MMModule]
                var intrude_modules = new Array[MMModule]
                var public_modules = new Array[MMModule]
                var private_modules = new Array[MMModule]
-               var owned_modules = new Array[MMModule]
-               dctx.owned_modules = owned_modules
+               var owned_modules = dctx.owned_modules
+               owned_modules.clear
                for m in mhe.greaters do
                        var v = visibility_for(m) 
                        if not dctx.inside_mode and not dctx.intrude_mode and m.directory.owner == self then 
                for m in mhe.greaters do
                        var v = visibility_for(m) 
                        if not dctx.inside_mode and not dctx.intrude_mode and m.directory.owner == self then 
@@ -567,10 +607,12 @@ redef class MMSrcModule
                                end
                        else
                                for m in owned_modules do
                                end
                        else
                                for m in owned_modules do
-                                       var mc = m[c.global]
-                                       if mc != null and mc.need_doc(dctx) then 
-                                               new_classes.add(c)
-                                               break
+                                       if m.global_classes.has(c.global) then
+                                               var mc = m[c.global]
+                                               if mc.need_doc(dctx) then
+                                                       new_classes.add(c)
+                                                       break
+                                               end
                                        end
                                end
                        end
                                        end
                                end
                        end
@@ -612,20 +654,16 @@ redef class MMSrcModule
        redef meth doc
        do
                var n = node
        redef meth doc
        do
                var n = node
-               if not n isa AModule then
-                       return null
-               end
-               assert n isa AModule
                if n.n_packagedecl == null then
                        return null
                end
                var np = n.n_packagedecl
                assert np isa APackagedecl
                var d = np.n_doc
                if n.n_packagedecl == null then
                        return null
                end
                var np = n.n_packagedecl
                assert np isa APackagedecl
                var d = np.n_doc
-               assert d isa ADoc
                if d == null then
                        return null
                end
                if d == null then
                        return null
                end
+               assert d isa ADoc
                if d.n_comment.is_empty then
                        return null
                else
                if d.n_comment.is_empty then
                        return null
                else
@@ -638,11 +676,11 @@ redef class ADoc
        # Html transcription of the doc
        meth to_html: String
        do
        # Html transcription of the doc
        meth to_html: String
        do
-               var res = new String
+               var res = new Buffer
                for c in n_comment do
                        res.append(c.text.substring_from(1))
                end
                for c in n_comment do
                        res.append(c.text.substring_from(1))
                end
-               return res
+               return res.to_s
        end
 
        # Oneliner transcription of the doc
        end
 
        # Oneliner transcription of the doc
@@ -676,8 +714,10 @@ special MMEntity
        redef meth need_doc(dctx) do
                if module == dctx.module then
                        for m in dctx.owned_modules do
        redef meth need_doc(dctx) do
                if module == dctx.module then
                        for m in dctx.owned_modules do
-                               var c = m[global]
-                               if c != null and c.need_doc(dctx) then return true
+                               if m.global_classes.has(global) then
+                                       var c = m[global]
+                                       if c.need_doc(dctx) then return true
+                               end
                        end
                end
                return false
                        end
                end
                return false
@@ -689,19 +729,19 @@ special MMEntity
 
        redef meth prototype_head(dctx)
        do
 
        redef meth prototype_head(dctx)
        do
-               var res = ""
+               var res = new Buffer
                var ki = known_intro(dctx)
                var is_redef = ki != self
                if is_redef then res.append("redef ")
                if global.visibility_level == 3 then res.append("private ")
                res.append("class ")
                if is_redef then res.append("{ki.module.html_link(dctx)}::")
                var ki = known_intro(dctx)
                var is_redef = ki != self
                if is_redef then res.append("redef ")
                if global.visibility_level == 3 then res.append("private ")
                res.append("class ")
                if is_redef then res.append("{ki.module.html_link(dctx)}::")
-               return res
+               return res.to_s
        end
 
        redef meth prototype_body(dctx)
        do
        end
 
        redef meth prototype_body(dctx)
        do
-               var res = ""
+               var res = new Buffer
                if arity > 0 then
                        res.append("[")
                        for i in [0..arity[ do
                if arity > 0 then
                        res.append("[")
                        for i in [0..arity[ do
@@ -712,7 +752,7 @@ special MMEntity
                        end
                        res.append("]")
                end
                        end
                        res.append("]")
                end
-               return res
+               return res.to_s
        end
 
        # Extract the doc of a class
        end
 
        # Extract the doc of a class
@@ -827,20 +867,22 @@ special MMEntity
                var passname = pass_name(pass)
                dctx.open_stage
                dctx.stage("<table border=\"1\" width=\"100%\" cellpadding=\"3\" cellspacing=\"0\">\n")
                var passname = pass_name(pass)
                dctx.open_stage
                dctx.stage("<table border=\"1\" width=\"100%\" cellpadding=\"3\" cellspacing=\"0\">\n")
-               dctx.stage("<tr bgcolor=\"#CCCCFF\"><th colspan=\"2\">{passname} Summary of {self}</th><tr>\n")
+               dctx.stage("<tr bgcolor=\"#CCCCFF\"><th colspan=\"2\">{passname} Summary of {self}</th></tr>\n")
 
                var new_props = new Array[MMLocalProperty]
                for g in global_properties do
                        if not accept_prop(g.intro, pass) then continue
                        if module.visibility_for(g.intro.module) < g.visibility_level then continue
                        var p = self[g]
 
                var new_props = new Array[MMLocalProperty]
                for g in global_properties do
                        if not accept_prop(g.intro, pass) then continue
                        if module.visibility_for(g.intro.module) < g.visibility_level then continue
                        var p = self[g]
-                       if not p.need_doc(dctx) then
+                       if p.local_class != self or not p.need_doc(dctx) then
                                var cla = new Array[MMLocalClass]
                                for m in dctx.owned_modules do
                                var cla = new Array[MMLocalClass]
                                for m in dctx.owned_modules do
+                                       if not m.global_classes.has(global) then continue
                                        var c = m[global]
                                        var c = m[global]
-                                       if c == null or not c isa MMConcreteClass then continue
+                                       if not c isa MMConcreteClass then continue
+                                       if not c.has_global_property(g) then continue
                                        var p2 = c[g]
                                        var p2 = c[g]
-                                       if p2 == null or not p2.need_doc(dctx) then continue
+                                       if p2.local_class != c or not p2.need_doc(dctx) then continue
                                        cla.add(c)
                                end
                                if cla.is_empty then continue
                                        cla.add(c)
                                end
                                if cla.is_empty then continue
@@ -854,7 +896,7 @@ special MMEntity
                end
                dctx.sort(new_props)
                for p in new_props do
                end
                dctx.sort(new_props)
                for p in new_props do
-                       dctx.add("<tr><td width=\"20%\" align=\"right\">{p.prototype_head(dctx)}</td><td><b>{p.html_link(dctx)}</b>{p.prototype_body(dctx)}<br/>&nbsp;&nbsp;&nbsp;&nbsp;{p.short_doc}</td><tr>\n")
+                       dctx.add("<tr><td width=\"20%\" align=\"right\">{p.prototype_head(dctx)}</td><td><b>{p.html_link(dctx)}</b>{p.prototype_body(dctx)}<br/>&nbsp;&nbsp;&nbsp;&nbsp;{p.short_doc}</td></tr>\n")
                end
                dctx.stage("</table><br/>\n")
 
                end
                dctx.stage("</table><br/>\n")
 
@@ -869,7 +911,7 @@ special MMEntity
                                if c isa MMSrcLocalClass then
                                        var km = dctx.known_owner_of(c.module)
                                        var kc = km[c.global]
                                if c isa MMSrcLocalClass then
                                        var km = dctx.known_owner_of(c.module)
                                        var kc = km[c.global]
-                                       if kc == self or not c isa MMConcreteClass then continue
+                                       if kc == self then continue
                                        var props: Array[MMLocalProperty]
                                        if km == module then
                                                if cmap.has_key(kc) then
                                        var props: Array[MMLocalProperty]
                                        if km == module then
                                                if cmap.has_key(kc) then
@@ -888,7 +930,7 @@ special MMEntity
                                        end
                                        for g in c.global_properties do
                                                var p = c[g]
                                        end
                                        for g in c.global_properties do
                                                var p = c[g]
-                                               if p.need_doc(dctx) and accept_prop(p, pass) then
+                                               if p.local_class == c and p.need_doc(dctx) and accept_prop(p, pass) then
                                                        props.add(kc[g])
                                                end
                                        end
                                                        props.add(kc[g])
                                                end
                                        end
@@ -937,7 +979,7 @@ special MMEntity
                        end
                        for g in c.global_properties do
                                var p = c[g]
                        end
                        for g in c.global_properties do
                                var p = c[g]
-                               if p.need_doc(dctx) and accept_prop(p, pass) then
+                               if p.local_class == c and p.need_doc(dctx) and accept_prop(p, pass) then
                                        var kp = kc[g]
                                        if not props.has(kp) then props.add(kp)
                                end
                                        var kp = kc[g]
                                        if not props.has(kp) then props.add(kp)
                                end
@@ -997,7 +1039,7 @@ special MMEntity
                var properties = new Array[String]
                for g in global_properties do
                        var p = self[g]
                var properties = new Array[String]
                for g in global_properties do
                        var p = self[g]
-                       if p.need_doc(dctx) and accept_prop(p, pass) then
+                       if p.local_class == self and p.need_doc(dctx) and accept_prop(p, pass) then
                                properties.add(p.html_link(dctx))
                        end
                end
                                properties.add(p.html_link(dctx))
                        end
                end
@@ -1035,10 +1077,10 @@ redef class MMSrcLocalClass
                end
                assert n isa AClassdef
                var d = n.n_doc
                end
                assert n isa AClassdef
                var d = n.n_doc
-               assert d isa ADoc
                if d == null then
                        return null
                end
                if d == null then
                        return null
                end
+               assert d isa ADoc
                if d.n_comment.is_empty then
                        return null
                else
                if d.n_comment.is_empty then
                        return null
                else
@@ -1064,56 +1106,11 @@ redef class MMSrcLocalClass
        end
 end
 
        end
 end
 
-redef class MMConcreteProperty
-       redef meth need_doc(dctx)
-       do
-               if global.visibility_level >= 3 or self isa MMAttribute then
-                       if not dctx.intrude_mode then return false
-                       if dctx.module.visibility_for(module) == 0 then return false
-               end
-               if global.intro == self then
-                       return true
-               end
-               return true
-       end
-
-       redef meth short_doc
-       do
-               var d = doc
-               if d != null then
-                       return d.short
-               else if global.intro == self then
-                       return "&nbsp;"
-               else
-                       return global.intro.short_doc
-               end
-       end
-       
-       redef meth doc
-       do
-               var n = node
-               if not node isa PPropdef then
-                       return null
-               end
-               assert n isa PPropdef
-               var d = n.n_doc
-               assert d isa ADoc
-               if d == null then
-                       return null
-               end
-               if d.n_comment.is_empty then
-                       return null
-               else
-                       return d
-               end
-       end
-end
-
 redef class MMSignature
        # Htlm transcription of the signature (with nested links)
        meth to_html(dctx: DocContext): String
        do
 redef class MMSignature
        # Htlm transcription of the signature (with nested links)
        meth to_html(dctx: DocContext): String
        do
-               var res = new String
+               var res = new Buffer
                if arity > 0 then
                        res.append("(")
                        res.append(self[0].html_link(dctx))
                if arity > 0 then
                        res.append("(")
                        res.append(self[0].html_link(dctx))
@@ -1127,7 +1124,7 @@ redef class MMSignature
                        res.append(": ")
                        res.append(return_type.html_link(dctx))
                end
                        res.append(": ")
                        res.append(return_type.html_link(dctx))
                end
-               return res
+               return res.to_s
        end
 end
 
        end
 end
 
@@ -1143,7 +1140,8 @@ end
 redef class MMTypeGeneric
        redef meth html_link(dctx)
        do
 redef class MMTypeGeneric
        redef meth html_link(dctx)
        do
-               var res = local_class.html_link(dctx)
+               var res = new Buffer
+               res.append(local_class.html_link(dctx))
                res.append("[")
                res.append(params[0].html_link(dctx))
                for i in [1..params.length[ do
                res.append("[")
                res.append(params[0].html_link(dctx))
                for i in [1..params.length[ do
@@ -1151,7 +1149,7 @@ redef class MMTypeGeneric
                        res.append(params[i].html_link(dctx))
                end
                res.append("]")
                        res.append(params[i].html_link(dctx))
                end
                res.append("]")
-               return res
+               return res.to_s
        end
 end
 
        end
 end