Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / TAO_IDL / ast / ast_module.cpp
blob8d5348fe105db0889bd8fbfcb51d6986dd67fe70
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_Modules denote IDL module declarations
66 // AST_Modules are subclasses of AST_Decl (they are not a type!) and
67 // of UTL_Scope.
69 #include "ast_module.h"
70 #include "ast_template_module_inst.h"
71 #include "ast_predefined_type.h"
72 #include "ast_valuebox.h"
73 #include "ast_valuetype.h"
74 #include "ast_valuetype_fwd.h"
75 #include "ast_eventtype.h"
76 #include "ast_eventtype_fwd.h"
77 #include "ast_component.h"
78 #include "ast_component_fwd.h"
79 #include "ast_porttype.h"
80 #include "ast_connector.h"
81 #include "ast_home.h"
82 #include "ast_constant.h"
83 #include "ast_exception.h"
84 #include "ast_union.h"
85 #include "ast_union_fwd.h"
86 #include "ast_structure_fwd.h"
87 #include "ast_enum.h"
88 #include "ast_enum_val.h"
89 #include "ast_typedef.h"
90 #include "ast_native.h"
91 #include "ast_generator.h"
92 #include "ast_visitor.h"
93 #include "ast_extern.h"
95 #include "utl_err.h"
96 #include "utl_identifier.h"
97 #include "utl_indenter.h"
99 #include "global_extern.h"
100 #include "nr_extern.h"
102 AST_Decl::NodeType const
103 AST_Module::NT = AST_Decl::NT_module;
105 AST_Module::AST_Module (UTL_ScopedName *n, AST_Module *previous)
106 : AST_Decl (AST_Decl::NT_module, n),
107 UTL_Scope (AST_Decl::NT_module),
108 pd_has_nested_valuetype_ (0),
109 previous_opening_ (previous),
110 last_in_same_parent_scope_ (this),
111 from_inst_ (0),
112 from_ref_ (0)
114 // NOTE previous passed into this constructor should be
115 // the FIRST module that is a previous opening of this same
116 // module in the scope in which it is stored. Later on if
117 // the module is validated and added to it's scope, the
118 // previous_opening_ will be adjusted to be the immediatly
119 // previous opening of this module. See fe_add_module ()
120 // below.
123 AST_Module::~AST_Module (void)
127 void
128 AST_Module::destroy (void)
130 this->UTL_Scope::destroy ();
131 this->AST_Decl::destroy ();
134 IMPL_NARROW_FROM_DECL(AST_Module)
135 IMPL_NARROW_FROM_SCOPE(AST_Module)
137 // Dump this AST_Module node to the ostream o.
138 void
139 AST_Module::dump (ACE_OSTREAM_TYPE &o)
141 this->dump_i (o, "module ");
142 this->local_name ()->dump (o);
143 this->dump_i (o, " {\n");
144 UTL_Scope::dump (o);
145 idl_global->indent ()->skip_to (o);
146 this->dump_i (o, "}");
149 // We actually want to match the LAST module found in
150 // the scope being searched not the FIRST one in the
151 // list.
152 AST_Decl *
153 AST_Module::adjust_found (
154 bool /*ignore_fwd*/,
155 bool /*full_def_only*/)
157 return this->last_in_same_parent_scope_;
160 // Involved in OBV_ namespace generation.
161 void
162 AST_Module::set_has_nested_valuetype (void)
164 UTL_Scope *parent = this->defined_in ();
165 if (parent && !this->pd_has_nested_valuetype_)
167 AST_Module *pm = dynamic_cast<AST_Module*> (parent);
168 if (pm)
170 pm->set_has_nested_valuetype ();
174 this->pd_has_nested_valuetype_ = 1;
177 bool
178 AST_Module::has_nested_valuetype (void)
180 return this->pd_has_nested_valuetype_;
184 AST_Module::be_add_interface (AST_Interface *i,
185 AST_Interface *ix)
187 // Add it to scope.
188 this->add_to_scope (i, ix);
190 // Add it to set of locally referenced symbols.
191 this->add_to_referenced (i,
192 false,
193 i->local_name (),
194 ix);
195 return 0;
199 AST_Module::be_add_valuetype (AST_ValueType *v)
201 // Add it to scope.
202 this->add_to_scope (v);
204 // Add it to set of locally referenced symbols.
205 this->add_to_referenced (v,
206 false,
207 v->local_name ());
208 return 0;
211 // Has this node been referenced here before?
212 bool
213 AST_Module::referenced (AST_Decl *e,
214 Identifier *id)
216 bool refd = this->UTL_Scope::referenced (e, id);
217 if (refd)
219 return true;
222 AST_Decl *d =
223 this->look_in_prev_mods_local (e->local_name (), true);
224 if (!d)
226 return false;
229 AST_Type *t = dynamic_cast<AST_Type*> (d);
230 return (!t || t->is_defined ());
233 AST_Module *
234 AST_Module::previous_opening ()
236 return this->previous_opening_;
239 AST_Decl *
240 AST_Module::look_in_prev_mods_local (Identifier *e,
241 bool ignore_fwd)
243 for (AST_Module *po = this->previous_opening_;
245 po = po->previous_opening_)
247 for (UTL_ScopeActiveIterator iter (po, UTL_Scope::IK_decls);
248 !iter.is_done ();
249 iter.next ())
251 AST_Decl *d = iter.item ();
252 if ((!ignore_fwd || !d->is_fwd ())
253 && !!(d = d->adjust_found (ignore_fwd, false))
254 && e->case_compare (d->local_name ()))
256 return d;
261 return 0;
264 AST_Decl *
265 AST_Module::look_in_prev_mods (UTL_ScopedName *e,
266 bool full_def_only,
267 AST_Decl *&final_parent_decl)
269 for (AST_Module *po = this->previous_opening_;
271 po = po->previous_opening_)
273 for (UTL_ScopeActiveIterator iter (po, UTL_Scope::IK_decls);
274 !iter.is_done ();
275 iter.next ())
277 AST_Decl *const d =
278 iter.item ()->adjust_found (full_def_only, full_def_only);
280 if (d && e->head ()->case_compare (d->local_name ()))
282 UTL_ScopedName *sn =
283 static_cast<UTL_ScopedName *> (e->tail ());
284 if (!sn)
286 final_parent_decl= static_cast<AST_Decl *> (po);
287 return d; // Nothing left in path, found wanted name
290 UTL_Scope *s = DeclAsScope (d);
291 if (s)
293 // Recurse down what is left of the path
294 return s->lookup_by_name_r (sn, full_def_only, final_parent_decl);
297 // Find next match, this one didn't work out.
302 return 0; // Not found.
306 AST_Module::ast_accept (ast_visitor *visitor)
308 return visitor->visit_module (this);
311 AST_Template_Module_Inst *
312 AST_Module::from_inst (void) const
314 return this->from_inst_;
317 void
318 AST_Module::from_inst (AST_Template_Module_Inst *node)
320 this->from_inst_ = node;
323 AST_Template_Module_Ref *
324 AST_Module::from_ref (void) const
326 return this->from_ref_;
329 void
330 AST_Module::from_ref (AST_Template_Module_Ref *node)
332 this->from_ref_ = node;
335 AST_Decl *
336 AST_Module::special_lookup (UTL_ScopedName *e,
337 bool full_def_only,
338 AST_Decl *&final_parent_decl)
340 return this->look_in_prev_mods (e, full_def_only, final_parent_decl);
343 //================================================
345 AST_PredefinedType *
346 AST_Module::fe_add_predefined_type (AST_PredefinedType *t)
348 return dynamic_cast<AST_PredefinedType*> (this->fe_add_decl (t));
351 AST_Module *
352 AST_Module::fe_add_module (AST_Module *t)
354 // If our prefix is empty, we check to see if an ancestor has one.
355 if (!ACE_OS::strcmp (t->prefix (), ""))
357 UTL_Scope *scope = t->defined_in ();
358 while (scope)
360 AST_Decl *parent = ScopeAsDecl (scope);
361 const char *prefix_holder = parent->prefix ();
363 if (!prefix_holder)
365 break; // We have reached global scope.
368 t->prefix (const_cast<char *> (prefix_holder));
370 if (ACE_OS::strcmp (t->prefix (), ""))
372 break;
375 scope = parent->defined_in ();
379 // Already defined and cannot be redefined? Or already used?
380 AST_Module *m = 0;
381 AST_Decl *d = this->lookup_for_add (t);
383 if (d)
385 if (!FE_Utils::can_be_redefined (d, t))
387 idl_global->err ()->error3 (UTL_Error::EIDL_REDEF,
389 this,
391 return 0;
394 // has_ancestor() returns TRUE if both nodes are the same.
395 m = dynamic_cast<AST_Module*> (d);
396 if (t != m && t->has_ancestor (d))
398 idl_global->err ()->redefinition_in_scope (t, d);
399 return 0;
402 const char *prev_prefix = d->prefix ();
403 const char *this_prefix = t->prefix ();
405 if (!ACE_OS::strcmp (this_prefix, ""))
407 t->prefix (const_cast<char *> (prev_prefix));
409 else if (!ACE_OS::strcmp (prev_prefix, ""))
411 d->prefix (const_cast<char *> (this_prefix));
415 // Now we are actually adding this new module to the active
416 // scopes, ensure that the previous openings are all updated
417 // so that this new module is recorded as the current
418 // "last module" definition in our scope.
419 AST_Module *first_opening = t->previous_opening_;
420 if (first_opening)
422 // When we created the module we simply stored the first
423 // previous module we found (if any), we actually want
424 // to refer to that module's LAST opening in that same scope
425 // except this module itself (which isn't yet recorded anyway).
426 t->previous_opening_ = first_opening->last_in_same_parent_scope_;
427 if (first_opening->defined_in () == this)
429 // Since the previous openings are in our own scope,
430 // make sure that these all point to this new module.
431 for (AST_Module *pm = t->previous_opening_;
433 pm = pm->previous_opening_)
435 pm->last_in_same_parent_scope_ = t;
436 if (pm == first_opening)
438 // Even if there are still more previous modules,
439 // the ones before this one are in a different
440 // scope and do not need to be updated.
441 break;
447 // If this node is not a reopened module, add it to scope and referenced.
448 if (!m || t != m)
450 // Add it to scope.
451 this->add_to_scope (t);
453 // Add it to set of locally referenced symbols.
454 this->add_to_referenced (t,
455 false,
456 t->local_name ());
459 return t;
462 AST_Template_Module_Inst *
463 AST_Module::fe_add_template_module_inst (AST_Template_Module_Inst *t)
465 return dynamic_cast<AST_Template_Module_Inst*> (this->fe_add_ref_decl (t));
468 AST_Interface *
469 AST_Module::fe_add_interface (AST_Interface *t)
471 return this->fe_add_full_intf_decl<AST_Interface> (t);
474 AST_ValueBox *
475 AST_Module::fe_add_valuebox (AST_ValueBox *t)
477 return dynamic_cast<AST_ValueBox*> (this->fe_add_decl (t));
480 AST_ValueType *
481 AST_Module::fe_add_valuetype (AST_ValueType *t)
483 return this->fe_add_full_intf_decl<AST_ValueType> (t);
486 AST_EventType *
487 AST_Module::fe_add_eventtype (AST_EventType *t)
489 return this->fe_add_full_intf_decl<AST_EventType> (t);
492 AST_Component *
493 AST_Module::fe_add_component (AST_Component *t)
495 return this->fe_add_full_intf_decl<AST_Component> (t);
498 AST_Connector *
499 AST_Module::fe_add_connector (AST_Connector *t)
501 return dynamic_cast<AST_Connector*> (this->fe_add_decl (t));
504 AST_Home *
505 AST_Module::fe_add_home (AST_Home *t)
507 return dynamic_cast<AST_Home*> (this->fe_add_decl (t));
510 AST_InterfaceFwd *
511 AST_Module::fe_add_interface_fwd (AST_InterfaceFwd *t)
513 return this->fe_add_fwd_intf_decl<AST_Interface> (t);
516 AST_ValueTypeFwd *
517 AST_Module::fe_add_valuetype_fwd (AST_ValueTypeFwd *t)
519 return this->fe_add_fwd_intf_decl<AST_ValueType> (t);
522 AST_EventTypeFwd *
523 AST_Module::fe_add_eventtype_fwd (AST_EventTypeFwd *t)
525 return this->fe_add_fwd_intf_decl<AST_EventType> (t);
528 AST_ComponentFwd *
529 AST_Module::fe_add_component_fwd (AST_ComponentFwd *t)
531 return this->fe_add_fwd_intf_decl<AST_Component> (t);
534 AST_Constant *
535 AST_Module::fe_add_constant (AST_Constant *t)
537 return dynamic_cast<AST_Constant*> (this->fe_add_decl (t));
540 AST_Exception *
541 AST_Module::fe_add_exception (AST_Exception *t)
543 return dynamic_cast<AST_Exception*> (this->fe_add_decl (t));
546 AST_Union *
547 AST_Module::fe_add_union (AST_Union *t)
549 return dynamic_cast<AST_Union*> (this->fe_add_full_struct_type (t));
552 AST_UnionFwd *
553 AST_Module::fe_add_union_fwd (AST_UnionFwd *t)
555 return dynamic_cast<AST_UnionFwd*> (this->fe_add_fwd_struct_type (t));
558 AST_Structure *
559 AST_Module::fe_add_structure (AST_Structure *t)
561 return this->fe_add_full_struct_type (t);
564 AST_StructureFwd *
565 AST_Module::fe_add_structure_fwd (AST_StructureFwd *t)
567 return this->fe_add_fwd_struct_type (t);
570 AST_Enum *
571 AST_Module::fe_add_enum (AST_Enum *t)
573 return dynamic_cast<AST_Enum*> (this->fe_add_decl (t));
576 // Add an AST_EnumVal node (an enumerator) to this scope.
577 // This is done to conform to the C++ scoping rules which declare
578 // enumerators in the enclosing scope (in addition to declaring them
579 // in the enum itself).
580 AST_EnumVal *
581 AST_Module::fe_add_enum_val (AST_EnumVal *t)
583 return dynamic_cast<AST_EnumVal*> (this->fe_add_decl (t));
586 AST_Typedef *
587 AST_Module::fe_add_typedef (AST_Typedef *t)
589 return dynamic_cast<AST_Typedef*> (this->fe_add_ref_decl (t));
592 AST_Native *
593 AST_Module::fe_add_native (AST_Native *t)
595 return dynamic_cast<AST_Native*> (this->fe_add_decl (t));
598 AST_PortType *
599 AST_Module::fe_add_porttype (AST_PortType *t)
601 return dynamic_cast<AST_PortType*> (this->fe_add_decl (t));
604 void
605 AST_Module::reset_last_in_same_parent_scope (void)
607 this->last_in_same_parent_scope_ = this;
610 AST_Annotation_Decl *
611 AST_Module::fe_add_annotation_decl (
612 AST_Annotation_Decl *annotation_decl)
614 return dynamic_cast<AST_Annotation_Decl*> (fe_add_decl (annotation_decl));