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

#ifndef CCF_IDL2_SEMANTIC_ACTION_IMPL_TYPE_ID_HPP
#define CCF_IDL2_SEMANTIC_ACTION_IMPL_TYPE_ID_HPP

#include "CCF/IDL2/SyntaxTree/TypeId.hpp"

// These inclusions are needed to ensure that typeprefix is applied to a
// suitable declaration.

#include "CCF/IDL2/SyntaxTree/Module.hpp"
#include "CCF/IDL2/SyntaxTree/Interface.hpp"
#include "CCF/IDL2/SyntaxTree/ValueType.hpp"


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

#include <sstream>

namespace CCF
{
  namespace IDL2
  {
    namespace SemanticAction
    {
      namespace Impl
      {
        //
        //
        //
        class TypeId : public virtual SemanticAction::TypeId
        {
        public:
          virtual
          ~TypeId () throw () {}

          TypeId (bool trace, SyntaxTree::ScopePtr& current)
              : trace_ (trace),
                scope_ (current)
          {
          }

          virtual void
          begin (IdentifierPtr const& d, StringLiteralPtr const& id)
          {
            if (trace_) cerr << "typeid " << d << " " << id << endl;

            using namespace SyntaxTree;

            Name dn (d->lexeme ());

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

              Order order = scope_->create_order ();

              std::ostringstream ostr;
              ostr << order;

              SimpleName name (ostr.str ());

              TypeIdPtr ti (
                new SyntaxTree::TypeId (
                  name,
                  order,
                  scope_,
                  dsn,
                  SyntaxTree::StringLiteral (id->lexeme ())));

              scope_->insert (ti);

              if (trace_) cerr << "assigned declaration name " << ti->name ()
                               << endl;
            }
            catch (DeclarationTable::NameNotFound const&)
            {
              cerr << "error: invalid typeid declaration" << endl;
              cerr << "no declaration with name \'"
                   << dn << "\' visible from scope \'"
                   << scope_->name () << "\'" << endl;
            }
          }

          virtual void
          end ()
          {
            if (trace_) cerr << "end" << endl;
          }

        private:
          bool trace_;
          SyntaxTree::ScopePtr& scope_;
        };


        //
        //
        //
        class TypePrefix : public virtual SemanticAction::TypePrefix
        {
        public:
          virtual
          ~TypePrefix () throw () {}

          TypePrefix (bool trace, SyntaxTree::ScopePtr& current)
              : trace_ (trace),
                scope_ (current)
          {
          }

          virtual void
          begin (IdentifierPtr const& d, StringLiteralPtr const& prefix)
          {
            if (trace_) cerr << "typeprefix " << d << " " << prefix << endl;

            using namespace SyntaxTree;

            Name dn (d->lexeme ());

            try
            {
              struct Predicate : public DeclarationTable::ResolvePredicate
              {
                virtual bool
                test (DeclarationPtr const& d) const throw ()
                {
                  return
                       d->is_a<SyntaxTree::Module> ()
                    || d->is_a<SyntaxTree::FileScope> ()
                    || d->is_a<SyntaxTree::InterfaceDecl> ()
                    || d->is_a<SyntaxTree::ValueTypeDecl> ();
                }
              } p;

              ScopedName dsn = scope_->table ().resolve (
                dn,
                scope_->name (),
                scope_->peek_order (),
                p);

              Order order = scope_->create_order ();

              std::ostringstream ostr;
              ostr << order;

              SimpleName name (ostr.str ());

              TypePrefixPtr tp (
                new SyntaxTree::TypePrefix (
                  name,
                  order,
                  scope_,
                  dsn,
                  SyntaxTree::StringLiteral (prefix->lexeme ())));

              scope_->insert (tp);

              if (trace_) cerr << "assigned declaration name " << tp->name ()
                               << endl;
            }
            catch (DeclarationTable::NameNotFound const&)
            {
              cerr << "error: invalid typeprefix declaration" << endl;
              cerr << "no declaration with name \'"
                   << dn << "\' visible from scope \'"
                   << scope_->name () << "\'" << endl;
            }
            catch (DeclarationTable::PredicateNotMet const&)
            {
              cerr << "error: invalid typeprefix declaration" << endl;
              cerr << "no suitable declaration with name \'"
                   << dn << "\' visible from scope \'"
                   << scope_->name () << "\'" << endl;
            }
          }

          virtual void
          end ()
          {
            if (trace_) cerr << "end" << endl;
          }

        private:
          bool trace_;
          SyntaxTree::ScopePtr& scope_;
        };
      }
    }
  }
}

#endif  // CCF_IDL2_SEMANTIC_ACTION_IMPL_TYPE_ID_HPP