docs: support for documenting variables
authorDaniel Kolesa <d.kolesa@osg.samsung.com>
Fri, 21 Oct 2016 14:00:45 +0000 (16:00 +0200)
committerDaniel Kolesa <d.kolesa@osg.samsung.com>
Fri, 21 Oct 2016 14:01:03 +0000 (16:01 +0200)
src/scripts/elua/apps/docgen/doctree.lua
src/scripts/elua/apps/gendoc.lua

index c8996c2..699cb1c 100644 (file)
@@ -1118,7 +1118,11 @@ M.Variable = Node:clone {
     end,
 
     value_get = function(self)
-        return self.variable:value_get()
+        local v = self.variable:value_get()
+        if not v then
+            return nil
+        end
+        return M.Expression(v)
     end,
 
     name_get = function(self)
@@ -1141,6 +1145,68 @@ M.Variable = Node:clone {
         return M.Node.nspaces_get(self, self:type_str_get(), root)
     end,
 
+    serialize = function(self)
+        local buf = {}
+        if self:type_get() == self.GLOBAL then
+            buf[#buf + 1] = "var "
+        else
+            buf[#buf + 1] = "const "
+        end
+        if self:is_extern() then
+            buf[#buf + 1] = "@extern "
+        end
+        buf[#buf + 1] = self:full_name_get()
+        buf[#buf + 1] = ": "
+        buf[#buf + 1] = self:base_type_get():serialize()
+        local val = self:value_get()
+        if val then
+            buf[#buf + 1] = " = "
+            buf[#buf + 1] = val:serialize()
+        end
+        buf[#buf + 1] = ";"
+        return table.concat(buf)
+    end,
+
+    serialize_c = function(self, ns)
+        local buf = {}
+        local bt = self:base_type_get()
+        local fulln = self:full_name_get():gsub("%.", "_"):upper()
+        keyref.add(fulln, ns, "c")
+        if self:type_get() == self.GLOBAL then
+            local ts = bt:c_type_get()
+            buf[#buf + 1] = ts
+            if ts:sub(#ts) ~= "*" then
+                buf[#buf + 1] = " "
+            end
+            buf[#buf + 1] = fulln
+            local val = self:value_get()
+            if val then
+                buf[#buf + 1] = " = "
+                local vt = val:eval_type(bt)
+                local lv = vt:to_literal()
+                local sv = val:serialize()
+                buf[#buf + 1] = lv
+                if lv ~= sv then
+                    buf[#buf + 1] = "/* " .. sv .. " */"
+                end
+            end
+            buf[#buf + 1] = ";"
+        else
+            buf[#buf + 1] = "#define "
+            buf[#buf + 1] = fulln
+            buf[#buf + 1] = " "
+            local val = self:value_get()
+            local vt = val:eval_type(bt)
+            local lv = vt:to_literal()
+            local sv = val:serialize()
+            buf[#buf + 1] = lv
+            if lv ~= sv then
+                buf[#buf + 1] = "/* " .. sv .. " */"
+            end
+        end
+        return table.concat(buf)
+    end,
+
     -- static getters
 
     all_globals_get = function()
@@ -1202,6 +1268,10 @@ M.Expression = Node:clone {
         return self.expr:eval(eolian.expression_mask.INT)
     end,
 
+    eval_type = function(self, tp)
+        return self.expr:eval_type(tp.type)
+    end,
+
     serialize = function(self)
         return self.expr:serialize()
     end
index e882d03..dc1f726 100644 (file)
@@ -840,13 +840,23 @@ local build_enum = function(tp)
 end
 
 local build_variable = function(v, constant)
-    local f = writer.Writer(v:nspaces_get(), v:full_name_get())
+    local ns = v:nspaces_get()
+    local f = writer.Writer(ns, v:full_name_get())
     if constant then
         stats.check_constant(v)
     else
         stats.check_global(v)
     end
 
+    write_tsigs(f, v, ns)
+
+    f:write_h("Description", 2)
+    f:write_raw(v:doc_get():full_get(nil, true))
+    f:write_nl(2)
+
+    f:write_editable(ns, "description")
+    f:write_nl()
+
     f:finish()
 end