Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / TAO_IDL / ast / ast_template_module.cpp
blob0e1861578c80d920d69ef394c7fc8f2b385f51c9
1 #include "ast_template_module.h"
2 #include "ast_template_module_ref.h"
3 #include "ast_constant.h"
4 #include "ast_enum.h"
5 #include "ast_typedef.h"
6 #include "ast_visitor.h"
8 #include "utl_err.h"
9 #include "utl_string.h"
10 #include "utl_strlist.h"
11 #include "global_extern.h"
12 #include "nr_extern.h"
14 AST_Decl::NodeType const
15 AST_Template_Module::NT = AST_Decl::NT_module;
17 AST_Template_Module::AST_Template_Module (
18 UTL_ScopedName *n,
19 FE_Utils::T_PARAMLIST_INFO *template_params)
20 : COMMON_Base (false,
21 false),
22 AST_Decl (AST_Decl::NT_module,
23 n),
24 UTL_Scope (AST_Decl::NT_module),
25 AST_Module (n),
26 AST_Type (AST_Decl::NT_module,
27 n),
28 template_params_ (template_params)
32 AST_Template_Module::~AST_Template_Module (void)
36 FE_Utils::T_PARAMLIST_INFO *
37 AST_Template_Module::template_params (void) const
39 return this->template_params_;
42 bool
43 AST_Template_Module::match_arg_names (FE_Utils::T_ARGLIST *args)
45 if (args->size () != this->template_params_->size ())
47 idl_global->err ()->error1 (UTL_Error::EIDL_T_ARG_LENGTH,
48 this);
49 return false;
52 size_t slot = 0UL;
54 for (FE_Utils::T_ARGLIST::CONST_ITERATOR i (*args);
55 !i.done ();
56 i.advance (), ++slot)
58 AST_Decl **item = 0;
59 i.next (item);
60 AST_Decl *d = *item;
62 if (d->node_type () == AST_Decl::NT_typedef)
64 AST_Typedef *td = dynamic_cast<AST_Typedef*> (d);
66 d = td->primitive_base_type ();
69 FE_Utils::T_Param_Info *param = 0;
70 (void) this->template_params_->get (param, slot);
71 const char *s = 0;
73 if (! this->match_one_param (param, d))
75 UTL_ScopedName *n = d->name ();
77 if (n == 0)
79 AST_Constant *c = dynamic_cast<AST_Constant*> (d);
81 s = c->exprtype_to_string ();
83 else
85 s = d->full_name ();
88 idl_global->err ()->mismatched_template_param (s);
90 return false;
94 return true;
97 bool
98 AST_Template_Module::match_param_refs (UTL_StrList *refs,
99 UTL_Scope *decl_scope)
101 UTL_Scope *s = decl_scope;
102 AST_Template_Module *enclosing = 0;
104 while (enclosing == 0 && s != 0)
106 enclosing = dynamic_cast<AST_Template_Module*> (s);
107 s = ScopeAsDecl (s)->defined_in ();
110 if (enclosing == 0)
112 ACE_ERROR_RETURN ((LM_ERROR,
113 ACE_TEXT ("AST_Template_Module::")
114 ACE_TEXT ("match_param_refs - ")
115 ACE_TEXT ("enclosing template ")
116 ACE_TEXT ("module scope not found\n")),
117 false);
120 for (UTL_StrlistActiveIterator i (refs);
121 !i.is_done ();
122 i.next ())
124 FE_Utils::T_Param_Info *enclosing_param =
125 enclosing->find_param (i.item ());
127 if (enclosing_param == 0)
129 // Enclosing param not found
130 return false;
133 if (!this->match_param_by_type (enclosing_param))
135 // Referenced param type not matched to enclosiong param.
136 return false;
140 return true;
143 void
144 AST_Template_Module::destroy (void)
146 delete this->template_params_;
147 this->template_params_ = 0;
149 this->AST_Module::destroy ();
153 AST_Template_Module::ast_accept (ast_visitor *visitor)
155 return visitor->visit_template_module (this);
158 AST_Template_Module_Ref *
159 AST_Template_Module::fe_add_template_module_ref (
160 AST_Template_Module_Ref *m)
162 return dynamic_cast<AST_Template_Module_Ref*> (this->fe_add_ref_decl (m));
165 void
166 AST_Template_Module::dump (ACE_OSTREAM_TYPE & /* o */)
170 bool
171 AST_Template_Module::match_one_param (FE_Utils::T_Param_Info *param,
172 AST_Decl *d)
174 if (param->type_ == AST_Decl::NT_type)
176 return true;
179 if (d->node_type () == AST_Decl::NT_typedef)
181 AST_Typedef *td = dynamic_cast<AST_Typedef*> (d);
182 d = td->primitive_base_type ();
185 AST_Decl::NodeType other_type = d->node_type ();
187 if (other_type == AST_Decl::NT_const)
189 AST_Constant *c = dynamic_cast<AST_Constant*> (d);
191 AST_Expression *ex = c->constant_value ();
193 AST_Expression::AST_ExprValue *ev =
194 ex->check_and_coerce (param->const_type_,
195 param->enum_const_type_decl_);
197 if (ev == 0)
199 idl_global->err ()->coercion_error (ex,
200 param->const_type_);
203 bool retval = (ev != 0);
204 delete ev;
205 ev = 0;
206 return retval;
209 return (param->type_ == other_type);
212 FE_Utils::T_Param_Info *
213 AST_Template_Module::find_param (UTL_String *name)
215 for (FE_Utils::T_PARAMLIST_INFO::CONST_ITERATOR i (
216 *this->template_params_);
217 !i.done ();
218 i.advance ())
220 FE_Utils::T_Param_Info *param = 0;
221 i.next (param);
223 if (param->name_ == name->get_string ())
225 return param;
229 return 0;
232 bool
233 AST_Template_Module::match_param_by_type (
234 FE_Utils::T_Param_Info *param)
236 for (FE_Utils::T_PARAMLIST_INFO::CONST_ITERATOR i (
237 *this->template_params_);
238 !i.done ();
239 i.advance ())
241 FE_Utils::T_Param_Info *my_param = 0;
242 i.next (my_param);
244 if (param->type_ == my_param->type_)
246 if (param->type_ == AST_Decl::NT_const)
248 if (param->const_type_ == my_param->const_type_)
250 if (param->const_type_ == AST_Expression::EV_enum)
252 if (param->enum_const_type_decl_
253 == my_param->enum_const_type_decl_)
255 return true;
258 else
260 return true;
264 else
266 return true;
271 idl_global->err ()->mismatched_template_param (
272 param->name_.c_str ());
274 return false;
277 IMPL_NARROW_FROM_DECL (AST_Template_Module)
278 IMPL_NARROW_FROM_SCOPE (AST_Template_Module)