Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / TAO_IDL / ast / ast_template_module.cpp
bloba0a66923010c1496c0acb64658eee0a3d8c1a727
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 ()
36 FE_Utils::T_PARAMLIST_INFO *
37 AST_Template_Module::template_params () 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 = nullptr;
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 = nullptr;
70 (void) this->template_params_->get (param, slot);
72 if (!this->match_one_param (param, d))
74 UTL_ScopedName *n = d->name ();
75 const char *s = nullptr;
77 if (!n)
79 AST_Constant *c = dynamic_cast<AST_Constant *> (d);
80 s = AST_Expression::exprtype_to_string (c->et ());
82 else
84 s = d->full_name ();
87 idl_global->err ()->mismatched_template_param (s);
89 return false;
93 return true;
96 bool
97 AST_Template_Module::match_param_refs (UTL_StrList *refs,
98 UTL_Scope *decl_scope)
100 UTL_Scope *s = decl_scope;
101 AST_Template_Module *enclosing = nullptr;
103 while (enclosing == nullptr && s != nullptr)
105 enclosing = dynamic_cast<AST_Template_Module*> (s);
106 s = ScopeAsDecl (s)->defined_in ();
109 if (enclosing == nullptr)
111 ACE_ERROR_RETURN ((LM_ERROR,
112 ACE_TEXT ("AST_Template_Module::")
113 ACE_TEXT ("match_param_refs - ")
114 ACE_TEXT ("enclosing template ")
115 ACE_TEXT ("module scope not found\n")),
116 false);
119 for (UTL_StrlistActiveIterator i (refs);
120 !i.is_done ();
121 i.next ())
123 FE_Utils::T_Param_Info *enclosing_param =
124 enclosing->find_param (i.item ());
126 if (enclosing_param == nullptr)
128 // Enclosing param not found
129 return false;
132 if (!this->match_param_by_type (enclosing_param))
134 // Referenced param type not matched to enclosiong param.
135 return false;
139 return true;
142 void
143 AST_Template_Module::destroy ()
145 delete this->template_params_;
146 this->template_params_ = nullptr;
148 this->AST_Module::destroy ();
152 AST_Template_Module::ast_accept (ast_visitor *visitor)
154 return visitor->visit_template_module (this);
157 AST_Template_Module_Ref *
158 AST_Template_Module::fe_add_template_module_ref (
159 AST_Template_Module_Ref *m)
161 return dynamic_cast<AST_Template_Module_Ref*> (this->fe_add_ref_decl (m));
164 void
165 AST_Template_Module::dump (ACE_OSTREAM_TYPE & /* o */)
169 bool
170 AST_Template_Module::match_one_param (FE_Utils::T_Param_Info *param,
171 AST_Decl *d)
173 if (param->type_ == AST_Decl::NT_type)
175 return true;
178 if (d->node_type () == AST_Decl::NT_typedef)
180 AST_Typedef *td = dynamic_cast<AST_Typedef*> (d);
181 d = td->primitive_base_type ();
184 AST_Decl::NodeType other_type = d->node_type ();
186 if (other_type == AST_Decl::NT_const)
188 AST_Constant *c = dynamic_cast<AST_Constant*> (d);
190 AST_Expression *ex = c->constant_value ();
192 AST_Expression::AST_ExprValue *ev =
193 ex->check_and_coerce (param->const_type_,
194 param->enum_const_type_decl_);
196 if (ev == nullptr)
198 idl_global->err ()->coercion_error (ex,
199 param->const_type_);
202 bool retval = (ev != nullptr);
203 delete ev;
204 ev = nullptr;
205 return retval;
208 return (param->type_ == other_type);
211 FE_Utils::T_Param_Info *
212 AST_Template_Module::find_param (UTL_String *name)
214 for (FE_Utils::T_PARAMLIST_INFO::CONST_ITERATOR i (
215 *this->template_params_);
216 !i.done ();
217 i.advance ())
219 FE_Utils::T_Param_Info *param = nullptr;
220 i.next (param);
222 if (param->name_ == name->get_string ())
224 return param;
228 return nullptr;
231 bool
232 AST_Template_Module::match_param_by_type (
233 FE_Utils::T_Param_Info *param)
235 for (FE_Utils::T_PARAMLIST_INFO::CONST_ITERATOR i (
236 *this->template_params_);
237 !i.done ();
238 i.advance ())
240 FE_Utils::T_Param_Info *my_param = nullptr;
241 i.next (my_param);
243 if (param->type_ == my_param->type_)
245 if (param->type_ == AST_Decl::NT_const)
247 if (param->const_type_ == my_param->const_type_)
249 if (param->const_type_ == AST_Expression::EV_enum)
251 if (param->enum_const_type_decl_
252 == my_param->enum_const_type_decl_)
254 return true;
257 else
259 return true;
263 else
265 return true;
270 idl_global->err ()->mismatched_template_param (
271 param->name_.c_str ());
273 return false;