Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / TAO_IDL / be / be_union_branch.cpp
blob06a1416565cbe92c25c2612fcf7055d297489812
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 == nullptr)
61 return -1;
64 be_type* dt =
65 dynamic_cast<be_type*> (u->disc_type ());
67 if (dt == nullptr)
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 == nullptr)
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 case AST_Expression::EV_wchar:
125 *os << dv.u.ushort_val;
126 break;
127 case AST_Expression::EV_long:
128 *os << dv.u.long_val;
129 break;
130 case AST_Expression::EV_ulong:
131 *os << dv.u.ulong_val;
132 break;
133 case AST_Expression::EV_octet:
134 case AST_Expression::EV_char:
135 case AST_Expression::EV_int8:
136 case AST_Expression::EV_uint8:
137 os->print ("'\\%o'", dv.u.char_val);
138 break;
139 case AST_Expression::EV_bool:
140 *os << (dv.u.bool_val == 0 ? "false" : "true");
141 break;
142 case AST_Expression::EV_enum:
143 // The discriminant is an enum. Some compilers will
144 // not accept a numeric value assigned to this
145 // discriminant, so we must generate the string name.
147 AST_ConcreteType *act = bu->disc_type ();
148 be_enum *be = dynamic_cast<be_enum*> (act);
150 UTL_ScopedName *sn = be->value_to_name (dv.u.enum_val);
151 if (sn)
153 // The function value_to_name() takes care of adding
154 // any necessary scoping to the output.
155 *os << be->value_to_name (dv.u.enum_val);
157 else
159 // Since CORBA defines enums to be 32bits, use -1 as the
160 // out-of-bounds value for the _default() function.
161 *os << "(" << be->name () << ") -1";
163 break;
165 case AST_Expression::EV_longlong:
166 *os << dv.u.longlong_val;
167 break;
168 case AST_Expression::EV_ulonglong:
169 *os << dv.u.ulonglong_val;
170 break;
171 default:
172 // Error caught earlier.
173 ACE_ERROR_RETURN ((LM_ERROR,
174 "(%N:%l) be_visitor_union_branch::"
175 "gen_default_label_value - "
176 "bad or unimplemented discriminant type\n"),
177 -1);
180 return 0;
184 be_union_branch::accept (be_visitor *visitor)
186 return visitor->visit_union_branch (this);
189 void
190 be_union_branch::destroy ()
192 this->be_decl::destroy ();
193 this->AST_UnionBranch::destroy ();