summaryrefslogtreecommitdiff
path: root/TAO/CIAO/CCF/CCF/IDL2/SemanticAction/Impl/Interface.hpp
blob: 8351d30a837e9c3c7d22f0c3771be7a564db005f (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
// file      : CCF/IDL2/SemanticAction/Impl/Interface.hpp
// author    : Boris Kolpackov <boris@dre.vanderbilt.edu>
// cvs-id    : $Id$

#ifndef CCF_IDL2_SEMANTIC_ACTION_IMPL_INTERFACE_HPP
#define CCF_IDL2_SEMANTIC_ACTION_IMPL_INTERFACE_HPP

#include "CCF/IDL2/SemanticAction/Impl/Elements.hpp"

#include "CCF/IDL2/SemanticAction/Interface.hpp"

namespace CCF
{
  namespace IDL2
  {
    namespace SemanticAction
    {
      namespace Impl
      {
        //
        //
        //
        class Interface : public virtual SemanticAction::Interface,
                          public virtual ScopeBase<SyntaxTree::InterfaceDeclPtr>
        {
        public:
          virtual
          ~Interface () throw () {}

          Interface (bool trace,
                     SyntaxTree::ScopePtr& scope)
              : ScopeBase<SyntaxTree::InterfaceDeclPtr> (scope),
                trace_ (trace),
                name_ ("::") //@@ this is dirty
          {
          }

          virtual void
          begin_abstract (SimpleIdentifierPtr const& id)
          {
            if (trace_) cerr << "abstract interface " << id << endl;

            qualifier_ = Qualifier::ABSTRACT;
            name_ = SyntaxTree::SimpleName (id->lexeme ());
          }


          virtual void
          begin_local (SimpleIdentifierPtr const& id)
          {
            if (trace_) cerr << "local interface " << id << endl;

            qualifier_ = Qualifier::LOCAL;
            name_ = SyntaxTree::SimpleName (id->lexeme ());
          }

          virtual void
          begin_unconstrained (SimpleIdentifierPtr const& id)
          {
            if (trace_) cerr << "unconstrained interface " << id << endl;

            qualifier_ = Qualifier::UNCONSTRAINED;
            name_ = SyntaxTree::SimpleName (id->lexeme ());
          }

          virtual void
          inherits (IdentifierPtr const& id)
          {
            using namespace SyntaxTree;

            if (trace_) cerr << " inherits: " << id << endl;

            Name name (id->lexeme ());

            struct InheritancePredicate : public DeclarationTable::ResolvePredicate
            {
              InheritancePredicate (Qualifier::Value q) : q_ (q) {}

              struct IncompatibleType :
                public DeclarationTable::ResolutionFailure
              {
                IncompatibleType (std::string const& t) : type (t) {}
                std::string type;
              };

              virtual bool
              test (DeclarationPtr const& d) const throw (IncompatibleType)
              {
                bool passed = false;

                switch (q_)
                {
                case Qualifier::UNCONSTRAINED:
                  {
                    if (d->is_a<AbstractInterfaceDecl> () ||
                        d->is_a<UnconstrainedInterfaceDecl> ()) passed = true;

                    break;
                  }
                case Qualifier::LOCAL:
                  {
                    if (d->is_a<LocalInterfaceDecl> () ||
                        d->is_a<AbstractInterfaceDecl> () ||
                        d->is_a<UnconstrainedInterfaceDecl> ()) passed = true;

                    break;
                  }
                case Qualifier::ABSTRACT:
                  {
                    if (d->is_a<AbstractInterfaceDecl> ()) passed = true;

                    break;
                  }
                }

                if (!passed) throw IncompatibleType (d->declaration_class ());

                return d->dynamic_type<TypeDecl> ()->defined ();
              }

            private:
              Qualifier::Value q_;
            } p (qualifier_);

            try
            {
              ScopedName sn = scope_->table ().resolve (
                name,
                scope_->name (),
                scope_->peek_order (),
                p);

              if (inherits_.insert (sn).second == false)
              {
                cerr << "error: invalid inheritance specification" << endl;
                cerr << "directly inheriting from interface \'"
                     << sn << "\' more than once is illegal" << endl;
              }
            }
            catch (InheritancePredicate::IncompatibleType const& e)
            {
              cerr << "error: invalid inheritance specification" << endl;

              cerr << "inheritance of ";

              switch (qualifier_)
              {
              case Qualifier::UNCONSTRAINED:
                {
                  cerr << "unconstrained ";
                  break;
                }
              case Qualifier::LOCAL:
                {
                  cerr << "local ";
                  break;
                }
              case Qualifier::ABSTRACT:
                {
                  cerr << "abstract ";
                  break;
                }
              }

              cerr << "interface \'" << name_ <<"\' from "
                   << e.type << " \'" << name << "\' is illegal" << endl;
            }
            catch (DeclarationTable::NameNotFound const&)
            {
              cerr << "error: invalid inheritance specification" << endl;
              cerr << "no interface with name \'"
                   << name << "\' visible from scope \'"
                   << scope_->name () << "\'" << endl;
            }
            catch (DeclarationTable::PredicateNotMet const&)
            {
              cerr << "error: invalid inheritance specification" << endl;
              cerr << "no defined interface with name \'"
                   << name << "\' visible from scope \'"
                   << scope_->name () << "\'" << endl;
              cerr << "inheritance from forward-declared interface is illegal"
                   << endl;
            }
          }

          virtual void
          open_scope ()
          {
            if (trace_) cerr << "scope open" << endl;

            using namespace SyntaxTree;

            InterfaceDefPtr def;

            switch (qualifier_)
            {
            case Qualifier::UNCONSTRAINED:
              {
                def = new UnconstrainedInterfaceDef (name_, scope_, inherits_);
                break;
              }
            case Qualifier::LOCAL:
              {
                def = new LocalInterfaceDef (name_, scope_, inherits_);
                break;
              }
            case Qualifier::ABSTRACT:
              {
                def = new AbstractInterfaceDef (name_, scope_, inherits_);
                break;
              }
            }

            scope_->insert (def);
            push (def);
            scope_ = def;

            name_ = SimpleName ("::"); //indicate that we are done
            inherits_.clear ();
          }

          virtual void
          close_scope ()
          {
            scope_ = scope_->scope ();
            if (trace_) cerr << "scope close" << endl;
          }

          virtual void
          end ()
          {
            using namespace SyntaxTree;

            if (trace_) cerr << "end" << endl;

            if (name_ != SimpleName ("::"))
            {
              InterfaceDeclPtr decl;

              switch (qualifier_)
              {
              case Qualifier::UNCONSTRAINED:
                {
                  decl = new UnconstrainedInterfaceForwardDecl (name_, scope_);
                  break;
                }
              case Qualifier::LOCAL:
                {
                  decl = new LocalInterfaceForwardDecl (name_, scope_);
                  break;
                }
              case Qualifier::ABSTRACT:
                {
                  decl = new AbstractInterfaceForwardDecl (name_, scope_);
                  break;
                }
              }

              scope_->insert (decl);
            }
            else
            {
              pop ();
            }
          }
        private:

          struct Qualifier
          {
            enum Value
            {
              ABSTRACT,
              LOCAL,
              UNCONSTRAINED
            };
          };

          bool trace_;

          Qualifier::Value qualifier_;
          SyntaxTree::SimpleName name_;
          SyntaxTree::ScopedNameSet inherits_;
        };

      }
    }
  }
}

#endif  // CCF_IDL2_SEMANTIC_ACTION_IMPL_INTERFACE_HPP