Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / TAO_IDL / be / be_union_branch.cpp
blobec8442d5f435014a51d91585751f81440ff8d0fe
2 //=============================================================================
3 /**
4 * @file be_union_branch.cpp
6 * Extension of class AST_UnionBranch that provides additional means for C++
7 * mapping.
9 * @author Copyright 1994-1995 by Sun Microsystems
10 * @author Inc. and Aniruddha Gokhale
12 //=============================================================================
14 #include "be_union_branch.h"
15 #include "be_union.h"
16 #include "be_type.h"
17 #include "be_enum.h"
18 #include "be_visitor.h"
19 #include "be_helper.h"
20 #include "ast_union_label.h"
21 #include "ace/Log_Msg.h"
23 be_union_branch::be_union_branch (UTL_LabelList *ll,
24 AST_Type *ft,
25 UTL_ScopedName *n)
26 : COMMON_Base (ft->is_local (),
27 ft->is_abstract ()),
28 AST_Decl (AST_Decl::NT_union_branch,
29 n),
30 AST_Field (AST_Decl::NT_union_branch,
31 ft,
32 n),
33 AST_UnionBranch (ll,
34 ft,
35 n),
36 be_decl (AST_Decl::NT_union_branch,
37 n),
38 be_field (ft,
43 int
44 be_union_branch::gen_label_value (TAO_OutStream *os, unsigned long index)
46 AST_Expression *e = this->label (index)->label_val ();
48 if (e->ec () != AST_Expression::EC_symbol)
50 // Easy, just a number...
51 *os << e;
52 return 0;
55 // If the enum is not in the global scope we have to prefix it.
56 be_union *u =
57 dynamic_cast<be_union*> (this->defined_in ());
59 if (u == 0)
61 return -1;
64 be_type* dt =
65 dynamic_cast<be_type*> (u->disc_type ());
67 if (dt == 0)
69 return -1;
72 // Check if discriminator is a typedef of an integer. If so, and the
73 // first IF block in this function didn't catch it, then we
74 // are a constant of this type. We can't generate the constant's name,
75 // we must generate the underlying integer value for the
76 // label value.
77 if (dt->node_type () == AST_Decl::NT_pre_defined)
79 *os << e;
80 return 0;
83 // Find where was the enum defined, if it was defined in the globa
84 // scope, then it is easy to generate the enum values....
85 be_scope* scope =
86 dynamic_cast<be_scope*> (dt->defined_in ());
88 if (scope == 0)
90 *os << e->n ();
91 return 0;
94 // But if it was generated inside a module or something similar then
95 // we must prefix the enum value with something...
96 be_decl* decl = scope->decl ();
98 *os << decl->full_name () << "::" << e->n ()->last_component ();
100 return 0;
104 be_union_branch::gen_default_label_value (TAO_OutStream *os,
105 be_union *bu)
107 be_union::DefaultValue dv;
109 if (bu->default_value (dv) == -1)
111 ACE_ERROR_RETURN ((LM_ERROR,
112 "(%N:%l) be_visitor_union_branch::"
113 "gen_default_label_value - "
114 "computing default value failed\n"),
115 -1);
118 switch (bu->udisc_type ())
120 case AST_Expression::EV_short:
121 *os << dv.u.short_val;
122 break;
123 case AST_Expression::EV_ushort:
124 *os << dv.u.ushort_val;
125 break;
126 case AST_Expression::EV_long:
127 *os << dv.u.long_val;
128 break;
129 case AST_Expression::EV_ulong:
130 *os << dv.u.ulong_val;
131 break;
132 case AST_Expression::EV_octet:
133 case AST_Expression::EV_char:
134 case AST_Expression::EV_int8:
135 case AST_Expression::EV_uint8:
136 os->print ("'\\%o'", dv.u.char_val);
137 break;
138 case AST_Expression::EV_bool:
139 *os << (dv.u.bool_val == 0 ? "false" : "true");
140 break;
141 case AST_Expression::EV_enum:
142 // The discriminant is an enum. Some compilers will
143 // not accept a numeric value assigned to this
144 // discriminant, so we must generate the string name.
146 AST_ConcreteType *act = bu->disc_type ();
147 be_enum *be = dynamic_cast<be_enum*> (act);
149 UTL_ScopedName *sn = be->value_to_name (dv.u.enum_val);
150 if (sn)
152 // The function value_to_name() takes care of adding
153 // any necessary scoping to the output.
154 *os << be->value_to_name (dv.u.enum_val);
156 else
158 // Since CORBA defines enums to be 32bits, use -1 as the
159 // out-of-bounds value for the _default() function.
160 *os << "(" << be->name () << ") -1";
162 break;
164 case AST_Expression::EV_longlong:
165 *os << dv.u.longlong_val;
166 break;
167 case AST_Expression::EV_ulonglong:
168 *os << dv.u.ulonglong_val;
169 break;
170 default:
171 // Error caught earlier.
172 ACE_ERROR_RETURN ((LM_ERROR,
173 "(%N:%l) be_visitor_union_branch::"
174 "gen_default_label_value - "
175 "bad or unimplemented discriminant type\n"),
176 -1);
179 return 0;
183 be_union_branch::accept (be_visitor *visitor)
185 return visitor->visit_union_branch (this);
188 void
189 be_union_branch::destroy (void)
191 this->be_decl::destroy ();
192 this->AST_UnionBranch::destroy ();
197 IMPL_NARROW_FROM_DECL (be_union_branch)