Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / TAO_IDL / ast / ast_map.cpp
blob3b1255a518538cb1e18fee2ce71b181c8e8adadf
1 /*
3 COPYRIGHT
5 Copyright 1992, 1993, 1994 Sun Microsystems, Inc. Printed in the United
6 States of America. All Rights Reserved.
8 This product is protected by copyright and distributed under the following
9 license restricting its use.
11 The Interface Definition Language Compiler Front End (CFE) is made
12 available for your use provided that you include this license and copyright
13 notice on all media and documentation and the software program in which
14 this product is incorporated in whole or part. You may copy and extend
15 functionality (but may not remove functionality) of the Interface
16 Definition Language CFE without charge, but you are not authorized to
17 license or distribute it to anyone else except as part of a product or
18 program developed by you or with the express written consent of Sun
19 Microsystems, Inc. ("Sun").
21 The names of Sun Microsystems, Inc. and any of its subsidiaries or
22 affiliates may not be used in advertising or publicity pertaining to
23 distribution of Interface Definition Language CFE as permitted herein.
25 This license is effective until terminated by Sun for failure to comply
26 with this license. Upon termination, you shall destroy or return all code
27 and documentation for the Interface Definition Language CFE.
29 INTERFACE DEFINITION LANGUAGE CFE IS PROVIDED AS IS WITH NO WARRANTIES OF
30 ANY KIND INCLUDING THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS
31 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR ARISING FROM A COURSE OF
32 DEALING, USAGE OR TRADE PRACTICE.
34 INTERFACE DEFINITION LANGUAGE CFE IS PROVIDED WITH NO SUPPORT AND WITHOUT
35 ANY OBLIGATION ON THE PART OF Sun OR ANY OF ITS SUBSIDIARIES OR AFFILIATES
36 TO ASSIST IN ITS USE, CORRECTION, MODIFICATION OR ENHANCEMENT.
38 SUN OR ANY OF ITS SUBSIDIARIES OR AFFILIATES SHALL HAVE NO LIABILITY WITH
39 RESPECT TO THE INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY
40 INTERFACE DEFINITION LANGUAGE CFE OR ANY PART THEREOF.
42 IN NO EVENT WILL SUN OR ANY OF ITS SUBSIDIARIES OR AFFILIATES BE LIABLE FOR
43 ANY LOST REVENUE OR PROFITS OR OTHER SPECIAL, INDIRECT AND CONSEQUENTIAL
44 DAMAGES, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
46 Use, duplication, or disclosure by the government is subject to
47 restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
48 Technical Data and Computer Software clause at DFARS 252.227-7013 and FAR
49 52.227-19.
51 Sun, Sun Microsystems and the Sun logo are trademarks or registered
52 trademarks of Sun Microsystems, Inc.
54 SunSoft, Inc.
55 2550 Garcia Avenue
56 Mountain View, California 94043
58 NOTE:
60 SunOS, SunSoft, Sun, Solaris, Sun Microsystems or the Sun logo are
61 trademarks or registered trademarks of Sun Microsystems, Inc.
65 // AST_Map nodes represent IDL map declarations.
66 // AST_Map is a subclass of AST_ConcreteType.
67 // AST_Map nodes have a maximum size (an AST_Expression which
68 // must evaluate to a positive integer) and a base type (a subclass
69 // of AST_Type).
71 #include "ast_map.h"
72 #include "ast_typedef.h"
73 #include "ast_expression.h"
74 #include "ast_param_holder.h"
75 #include "ast_visitor.h"
76 #include "ast_annotation_appl.h"
78 #include "utl_identifier.h"
79 #include "utl_err.h"
81 #include "global_extern.h"
82 #include "fe_extern.h"
84 #include "ace/Log_Msg.h"
85 #include "ace/OS_Memory.h"
86 #include "ace/OS_NS_string.h"
88 #include <cstring>
90 AST_Decl::NodeType const
91 AST_Map::NT = AST_Decl::NT_map;
93 AST_Map::AST_Map (AST_Expression *ms,
94 AST_Type *key_bt,
95 AST_Type *val_bt,
96 UTL_ScopedName *n,
97 bool local,
98 bool abstract)
99 : COMMON_Base (key_bt->is_local () || val_bt->is_local() || local,
100 abstract),
101 AST_Decl (AST_Decl::NT_map,
103 true),
104 AST_Type (AST_Decl::NT_map,
106 AST_ConcreteType (AST_Decl::NT_map,
108 pd_max_size (ms),
109 key_pd_type (key_bt),
110 value_pd_type (val_bt),
111 unbounded_ (true),
112 owns_key_type_ (false),
113 owns_value_type_ (false)
115 FE_Utils::tmpl_mod_ref_check (this, key_bt);
117 AST_Decl::NodeType knt = key_bt->node_type ();
119 if (knt == AST_Decl::NT_param_holder)
121 AST_Param_Holder *ph = dynamic_cast<AST_Param_Holder *> (key_bt);
123 if (ph->info ()->type_ == AST_Decl::NT_const)
125 idl_global->err ()->not_a_type (key_bt);
126 key_bt->destroy ();
127 delete key_bt;
128 key_bt = nullptr;
129 throw Bailout ();
133 FE_Utils::tmpl_mod_ref_check (this, val_bt);
135 AST_Decl::NodeType vnt = val_bt->node_type ();
137 if (vnt == AST_Decl::NT_param_holder)
139 AST_Param_Holder *ph = dynamic_cast<AST_Param_Holder*> (val_bt);
141 if (ph->info ()->type_ == AST_Decl::NT_const)
143 idl_global->err ()->not_a_type (val_bt);
144 val_bt->destroy ();
145 delete val_bt;
146 val_bt = nullptr;
147 throw Bailout ();
151 // Check if we are bounded or unbounded. An expression value of 0 means
152 // unbounded. If our bound is a template parameter, skip the
153 // check altogether, this node will trigger no code generation.
154 if (ms->param_holder () == nullptr)
156 this->unbounded_ = (ms->ev ()->u.ulval == 0);
159 // A map data type is always VARIABLE.
160 this->size_type (AST_Type::VARIABLE);
162 this->owns_key_type_ =
163 knt == AST_Decl::NT_array
164 || knt == AST_Decl::NT_map
165 || knt == AST_Decl::NT_param_holder;
167 this->owns_value_type_ =
168 vnt == AST_Decl::NT_array
169 || vnt == AST_Decl::NT_map
170 || vnt == AST_Decl::NT_param_holder;
173 // Public operations.
175 bool
176 AST_Map::in_recursion (ACE_Unbounded_Queue<AST_Type *> &list)
178 if (list.is_empty ()) // only structs, unions and valuetypes can be recursive
179 return false;
181 list.enqueue_tail(this);
183 AST_Type *key_type = dynamic_cast<AST_Type*> (this->key_type ());
184 AST_Type *val_type = dynamic_cast<AST_Type*> (this->value_type());
186 if (key_type == nullptr)
188 ACE_ERROR_RETURN ((LM_ERROR,
189 ACE_TEXT ("AST_Map::in_recursion - ")
190 ACE_TEXT ("bad key type\n")),
191 false);
194 if (val_type == nullptr)
196 ACE_ERROR_RETURN ((LM_ERROR,
197 ACE_TEXT ("AST_Map::in_recursion - ")
198 ACE_TEXT ("bad value type\n")),
199 false);
202 AST_Decl::NodeType kt = key_type->node_type ();
203 AST_Decl::NodeType vt = val_type->node_type ();
205 if (kt == AST_Decl::NT_typedef)
207 AST_Typedef *td = dynamic_cast<AST_Typedef*> (key_type);
208 key_type = td->primitive_base_type ();
209 kt = key_type->node_type ();
212 if (vt == AST_Decl::NT_typedef)
214 AST_Typedef *td = dynamic_cast<AST_Typedef*>(val_type);
215 val_type = td->primitive_base_type();
216 vt = val_type->node_type();
219 if (kt != AST_Decl::NT_struct
220 && kt != AST_Decl::NT_union
221 && kt != AST_Decl::NT_valuetype
222 && kt != AST_Decl::NT_map
223 && vt != AST_Decl::NT_struct
224 && vt != AST_Decl::NT_union
225 && vt != AST_Decl::NT_valuetype
226 && vt != AST_Decl::NT_map)
228 return false;
231 bool recursion_found = false;
232 AST_Type** recursable_type = nullptr;
233 list.get (recursable_type, 0);
234 if (!std::strcmp (key_type->full_name (),
235 (*recursable_type)->full_name ())
236 || !std::strcmp(val_type->full_name (),
237 (*recursable_type)->full_name ()))
239 // They match.
240 recursion_found = true;
241 idl_global->recursive_type_seen_ = true;
243 else
245 // Check the element type.
246 recursion_found = key_type->in_recursion (list) && val_type->in_recursion (list);
249 return recursion_found;
252 // Redefinition of inherited virtual operations.
254 // Dump this AST_Map node to the ostream o.
255 void
256 AST_Map::dump (ACE_OSTREAM_TYPE &o)
258 this->dump_i (o, "map <");
259 AST_Annotation_Appls::iterator i,
260 finished = key_type_annotations ().end ();
261 for (i = key_type_annotations ().begin (); i != finished; ++i)
263 AST_Annotation_Appl *a = i->get ();
264 a->dump (o);
265 dump_i (o, " ");
267 this->key_pd_type->dump (o);
268 this->dump_i (o, ", ");
270 finished = value_type_annotations ().end ();
271 for (i = value_type_annotations ().begin (); i != finished; ++i)
273 AST_Annotation_Appl *a = i->get ();
274 a->dump (o);
275 dump_i (o, " ");
277 this->value_pd_type->dump (o);
278 this->dump_i (o, ", ");
279 this->pd_max_size->dump (o);
280 this->dump_i (o, ">");
284 AST_Map::ast_accept (ast_visitor *visitor)
286 return visitor->visit_map (this);
289 // Data accessors.
291 AST_Expression *
292 AST_Map::max_size () const
294 return this->pd_max_size;
297 AST_Type *
298 AST_Map::key_type () const
300 return this->key_pd_type;
303 AST_Type *
304 AST_Map::value_type () const
306 return this->value_pd_type;
309 AST_Type *
310 AST_Map::primitive_key_type () const
312 AST_Type *type_node = key_type ();
313 if (type_node && type_node->node_type () == AST_Decl::NT_typedef)
315 AST_Typedef *const typedef_node = dynamic_cast<AST_Typedef *> (type_node);
316 if (!typedef_node) return nullptr;
317 type_node = typedef_node->primitive_base_type ();
319 return type_node;
322 AST_Type *
323 AST_Map::primitive_value_type () const
325 AST_Type *type_node = value_type();
326 if (type_node && type_node->node_type() == AST_Decl::NT_typedef)
328 AST_Typedef * const typedef_node = dynamic_cast<AST_Typedef*>(type_node);
329 if (!typedef_node) return nullptr;
330 type_node = typedef_node->primitive_base_type();
332 return type_node;
335 bool
336 AST_Map::unbounded () const
338 return this->unbounded_;
341 bool
342 AST_Map::legal_for_primary_key () const
344 return this->key_type ()->legal_for_primary_key ();
347 bool
348 AST_Map::is_defined ()
350 return this->key_pd_type->is_defined () && this->value_pd_type->is_defined();
353 void
354 AST_Map::destroy ()
356 if (this->owns_key_type_)
358 this->key_pd_type->destroy ();
359 delete this->key_pd_type;
360 this->key_pd_type = nullptr;
363 if (this->owns_value_type_)
365 this->value_pd_type->destroy();
366 delete this->value_pd_type;
367 this->value_pd_type = nullptr;
370 this->pd_max_size->destroy ();
371 delete this->pd_max_size;
372 this->pd_max_size = nullptr;
374 this->AST_ConcreteType::destroy ();
377 AST_Annotation_Appls &
378 AST_Map::key_type_annotations ()
380 return value_type_annotations_;
383 void
384 AST_Map::key_type_annotations (const AST_Annotation_Appls &annotations)
386 key_type_annotations_ = annotations;
389 AST_Annotation_Appls &
390 AST_Map::value_type_annotations ()
392 return value_type_annotations_;
395 void
396 AST_Map::value_type_annotations (const AST_Annotation_Appls &annotations)
398 value_type_annotations_ = annotations;