Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / TAO_IDL / ast / ast_constant.cpp
blobf3a51add81e2a3af6885f1a60e65775f8433080f
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_Constant nodes denote IDL constant declarations.
66 // AST_Constants have a value (an AST_Expression) and a value type
67 // (a value from the enum AST_Expression::ExprType).
68 // AST_Constant has two constructors, one for use in creating constants
69 // and the other for use in creating enumerators (see the class
70 // AST_EnumVal).
72 #include "ast_constant.h"
73 #include "utl_identifier.h"
74 #include "ast_visitor.h"
75 #include "ast_generator.h"
76 #include "nr_extern.h"
78 // Static functions.
80 // Convert a value from the enum AST_Expression::ExprType to a char *.
81 const char *
82 AST_Constant::exprtype_to_string (AST_Expression::ExprType et)
84 switch (et)
86 case AST_Expression::EV_short:
87 return "Short";
88 case AST_Expression::EV_ushort:
89 return "UShort";
90 case AST_Expression::EV_long:
91 return "Long";
92 case AST_Expression::EV_ulong:
93 return "ULong";
94 case AST_Expression::EV_float:
95 return "Float";
96 case AST_Expression::EV_double:
97 return "Double";
98 case AST_Expression::EV_char:
99 return "Char";
100 case AST_Expression::EV_octet:
101 return "Octet";
102 case AST_Expression::EV_bool:
103 return "Boolean";
104 case AST_Expression::EV_string:
105 return "Char*";
106 case AST_Expression::EV_ulonglong:
107 return "ULongLong";
108 case AST_Expression::EV_longlong:
109 return "LongLong";
110 case AST_Expression::EV_wchar:
111 return "Wchar";
112 case AST_Expression::EV_wstring:
113 return "Wchar*";
114 case AST_Expression::EV_longdouble:
115 return "LongDouble";
116 case AST_Expression::EV_fixed:
117 return "Fixed";
118 default:
119 break;
122 return 0;
125 AST_Decl::NodeType const
126 AST_Constant::NT = AST_Decl::NT_const;
128 // Used in constructing AST_EnumVal nodes.
129 AST_Constant::AST_Constant (AST_Expression::ExprType t,
130 AST_Decl::NodeType nt,
131 AST_Expression *v,
132 UTL_ScopedName *n)
133 : COMMON_Base (),
134 AST_Decl (nt,
136 pd_constant_value (v),
137 pd_et (t),
138 ifr_added_ (0)
142 // Used when constructing AST_Constant nodes.
143 AST_Constant::AST_Constant (AST_Expression::ExprType t,
144 AST_Expression *v,
145 UTL_ScopedName *n)
146 : COMMON_Base (),
147 AST_Decl (AST_Decl::NT_const,
149 pd_constant_value (v),
150 pd_et (t),
151 ifr_added_ (0)
153 // Avoids a truncation warning on MSVC when assigning a decimal
154 // literal to a float constant. Must also check that the input
155 // expression is of type double (indicates that we are being
156 // assigned from a literal). If v is of type float, it may be
157 // a constant-to-constant assignment - in any case the danger
158 // of truncation would not apply.
159 if (t == AST_Expression::EV_float && v->ev ()->et == AST_Expression::EV_double)
161 AST_Expression::AST_ExprValue *ev =
162 this->pd_constant_value->ev ();
163 ev->et = t;
164 ev->u.fval = (float) ev->u.dval;
166 // Allows the enum value string name to be used in generating the
167 // rhs of the constant assignment.
168 else if (t == AST_Expression::EV_enum)
170 this->pd_constant_value->ev ()->et = t;
174 AST_Constant::~AST_Constant (void)
178 // Redefinition of inherited virtual operations.
180 // Dump this AST_Constant node to the ostream o.
181 void
182 AST_Constant::dump (ACE_OSTREAM_TYPE &o)
184 this->dump_i (o, "const ");
185 this->dump_i (o, this->exprtype_to_string ());
186 this->dump_i (o, " ");
188 this->local_name ()->dump (o);
190 this->dump_i (o, " = ");
192 this->pd_constant_value->dump (o);
196 AST_Constant::ast_accept (ast_visitor *visitor)
198 return visitor->visit_constant (this);
201 void
202 AST_Constant::destroy (void)
204 if (this->pd_constant_value != 0)
206 this->pd_constant_value->destroy ();
207 delete this->pd_constant_value;
208 this->pd_constant_value = 0;
211 this->AST_Decl::destroy ();
214 // Data accessors.
216 AST_Expression *
217 AST_Constant::constant_value (void)
219 return this->pd_constant_value;
222 AST_Expression::ExprType
223 AST_Constant::et (void)
225 return this->pd_et;
228 bool
229 AST_Constant::ifr_added (void)
231 return this->ifr_added_;
234 void
235 AST_Constant::ifr_added (bool val)
237 this->ifr_added_ = val;
240 const char *
241 AST_Constant::exprtype_to_string (void)
243 switch (this->pd_et)
245 case AST_Expression::EV_short:
246 return "CORBA::Short";
247 case AST_Expression::EV_ushort:
248 return "CORBA::UShort";
249 case AST_Expression::EV_long:
250 return "CORBA::Long";
251 case AST_Expression::EV_ulong:
252 return "CORBA::ULong";
253 case AST_Expression::EV_float:
254 return "CORBA::Float";
255 case AST_Expression::EV_double:
256 return "CORBA::Double";
257 case AST_Expression::EV_char:
258 return "CORBA::Char";
259 case AST_Expression::EV_octet:
260 return "CORBA::Octet";
261 case AST_Expression::EV_bool:
262 return "CORBA::Boolean";
263 case AST_Expression::EV_string:
264 return "char *const";
265 case AST_Expression::EV_void:
266 return "void";
267 case AST_Expression::EV_none:
268 return "none";
269 case AST_Expression::EV_longlong:
270 return "CORBA::LongLong";
271 case AST_Expression::EV_ulonglong:
272 return "CORBA::ULongLong";
273 case AST_Expression::EV_wchar:
274 return "CORBA::WChar";
275 case AST_Expression::EV_wstring:
276 return "CORBA::WChar *const";
277 case AST_Expression::EV_fixed:
278 return "Fixed";
279 default:
280 return 0;
283 return 0;
286 UTL_ScopedName *
287 AST_Constant::enum_full_name (void)
289 if (this->pd_et == AST_Expression::EV_enum)
291 UTL_Scope * const s = this->defined_in ();
292 AST_Decl * const d = s->lookup_by_name (this->pd_constant_value->n (),
294 return (d ? (ScopeAsDecl (d->defined_in ()))->name () : 0);
296 else
298 return 0;
302 IMPL_NARROW_FROM_DECL(AST_Constant)