summaryrefslogtreecommitdiff
path: root/TAO_IDL/ast/ast_template_module.cpp
blob: e362f620737c149006fb82e8be5d43acaa7225c8 (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
// $Id$

#include "ast_template_module.h"
#include "ast_template_module_ref.h"
#include "ast_constant.h"
#include "ast_enum.h"
#include "ast_typedef.h"
#include "ast_visitor.h"

#include "utl_err.h"
#include "utl_string.h"
#include "utl_strlist.h"
#include "global_extern.h"
#include "nr_extern.h"

AST_Decl::NodeType const
AST_Template_Module::NT = AST_Decl::NT_module;

AST_Template_Module::AST_Template_Module (
      UTL_ScopedName *n,
      FE_Utils::T_PARAMLIST_INFO *template_params)
  : COMMON_Base (false,
                 false),
    AST_Decl (AST_Decl::NT_module,
              n),
    UTL_Scope (AST_Decl::NT_module),
    AST_Module (n),
    AST_Type (AST_Decl::NT_module,
              n),
    template_params_ (template_params)
{
}

AST_Template_Module::~AST_Template_Module (void)
{
}

FE_Utils::T_PARAMLIST_INFO *
AST_Template_Module::template_params (void) const
{
  return this->template_params_;
}

bool
AST_Template_Module::match_arg_names (FE_Utils::T_ARGLIST *args)
{
  if (args->size () != this->template_params_->size ())
    {
      idl_global->err ()->error1 (UTL_Error::EIDL_T_ARG_LENGTH,
                                  this);
      return false;
    }

  size_t slot = 0UL;

  for (FE_Utils::T_ARGLIST::CONST_ITERATOR i (*args);
       !i.done ();
       i.advance (), ++slot)
    {
      AST_Decl **item = 0;
      i.next (item);
      AST_Decl *d = *item;

      if (d->node_type () == AST_Decl::NT_typedef)
        {
          AST_Typedef *td =
            AST_Typedef::narrow_from_decl (d);

          d = td->primitive_base_type ();
        }

      FE_Utils::T_Param_Info *param = 0;
      (void) this->template_params_->get (param, slot);
      const char *s = 0;

      if (! this->match_one_param (param, d))
        {
          UTL_ScopedName *n = d->name ();

          if (n == 0)
            {
              AST_Constant *c =
                AST_Constant::narrow_from_decl (d);

              s = c->exprtype_to_string ();
            }
          else
            {
              s = d->full_name ();
            }

          idl_global->err ()->mismatched_template_param (s);

          return false;
        }
    }

  return true;
}

bool
AST_Template_Module::match_param_refs (UTL_StrList *refs,
                                       UTL_Scope *decl_scope)
{
  UTL_Scope *s = decl_scope;
  AST_Template_Module *enclosing = 0;

  while (enclosing == 0 && s != 0)
    {
      enclosing = AST_Template_Module::narrow_from_scope (s);
      s = ScopeAsDecl (s)->defined_in ();
    }

  for (UTL_StrlistActiveIterator i (refs);
       !i.is_done ();
       i.next ())
    {
      FE_Utils::T_Param_Info *enclosing_param =
        enclosing->find_param (i.item ());

      if (enclosing_param == 0)
        {
          // Enclosing param not found
          return false;
        }

      if (!this->match_param_by_type (enclosing_param))
        {
          // Referenced param type not matched to enclosiong param.
          return false;
        }
    }

  return true;
}

void
AST_Template_Module::destroy (void)
{
  delete this->template_params_;
  this->template_params_ = 0;

  this->AST_Module::destroy ();
}

int
AST_Template_Module::ast_accept (ast_visitor *visitor)
{
  return visitor->visit_template_module (this);
}

AST_Template_Module_Ref *
AST_Template_Module::fe_add_template_module_ref (
  AST_Template_Module_Ref *m)
{
  return
    AST_Template_Module_Ref::narrow_from_decl (
      this->fe_add_ref_decl (m));
}

void
AST_Template_Module::dump (ACE_OSTREAM_TYPE & /* o */)
{
}

bool
AST_Template_Module::match_one_param (FE_Utils::T_Param_Info *param,
                                      AST_Decl *d)
{
  if (param->type_ == AST_Decl::NT_type)
    {
      return true;
    }

  if (d->node_type () == AST_Decl::NT_typedef)
    {
      AST_Typedef *td = AST_Typedef::narrow_from_decl (d);
      d = td->primitive_base_type ();
    }

  AST_Decl::NodeType other_type = d->node_type ();

  if (other_type == AST_Decl::NT_const)
    {
      AST_Constant *c =
        AST_Constant::narrow_from_decl (d);

      AST_Expression *ex = c->constant_value ();

      AST_Expression::AST_ExprValue *ev =
        ex->check_and_coerce (param->const_type_,
                              param->enum_const_type_decl_);

      if (ev == 0)
        {
          idl_global->err ()->coercion_error (ex,
                                              param->const_type_);
        }

      return (ev != 0);
    }

  return (param->type_ == other_type);
}

FE_Utils::T_Param_Info *
AST_Template_Module::find_param (UTL_String *name)
{
  for (FE_Utils::T_PARAMLIST_INFO::CONST_ITERATOR i (
         *this->template_params_);
       !i.done ();
       i.advance ())
    {
      FE_Utils::T_Param_Info *param = 0;
      i.next (param);

      if (param->name_ == name->get_string ())
        {
          return param;
        }
    }

  return 0;
}

bool
AST_Template_Module::match_param_by_type (
  FE_Utils::T_Param_Info *param)
{
  for (FE_Utils::T_PARAMLIST_INFO::CONST_ITERATOR i (
         *this->template_params_);
       !i.done ();
       i.advance ())
    {
      FE_Utils::T_Param_Info *my_param = 0;
      i.next (my_param);

      if (param->type_ == my_param->type_)
        {
          if (param->type_ == AST_Decl::NT_const)
            {
              if (param->const_type_ == my_param->const_type_)
                {
                  if (param->const_type_ == AST_Expression::EV_enum)
                    {
                      if (param->enum_const_type_decl_
                            == my_param->enum_const_type_decl_)
                        {
                          return true;
                        }
                    }
                  else
                    {
                      return true;
                    }
                }
            }
          else
            {
              return true;
            }
        }
    }

  idl_global->err ()->mismatched_template_param (
    param->name_.c_str ());

  return false;
}

IMPL_NARROW_FROM_DECL (AST_Template_Module)
IMPL_NARROW_FROM_SCOPE (AST_Template_Module)