summaryrefslogtreecommitdiff
path: root/TAO/CIAO/CIDLC/RepositoryIdGenerator.cpp
blob: 408bca2147c1fddb5176a0858f35ea7c702f62fc (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
// 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/SyntaxTree.hpp"
#include "CCF/CIDL/Traversal.hpp"

using namespace CCF::CIDL;
using namespace SyntaxTree;

namespace
{
  char const* const
  type_id_label = "CIDLC::RepositoryIdGenerator::TypeId";

  char const* const
  type_prefix_label = "CIDLC::RepositoryIdGenerator::TypePrefix";
}

namespace
{
  //
  //
  //
  class TypeIdLabelGenerator : public Traversal::TypeId
  {
  public:
    TypeIdLabelGenerator (Diagnostic::Stream& dout)
        : dout_ (dout)
    {
    }

    virtual void
    traverse (TypeIdPtr const& ti)
    {
      ScopedName decl_name (ti->declaration ());

      DeclarationTable::IteratorPair iters (
        ti->table ().lookup (decl_name));

      for (; iters.first != iters.second; ++iters.first)
      {
        DeclarationPtr decl (*iters.first);

        if (decl->context ().count (
              StringLiterals::STRS[StringLiterals::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 (
            StringLiterals::STRS[StringLiterals::TYPE_ID], ti);
          decl->context ().set (
            StringLiterals::STRS[StringLiterals::REPO_ID], ti->id ().str ());
        }
      }
    }

  private:
    Diagnostic::Stream& dout_;
  };


  //
  //
  //
  class TypePrefixLabelGenerator : public Traversal::TypePrefix
  {
  public:
    TypePrefixLabelGenerator (Diagnostic::Stream& dout)
        : dout_ (dout)
    {
    }

    virtual void
    traverse (TypePrefixPtr const& tp)
    {
      ScopedName decl_name (tp->declaration ());

      DeclarationTable::IteratorPair iters (
        tp->table ().lookup (decl_name));

      for (; iters.first != iters.second; ++iters.first)
      {
        DeclarationPtr decl (*iters.first);

        if (decl->context ().count (
              StringLiterals::STRS[StringLiterals::TYPE_PREFIX]))
        {
          TypePrefixPtr prev (
            decl->context ().get<TypePrefixPtr> (
              StringLiterals::STRS[StringLiterals::TYPE_PREFIX]));

          if ((prev->prefix ()) != (tp->prefix ()))
          {

            //@@ Seems tp should containt 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 (
            StringLiterals::STRS[StringLiterals::TYPE_PREFIX], tp);
        }
      }
    }

  private:
    Diagnostic::Stream& dout_;
  };
}


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

    TypeIdLabelGenerator type_id (dout);
    TypePrefixLabelGenerator type_prefix (dout);

    Traversal::Scope scope;
    scope.add_scope_delegate (&type_id);
    scope.add_scope_delegate (&type_prefix);

    Traversal::TranslationRegion region (&scope);

    Traversal::TranslationUnit unit;
    unit.add_content_delegate (&region);

    unit.dispatch (u);

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

    //@@ check errors
  }

  return true;
}