summaryrefslogtreecommitdiff
path: root/CIAO/CIDLC/SizeTypeCalculator.cpp
blob: b8f44e879ed93f0bd88bf65a71dd3ff088954641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// file      : CIDLC/SizeTypeCalculator.cpp
// author    : Boris Kolpackov <boris@dre.vanderbilt.edu>
// cvs-id    : $Id$

#include "SizeTypeCalculator.hpp"
#include "Literals.hpp"

#include "CCF/CIDL/SemanticGraph.hpp"
#include "CCF/CIDL/Traversal.hpp"

#include <stack>

using namespace CCF::CIDL;
using namespace CCF::CIDL::SemanticGraph;
using namespace StringLiterals;

namespace
{
  class Calculator : public Traversal::String,
                     public Traversal::Struct,
                     public Traversal::Union,
                     public Traversal::Wstring,
                     public Traversal::UnboundedSequence,
                     public Traversal::Interface,
                     public Traversal::ValueType
  {
  public:
    Calculator ()
    {
      push (false);
    }

  public:
    virtual void
    traverse (SemanticGraph::String&)
    {
      top () = true;
    }

    virtual void
    traverse (SemanticGraph::Wstring&)
    {
      top () = true;
    }

    virtual void
    traverse (SemanticGraph::UnboundedSequence&)
    {
      top () = true;
    }

    virtual void
    traverse (SemanticGraph::Interface&)
    {
      top () = true;
    }

    virtual void
    traverse (SemanticGraph::ValueType&)
    {
      top () = true;
    }

    virtual void
    pre (SemanticGraph::Struct&)
    {
      push (false);
    }

    virtual void
    traverse (SemanticGraph::Struct& s)
    {
      std::string n (s.scoped_name ().str ());
      if (s.context ().count (STRS[VAR_SIZE]))
      {
        // Never set 'top' to false (except in pre() above),
        // so a 'true' value will propagate up the scopes.
        bool r = s.context ().get<bool> (STRS[VAR_SIZE]);
        if (r) top () = r;
      }
      else
      {
        Traversal::Struct::traverse (s);
      }
    }

    virtual void
    post (SemanticGraph::Struct& s)
    {
      // Set our context to the result of nested scope traversal.
      s.context ().set (STRS[VAR_SIZE], top ());
    }

    virtual void
    pre (SemanticGraph::Union&)
    {
      push (false);
    }

    virtual void
    traverse (SemanticGraph::Union& u)
    {
      if (u.context ().count (STRS[VAR_SIZE]))
      {
        // Never set 'top' to false (except in pre() above),
        // so a 'true' value will propagate up the scopes.
        bool r = u.context ().get<bool> (STRS[VAR_SIZE]);
        if (r) top () = r;
      }
      else
      {
        Traversal::Union::traverse (u);
      }
    }

    virtual void
    post (SemanticGraph::Union& u)
    {
      // Set our context to the result of nested scope traversal.
      u.context ().set (STRS[VAR_SIZE], top ());
    }

  private:
    void
    push (bool v)
    {
      stack_.push (v);
    }

    bool&
    top ()
    {
      return stack_.top ();
    }

    void
    pop ()
    {
      stack_.pop ();
    }

  private:
    std::stack<bool> stack_;
  };
}


void SizeTypeCalculator::
calculate (SemanticGraph::TranslationUnit& u)
{
  Traversal::TranslationUnit unit;

  // Layer 1
  //
  Traversal::ContainsPrincipal contains_principal;
  unit.edge_traverser (contains_principal);

  //--
  Traversal::TranslationRegion region;
  contains_principal.node_traverser (region);

  // Layer 2
  //
  Traversal::ContainsRoot contains_root;
  Traversal::Includes includes;

  region.edge_traverser (includes);
  region.edge_traverser (contains_root);

  //--
  Traversal::Root root;
  includes.node_traverser (region);
  contains_root.node_traverser (root);

  // Layer 3
  //
  Traversal::Defines defines;
  root.edge_traverser (defines);

  //--
  // Everything that can contain a struct or union.
  Traversal::Module module;
  Traversal::UnconstrainedInterface uinterface;
  Traversal::ValueType vtype;
  Traversal::Home component_home;

  Calculator calculator;

  defines.node_traverser (module);
  defines.node_traverser (uinterface);
  defines.node_traverser (vtype);
  defines.node_traverser (component_home);
  defines.node_traverser (calculator);

  // Layer 4
  //
  Traversal::Defines struct_defines;
  module.edge_traverser (defines);
  uinterface.edge_traverser (defines);
  vtype.edge_traverser (defines);
  component_home.edge_traverser (defines);
  calculator.edge_traverser (struct_defines);

  //--
  Traversal::Member member;
  struct_defines.node_traverser (member);

  // Layer 5
  //
  Traversal::Belongs belongs;
  member.edge_traverser (belongs);

  //--
  belongs.node_traverser (calculator);

  unit.traverse (u);
}