Class: C::Declaration

Inherits:
Object
  • Object
show all
Defined in:
lib/cast-to-yaml/to_yaml.rb

Instance Method Summary collapse

Instance Method Details

#extract(res = Hash::new { |h, k| h[k] = [] }, declarations: true) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cast-to-yaml/to_yaml.rb', line 67

def extract(res = Hash::new { |h, k| h[k] = [] }, declarations: true)
  if typedef?
    declarators.each { |d|
      res["typedefs"].push d.to_h_split(self)
    }
  else
    declarators.each { |d|
      if d.indirect_type && d.indirect_type.kind_of?(Function)
        f = {}
        f["name"] = d.name
        if d.indirect_type.type
          f["type"] = d.indirect_type.type.to_h_split(self)
        else
          f["type"] = type.to_h_split(self)
        end
        if d.indirect_type.params
          f["params"] = d.indirect_type.params.collect { |p| p.to_h_split }
        end
        if d.indirect_type.var_args?
          f["var_args"] = true
        end
        if inline?
          f["inline"] = true
        end
        if storage
          f["storage"] = storage.to_s
        end
        
        res["functions"].push f
      elsif declarations
        r = d.to_h_split(self)
        r["storage"] = storage.to_s if storage
        r["inline"] = true if inline?
        res["declarations"].push r
      end
    }
  end
  if type.kind_of?(Struct) && type.members
    m = []
    type.members.each { |mem|
      mem.extract(res, declarations: false)
      m += mem.to_a
    }
    if type.name
      s = {}
      s["name"] = type.name
      s["members"] = m
      res["structs"].push s
    end
  elsif type.kind_of?(Enum) && type.members && (type.name || (declarators.empty? && declarations))
    s = {}
    s["name"] = type.name if type.name
    m = []
    type.members.each { |mem|
      m.push mem.to_h_split
    }
    s["members"] = m
    res["enums"].push s
  elsif type.kind_of?(Union) && type.members
    m = []
    type.members.each { |mem|
      mem.extract(res, declarations: false)
      m += mem.to_a
    }
    if type.name
      s = {}
      s["name"] = type.name
      s["members"] = m
      res["unions"].push s
    end
  end
  res
end

#to_aObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cast-to-yaml/to_yaml.rb', line 51

def to_a
  # Anonymous nested composites
  if declarators.empty?
    if (type.kind_of?(Struct) || type.kind_of?(Union)) && type.members && !type.name
      [{"type" => self.type.to_h_split}]
    end
  else
    declarators.collect { |d|
      res = d.to_h_split(self)
      res["storage"] = storage.to_s if storage
      res["inline"] = true if inline?
      res
    }
  end
end