Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / TAO_IDL / ast / ast_union_branch.cpp
blobb7da6dff6c3f175b13c7cf9530479d8871b757d8
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_UnionBranch nodes represent a single branch of an IDL union
66 // declaration.
67 // AST_UnionBranch is a subclass of AST_Field, adding a label (which
68 // is a subclass of AST_UnionLabel).
70 #include "ast_union_branch.h"
71 #include "ast_union_label.h"
72 #include "ast_union.h"
73 #include "ast_enum.h"
74 #include "ast_enum_val.h"
75 #include "ast_visitor.h"
76 #include "utl_indenter.h"
78 #include "utl_labellist.h"
79 #include "fe_extern.h"
81 AST_Decl::NodeType const
82 AST_UnionBranch::NT = AST_Decl::NT_union_branch;
84 AST_UnionBranch::AST_UnionBranch (UTL_LabelList *ll,
85 AST_Type *ft,
86 UTL_ScopedName *n)
87 : COMMON_Base (),
88 AST_Decl (AST_Decl::NT_union_branch,
89 n),
90 AST_Field (AST_Decl::NT_union_branch,
91 ft,
92 n),
93 pd_ll (ll)
97 AST_UnionBranch::~AST_UnionBranch (void)
101 // Redefinition of inherited virtual operations.
103 // Dump this AST_UnionBranch node to the ostream o.
104 void
105 AST_UnionBranch::dump (ACE_OSTREAM_TYPE &o)
107 unsigned long l = this->label_list_length ();
108 for (unsigned long i = 0; i < l; ++i)
110 this->dump_i (o, "case ");
112 AST_UnionLabel *ul = this->label (i);
113 ul->dump (o);
115 this->dump_i (o, ":\n");
116 if (i != l - 1) idl_global->indent ()->skip_to (o);
119 idl_global->indent ()->increase ();
120 idl_global->indent ()->skip_to (o);
121 AST_Field::dump_annotations (o, true /* print inline */);
122 AST_Field::dump (o);
123 idl_global->indent ()->decrease ();
127 AST_UnionBranch::ast_accept (ast_visitor *visitor)
129 return visitor->visit_union_branch (this);
132 void
133 AST_UnionBranch::destroy (void)
135 this->pd_ll->destroy ();
136 delete this->pd_ll;
137 this->pd_ll = 0;
139 this->AST_Field::destroy ();
142 UTL_LabelList *
143 AST_UnionBranch::labels (void) const
145 return this->pd_ll;
148 AST_UnionLabel *
149 AST_UnionBranch::label (unsigned long index)
151 unsigned long i = 0;
153 for (UTL_LabellistActiveIterator iter (this->pd_ll);
154 !iter.is_done ();
155 iter.next ())
157 if (i == index)
159 return iter.item ();
162 ++i;
165 return 0;
168 unsigned long
169 AST_UnionBranch::label_list_length (void)
171 if (this->pd_ll)
173 return this->pd_ll->length ();
175 else
177 return 0;
181 void
182 AST_UnionBranch::add_labels (AST_Union *u)
184 const bool enum_labels = (u->udisc_type () == AST_Expression::EV_enum);
185 for (UTL_LabellistActiveIterator i (this->pd_ll);
186 !i.is_done ();
187 i.next ())
189 if (AST_UnionLabel::UL_default == i.item ()->label_kind ())
191 continue;
194 AST_Expression *ex = i.item ()->label_val ();
195 UTL_ScopedName *n = ex->n ();
197 if (n)
199 u->add_to_name_referenced (n->first_component ());
202 // If we have enum val labels, we need to set the type and
203 // evaluate here, so the value will be available when the
204 // default index in calculated.
205 if (enum_labels)
207 ex->ev ()->et = AST_Expression::EV_enum;
208 AST_Enum *disc = dynamic_cast<AST_Enum*> (u->disc_type ());
209 AST_EnumVal *dval = disc->lookup_by_value (ex);
211 if (dval == 0)
213 idl_global->err ()->incompatible_disc_error (disc, ex);
214 throw Bailout ();
217 ex->ev ()->u.eval = dval->constant_value ()->ev ()->u.ulval;
222 void
223 AST_UnionBranch::coerce_labels (AST_Union *u)
225 for (unsigned long i = 0; i < this->label_list_length (); ++i)
227 AST_UnionLabel *ul = this->label (i);
229 if (ul->label_kind () == AST_UnionLabel::UL_default)
231 continue;
234 AST_Expression *lv = ul->label_val ();
235 lv->set_ev (lv->coerce (u->udisc_type ()));
239 IMPL_NARROW_FROM_DECL(AST_UnionBranch)