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
51 Sun, Sun Microsystems and the Sun logo are trademarks or registered
52 trademarks of Sun Microsystems, Inc.
56 Mountain View, California 94043
60 SunOS, SunSoft, Sun, Solaris, Sun Microsystems or the Sun logo are
61 trademarks or registered trademarks of Sun Microsystems, Inc.
66 #include "ast_enum_val.h"
67 #include "ast_generator.h"
68 #include "ast_visitor.h"
69 #include "utl_identifier.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
,
81 AST_Decl (AST_Decl::NT_enum
,
83 AST_Type (AST_Decl::NT_enum
,
85 AST_ConcreteType (AST_Decl::NT_enum
,
87 UTL_Scope (AST_Decl::NT_enum
),
91 this->size_type (AST_Type::FIXED
);
94 AST_Enum::~AST_Enum (void)
98 // Return the member count.
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
112 AST_Enum::value_to_name (const unsigned long v
)
114 AST_EnumVal
*item
= 0;
117 for (UTL_ScopeActiveIterator
i (this, IK_decls
); !i
.is_done (); i
.next ())
120 item
= dynamic_cast<AST_EnumVal
*> (d
);
122 if (item
->constant_value ()->ev ()->u
.ulval
== v
)
124 return item
->name ();
131 // Look up an enumerator by the value of the supplied expression.
133 AST_Enum::lookup_by_value (const AST_Expression
*v
)
135 AST_EnumVal
*item
= 0;
138 for (UTL_ScopeActiveIterator
i (this, IK_decls
);
143 item
= dynamic_cast<AST_EnumVal
*> (d
);
144 AST_Expression
*cv
= item
->constant_value ();
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 ();
158 Identifier
*cv_i
= item
->local_name ();
159 Identifier
*v_i
= v_n
->last_component ();
161 if (cv_i
->compare (v_i
))
171 // Compute the value to be assigned to the next enumerator. Bump the
174 AST_Enum::next_enum_val (void)
176 unsigned long i
= pd_enum_counter
++;
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
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.
202 n
= (UTL_ScopedName
*) n
->tail ();
207 UTL_IdList (last_component
->copy (),
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
);
229 // Get the next AST decl node.
230 ++this->member_count_
;
238 AST_Enum::fe_add_enum_val (AST_EnumVal
*t
)
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
,
253 munge_name_for_enumval ((UTL_IdList
*) t
->name ()->copy (),
258 sn
= munge_name_for_enumval ((UTL_IdList
*) t1
->name ()->copy (),
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
,
275 if (this->referenced (d
, t
->local_name ()))
277 idl_global
->err ()->error3 (UTL_Error::EIDL_DEF_USE
,
284 if (t
->has_ancestor (d
))
286 idl_global
->err ()->redefinition_in_scope (t
,
293 this->add_to_scope (t
);
295 // Add it to set of locally referenced symbols.
296 this->add_to_referenced (t
,
302 // Prevent dereferencing null pointer in nested calls.
306 // Add it to enclosing scope.
307 idl_global
->scopes ().next_to_top ()->fe_add_enum_val (t1
);
312 // Redefinition of inherited virtual operations.
314 // Dump this AST_Enum to the ostream o
316 AST_Enum::dump (ACE_OSTREAM_TYPE
&o
)
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 ();)
341 d
->dump_annotations (o
, true /* print inline */);
342 d
->local_name ()->dump (o
);
347 this->dump_i (o
, "\n");
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);
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
)