Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / TAO_IDL / ast / ast_enum.cpp
blobce4037d9cac4a5d06c6ecac94b34358a9cefe0d1
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 #include "ast_enum.h"
66 #include "ast_enum_val.h"
67 #include "ast_generator.h"
68 #include "ast_visitor.h"
69 #include "utl_identifier.h"
70 #include "utl_err.h"
71 #include "utl_indenter.h"
73 AST_Decl::NodeType const
74 AST_Enum::NT = AST_Decl::NT_enum;
76 AST_Enum::AST_Enum (UTL_ScopedName *n,
77 bool local,
78 bool abstract)
79 : COMMON_Base (local,
80 abstract),
81 AST_Decl (AST_Decl::NT_enum,
82 n),
83 AST_Type (AST_Decl::NT_enum,
84 n),
85 AST_ConcreteType (AST_Decl::NT_enum,
86 n),
87 UTL_Scope (AST_Decl::NT_enum),
88 pd_enum_counter (0),
89 member_count_ (-1)
91 this->size_type (AST_Type::FIXED);
94 AST_Enum::~AST_Enum (void)
98 // Return the member count.
99 int
100 AST_Enum::member_count (void)
102 if (this->member_count_ == -1)
104 this->compute_member_count ();
107 return this->member_count_;
110 // Convert a numeric value to the string name
111 UTL_ScopedName *
112 AST_Enum::value_to_name (const unsigned long v)
114 AST_EnumVal *item = 0;
115 AST_Decl *d = 0;
117 for (UTL_ScopeActiveIterator i (this, IK_decls); !i.is_done (); i.next ())
119 d = i.item ();
120 item = dynamic_cast<AST_EnumVal*> (d);
122 if (item->constant_value ()->ev ()->u.ulval == v)
124 return item->name ();
128 return 0;
131 // Look up an enumerator by the value of the supplied expression.
132 AST_EnumVal *
133 AST_Enum::lookup_by_value (const AST_Expression *v)
135 AST_EnumVal *item = 0;
136 AST_Decl *d = 0;
138 for (UTL_ScopeActiveIterator i (this, IK_decls);
139 !i.is_done ();
140 i.next ())
142 d = i.item ();
143 item = dynamic_cast<AST_EnumVal*> (d);
144 AST_Expression *cv = item->constant_value ();
146 if (cv == v)
148 return item;
151 // Enum union label expressions don't get evaluated upon
152 // creation, to evaluate them later, we have only the
153 // string name to look up the enum value with.
154 UTL_ScopedName *v_n = const_cast<AST_Expression *> (v)->n ();
156 if (v_n != 0)
158 Identifier *cv_i = item->local_name ();
159 Identifier *v_i = v_n->last_component ();
161 if (cv_i->compare (v_i))
163 return item;
168 return 0;
171 // Compute the value to be assigned to the next enumerator. Bump the
172 // counter.
173 unsigned long
174 AST_Enum::next_enum_val (void)
176 unsigned long i = pd_enum_counter++;
178 return i;
181 // Static helper functions
183 // Modify scoped name of an enumval so that it is scoped inside the scope
184 // in which the enum is defined and not inside the enum itself
185 static UTL_ScopedName *
186 munge_name_for_enumval (UTL_ScopedName *n,
187 Identifier *last_component)
189 long len = n->length ();
190 UTL_ScopedName *hold = n;
192 // Last three components are:
193 // - scope in which enum is defined
194 // - scope for enum
195 // - name of enumval
197 // We want to stop cdr'ing down the list when the head of the
198 // list is at the name for the scope in which the enum is defined.
199 while (len > 3)
201 len--;
202 n = (UTL_ScopedName *) n->tail ();
205 UTL_IdList *id = 0;
206 ACE_NEW_RETURN (id,
207 UTL_IdList (last_component->copy (),
211 n->set_tail (id);
213 return hold;
216 // Compute total number of members.
218 AST_Enum::compute_member_count (void)
220 this->member_count_ = 0;
222 // If there are elements in this scope
223 if (this->nmembers () > 0)
225 for (UTL_ScopeActiveIterator i (this, IK_decls);
226 !i.is_done ();
227 i.next ())
229 // Get the next AST decl node.
230 ++this->member_count_;
234 return 0;
237 AST_EnumVal *
238 AST_Enum::fe_add_enum_val (AST_EnumVal *t)
240 AST_Decl *d = 0;
241 AST_EnumVal *t1 = 0;
243 AST_Expression::AST_ExprValue *ev =
244 t->constant_value ()->coerce (AST_Expression::EV_ulong);
246 t1 = idl_global->gen ()->create_enum_val (ev->u.ulval,
247 t->name ());
249 delete ev;
250 ev = 0;
252 UTL_ScopedName *sn =
253 munge_name_for_enumval ((UTL_IdList *) t->name ()->copy (),
254 t->local_name ());
256 t->set_name (sn);
258 sn = munge_name_for_enumval ((UTL_IdList *) t1->name ()->copy (),
259 t1->local_name ());
261 t1->set_name (sn);
263 // Already defined and cannot be redefined? Or already used?
264 if ((d = this->lookup_for_add (t)) != 0)
266 if (!FE_Utils::can_be_redefined (d, t))
268 idl_global->err ()->error3 (UTL_Error::EIDL_REDEF,
270 this,
272 return 0;
275 if (this->referenced (d, t->local_name ()))
277 idl_global->err ()->error3 (UTL_Error::EIDL_DEF_USE,
279 this,
281 return 0;
284 if (t->has_ancestor (d))
286 idl_global->err ()->redefinition_in_scope (t,
288 return 0;
292 // Add it to scope.
293 this->add_to_scope (t);
295 // Add it to set of locally referenced symbols.
296 this->add_to_referenced (t,
297 false,
298 t->local_name ());
300 if (t1 == 0)
302 // Prevent dereferencing null pointer in nested calls.
303 return 0;
306 // Add it to enclosing scope.
307 idl_global->scopes ().next_to_top ()->fe_add_enum_val (t1);
309 return t;
312 // Redefinition of inherited virtual operations.
314 // Dump this AST_Enum to the ostream o
315 void
316 AST_Enum::dump (ACE_OSTREAM_TYPE &o)
318 AST_Decl *d = 0;
320 if (this->is_local ())
322 this->dump_i (o, "(local) ");
324 else if (this->is_abstract ())
326 this->dump_i (o, "(abstract) ");
329 this->dump_i (o, "enum ");
331 this->local_name ()->dump (o);
333 this->dump_i (o, " {\n");
334 idl_global->indent ()->increase ();
335 idl_global->indent ()->skip_to (o);
337 // Must increment the iterator explicitly inside the loop.
338 for (UTL_ScopeActiveIterator i (this, IK_decls);!i.is_done ();)
340 d = i.item ();
341 d->dump_annotations (o, true /* print inline */);
342 d->local_name ()->dump (o);
343 i.next ();
345 if (i.is_done ())
347 this->dump_i (o, "\n");
349 else
351 this->dump_i (o, ",\n");
352 idl_global->indent ()->skip_to (o);
356 idl_global->indent ()->decrease ();
357 idl_global->indent ()->skip_to (o);
359 this->dump_i (o, "}");
363 AST_Enum::ast_accept (ast_visitor *visitor)
365 return visitor->visit_enum (this);
368 void
369 AST_Enum::destroy (void)
371 this->UTL_Scope::destroy ();
372 this->AST_ConcreteType::destroy ();
378 IMPL_NARROW_FROM_DECL(AST_Enum)
379 IMPL_NARROW_FROM_SCOPE(AST_Enum)