summaryrefslogtreecommitdiff
path: root/TAO/CIAO/CIDLC/RepositoryIdGenerator.cpp
blob: 073aa844f7712aa2d7d634be304a40da1a5cb123 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// file      : CIDLC/RepositoryIdGenerator.cpp
// author    : Boris Kolpackov <boris@dre.vanderbilt.edu>
// cvs-id    : $Id$

#include "RepositoryIdGenerator.hpp"
#include "Literals.hpp"

#include "CCF/CompilerElements/Diagnostic.hpp"
#include "CCF/CIDL/Traversal.hpp"
#include "CCF/CodeGenerationKit/Regex.hpp"

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

namespace
{
  string
  name_to_string (Name const& n)
  {
    ostringstream os;
    os << n;
    return os.str ();
  }

  Nameable* defined_in (Nameable& n)
  {
    for (Nameable::NamedIterator i (n.named_begin ()), e (n.named_end ());
        i != e;
        ++i)
    {
      if (Defines* d = dynamic_cast<Defines*> (*i))
      {
        return &d->scope ();
      }
    }

    return 0;
  }
  
  void
  compute_new_repo_id (Nameable& d)
  {
    string prefix ("");
    TypePrefix *tp = 0;

    if (d.context ().count (STRS[TYPE_PREFIX]))
    {
      tp = d.context ().get<TypePrefix*> (STRS[TYPE_PREFIX]);
      prefix = tp->prefix ().literal ();
    }
    else
    {
      Nameable* parent = defined_in (d);

      while (parent != 0)
      {
        if (parent->context ().count (STRS[TYPE_PREFIX]))
        {
          tp =
            parent->context ().get<TypePrefix*> (STRS[TYPE_PREFIX]);

          prefix = tp->prefix ().literal ();
          break;
        }

        parent = defined_in (*parent);
      }
    }

    if (prefix != "") prefix += "/";

    ScopedName scoped (d.scoped_name ());
    Name stripped (scoped.begin () + 1, scoped.end ());

    string scope_name = regex::perl_s (name_to_string (stripped), "%::%/%");

    string repo_id = "IDL:" + prefix + scope_name + ":1.0";

    d.context ().set<string> (STRS[REPO_ID], repo_id);
  }

  void
  compute_repo_id (Nameable& d)
  {
    if (d.context ().count (STRS[REPO_ID]))
    {
      return;
    }
    else
    {
      compute_new_repo_id (d);
    }
  }
}

namespace
{
  class TypeIdLabelGenerator : public Traversal::TypeId
  {
  public:
    TypeIdLabelGenerator (Diagnostic::Stream& dout,
                          TranslationUnit& tu)
      : dout_ (dout),
        tu_ (tu)
    {
    }

    virtual void
    traverse (SemanticGraph::TypeId& ti)
    {
      ScopedName decl_name (ti.declaration ());
      Nameables ref = tu_.lookup (decl_name);

      for (Nameables::const_iterator iter (ref.begin ());
           iter != ref.end ();
           ++iter)
      {
        Nameable *decl = *iter;

        if (decl->context ().count (STRS[TYPE_ID]))
        {
          //@@ Seems it should contain filename and line
          //   of its origin.
          //
          Diagnostic::Error err ("???", 0);

          err << "repetition of typeid for " << decl_name
              << " is illegal";

          dout_ << err;

          Diagnostic::Info inf ("???", 0);

          err << "original typeid here";

          dout_ << inf;
        }
        else
        {
          decl->context ().set (STRS[TYPE_ID], &ti);
          decl->context ().set (STRS[REPO_ID], ti.id ().literal ());
        }
      }
    }

  private:
    Diagnostic::Stream& dout_;
    SemanticGraph::TranslationUnit& tu_;
  };

  class TypePrefixLabelGenerator : public Traversal::TypePrefix
  {
  public:
    TypePrefixLabelGenerator (Diagnostic::Stream& dout,
                              TranslationUnit& tu)
      : dout_ (dout),
        tu_ (tu)
    {
    }

    virtual void
    traverse (SemanticGraph::TypePrefix& tp)
    {
      ScopedName decl_name (tp.declaration ());
      Nameables ref = tu_.lookup (decl_name);

      for (Nameables::const_iterator iter (ref.begin ());
           iter != ref.end ();
           ++iter)
      {
        Nameable *decl = *iter;
        CCF::CompilerElements::Context& ctx = decl->context ();

        if (ctx.count (STRS[TYPE_PREFIX]))
        {
          SemanticGraph::TypePrefix* prev =
            ctx.get<SemanticGraph::TypePrefix*> (STRS[TYPE_PREFIX]);
        
          if (prev->prefix () != tp.prefix ())
          {
            //@@ Seems tp should contain filename and line
            //   of it's origin.
            //
            Diagnostic::Error err ("???", 0);

            err << "resetting type prefix for " << decl_name
                << " is illegal";

            dout_ << err;

            Diagnostic::Info inf ("???", 0);

            err << "original typeprefix here";

            dout_ << inf;
          }
        }
        else
        {
          decl->context ().set (STRS[TYPE_PREFIX], &tp);
          
          // A typeprefix declaration could appear after the IDL
          // type declaration, so we want to update the repo id
          // every time.
          compute_new_repo_id (*decl);
        }
      }
    }

  private:
    Diagnostic::Stream& dout_;
    SemanticGraph::TranslationUnit& tu_;
  };
  
  class RepoIdGenerator : public Traversal::Interface,
                          public Traversal::EventType,
                          public Traversal::Home,
                          public Traversal::Component
  {
    virtual void
    traverse (SemanticGraph::Interface& i)
    {
      compute_repo_id (i);
    }
    
    virtual void
    traverse (SemanticGraph::EventType& e)
    {
      compute_repo_id (e);
    }
    
    virtual void
    traverse (SemanticGraph::Home& h)
    {
      compute_repo_id (h);
    }
    
    virtual void
    traverse (SemanticGraph::Component& c)
    {
      compute_repo_id (c);
    }
  };
}


bool RepositoryIdGenerator::generate (TranslationUnit& u)
{
  Diagnostic::Stream dout;

  TypeIdLabelGenerator type_id (dout, u);
  TypePrefixLabelGenerator type_prefix (dout, u);
  RepoIdGenerator repo_id;

  Traversal::TranslationUnit unit;

  Traversal::ContainsPrincipal contains_principal;
  unit.edge_traverser (contains_principal);

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

  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);

  Traversal::Defines defines;
  root.edge_traverser (defines);

  Traversal::Module module;
  
  defines.node_traverser (module);
  defines.node_traverser (type_id);
  defines.node_traverser (type_prefix);
  defines.node_traverser (repo_id);
  
  module.edge_traverser (defines);
  
  Traversal::Interface iface;
  defines.node_traverser (iface);
  
  unit.traverse (u);

  if (dout.error_count () != 0) return false;

  //@@ check errors

  return true;
}