Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / TAO_IDL / ast / ast_expression.cpp
blobe7758a2d5a8c15259cad1102db6300a0715f85fb
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_Expression nodes denote IDL expressions used in the IDL input.
67 #include "ast_expression.h"
68 #include "ast_constant.h"
69 #include "ast_typedef.h"
70 #include "ast_param_holder.h"
71 #include "ast_visitor.h"
72 #include "ast_generator.h"
73 #include "ast_enum_val.h"
75 #include "utl_err.h"
76 #include "utl_scope.h"
77 #include "utl_string.h"
79 #include "nr_extern.h"
80 #include "global_extern.h"
82 #include "ace/ACE.h"
83 #include "ace/OS_NS_stdio.h"
84 // FUZZ: disable check_for_streams_include
85 #include "ace/streams.h"
87 AST_Expression::ExprType
88 AST_Expression::eval_kind_to_expr_type (AST_Expression::EvalKind eval_kind)
90 switch (eval_kind)
92 case EK_bool:
93 return EV_bool;
94 case EK_short:
95 return EV_short;
96 case EK_ushort:
97 return EV_ushort;
98 case EK_long:
99 return EV_long;
100 case EK_ulong:
101 case EK_positive_int:
102 return EV_ulong;
103 case EK_longlong:
104 return EV_longlong;
105 case EK_ulonglong:
106 return EV_ulonglong;
107 case EK_octet:
108 return EV_octet;
109 case EK_floating_point:
110 return EV_double;
111 case EK_fixed_point:
112 return EV_fixed;
113 case EK_int8:
114 return EV_int8;
115 case EK_uint8:
116 return EV_uint8;
117 case EK_const:
118 idl_global->err ()->misc_error ("eval_kind_to_expr_type can't handle EK_const");
119 return EV_none;
122 idl_global->err ()->misc_error ("eval_kind_to_expr_type unhandled EvalKind");
123 return EV_none;
126 // Helper function to fill out the details of where this expression
127 // is defined.
128 void
129 AST_Expression::fill_definition_details (void)
131 this->pd_defined_in = idl_global->scopes ().depth () > 0
132 ? idl_global->scopes ().top ()
133 : 0 ;
134 this->pd_line = idl_global->lineno ();
135 this->pd_file_name = idl_global->filename ();
136 enum_parent (0);
139 // An AST_Expression denoting a symbolic name.
140 AST_Expression::AST_Expression (UTL_ScopedName *nm)
141 : pd_ec (EC_symbol),
142 pd_ev (0),
143 pd_v1 (0),
144 pd_v2 (0),
145 pd_n (nm),
146 tdef (0),
147 param_holder_ (0)
149 this->fill_definition_details ();
151 AST_Decl *d =
152 idl_global->scopes ().top_non_null ()->lookup_by_name (nm,
153 true);
155 if (d->node_type () == AST_Decl::NT_param_holder)
157 this->param_holder_ =
158 dynamic_cast<AST_Param_Holder*> (d);
162 // An AST_Expression denoting a type coercion from another AST_Expression.
163 AST_Expression::AST_Expression (AST_Expression *v,
164 ExprType t)
165 : pd_ec (EC_none),
166 pd_ev (0),
167 pd_v1 (0),
168 pd_v2 (0),
169 pd_n (0),
170 tdef (0),
171 param_holder_ (0)
173 AST_Param_Holder *ph = v->param_holder_;
174 this->fill_definition_details ();
175 enum_parent (v->enum_parent ());
177 // If we are here because one string constant has
178 // another one as its rhs, we must copy the UTL_String
179 // so both can be destroyed at cleanup.
180 if (EV_string == t)
182 ACE_NEW (this->pd_ev,
183 AST_ExprValue);
185 ACE_NEW (this->pd_ev->u.strval,
186 UTL_String (v->pd_ev->u.strval, true));
188 this->pd_ev->et = EV_string;
190 else if (EV_wstring == t)
192 ACE_NEW (this->pd_ev,
193 AST_ExprValue);
195 this->pd_ev->u.wstrval = ACE::strnew (v->pd_ev->u.wstrval);
196 this->pd_ev->et = EV_string;
198 else
200 if (ph == 0)
202 this->pd_ev = v->coerce (t);
204 if (this->pd_ev == 0)
206 idl_global->err ()->coercion_error (v, t);
209 else
211 // The param info member isn't managed by this type,
212 // so we can just pass the pointer to the new expr.
213 this->param_holder_ =
214 idl_global->gen ()->create_param_holder (
215 ph->name (),
216 const_cast<FE_Utils::T_Param_Info *> (ph->info ()));
219 if (0 != v->pd_n)
221 this->pd_n =
222 dynamic_cast<UTL_ScopedName *> (v->pd_n->copy ());
227 // An AST_Expression denoting a binary expression combination from
228 // two other AST_Expressions.
229 AST_Expression::AST_Expression (ExprComb c,
230 AST_Expression *ev1,
231 AST_Expression *ev2)
232 : pd_ec (c),
233 pd_ev (0),
234 pd_v1 (ev1),
235 pd_v2 (ev2),
236 pd_n (0),
237 tdef (0),
238 param_holder_ (0)
240 this->fill_definition_details ();
243 // An AST_Expression denoting a short integer.
244 AST_Expression::AST_Expression (ACE_CDR::Short sv)
245 : pd_ec (EC_none),
246 pd_ev (0),
247 pd_v1 (0),
248 pd_v2 (0),
249 pd_n (0),
250 tdef (0),
251 param_holder_ (0)
253 this->fill_definition_details ();
255 ACE_NEW (this->pd_ev,
256 AST_ExprValue);
258 this->pd_ev->et = EV_short;
259 this->pd_ev->u.sval = sv;
262 // An AST_Expression denoting an unsigned short integer.
263 AST_Expression::AST_Expression (ACE_CDR::UShort usv)
264 : pd_ec (EC_none),
265 pd_ev (0),
266 pd_v1 (0),
267 pd_v2 (0),
268 pd_n (0),
269 tdef (0),
270 param_holder_ (0)
272 this->fill_definition_details ();
274 ACE_NEW (this->pd_ev,
275 AST_ExprValue);
277 this->pd_ev->et = EV_ushort;
278 this->pd_ev->u.usval = usv;
281 // An AST_Expression denoting a long integer.
282 AST_Expression::AST_Expression (ACE_CDR::Long lv)
283 : pd_ec (EC_none),
284 pd_ev (0),
285 pd_v1 (0),
286 pd_v2 (0),
287 pd_n (0),
288 tdef (0),
289 param_holder_ (0)
291 this->fill_definition_details ();
293 ACE_NEW (this->pd_ev,
294 AST_ExprValue);
296 this->pd_ev->et = EV_long;
297 this->pd_ev->u.lval = lv;
300 // An AST_Expression denoting a long integer.
301 AST_Expression::AST_Expression (ACE_CDR::LongLong llv)
302 : pd_ec (EC_none),
303 pd_ev (0),
304 pd_v1 (0),
305 pd_v2 (0),
306 pd_n (0),
307 tdef (0),
308 param_holder_ (0)
310 this->fill_definition_details ();
312 ACE_NEW (this->pd_ev,
313 AST_ExprValue);
315 this->pd_ev->et = EV_longlong;
316 this->pd_ev->u.llval = llv;
319 // An AST_Expression denoting a boolean.
320 AST_Expression::AST_Expression (ACE_CDR::Boolean b)
321 : pd_ec (EC_none),
322 pd_ev (0),
323 pd_v1 (0),
324 pd_v2 (0),
325 pd_n (0),
326 tdef (0),
327 param_holder_ (0)
329 this->fill_definition_details ();
331 ACE_NEW (this->pd_ev,
332 AST_ExprValue);
334 this->pd_ev->et = EV_bool;
335 this->pd_ev->u.bval = b;
338 // An AST_Expression denoting an unsigned long integer.
339 AST_Expression::AST_Expression (ACE_CDR::ULong ulv)
340 : pd_ec (EC_none),
341 pd_ev (0),
342 pd_v1 (0),
343 pd_v2 (0),
344 pd_n (0),
345 tdef (0),
346 param_holder_ (0)
348 this->fill_definition_details ();
350 ACE_NEW (this->pd_ev,
351 AST_ExprValue);
353 this->pd_ev->et = EV_ulong;
354 this->pd_ev->u.ulval = ulv;
357 // An AST_Expression denoting an unsigned long long integer.
358 AST_Expression::AST_Expression (ACE_CDR::ULongLong ulv)
359 : pd_ec (EC_none),
360 pd_ev (0),
361 pd_v1 (0),
362 pd_v2 (0),
363 pd_n (0),
364 tdef (0),
365 param_holder_ (0)
367 this->fill_definition_details ();
369 ACE_NEW (this->pd_ev,
370 AST_ExprValue);
372 this->pd_ev->et = EV_ulonglong;
373 this->pd_ev->u.ullval = ulv;
376 // An AST_Expression denoting an unsigned long integer.
377 AST_Expression::AST_Expression (ACE_CDR::ULong ulv,
378 ExprType t)
379 : pd_ec (EC_none),
380 pd_ev (0),
381 pd_v1 (0),
382 pd_v2 (0),
383 pd_n (0),
384 tdef (0),
385 param_holder_ (0)
387 this->fill_definition_details ();
389 ACE_NEW (this->pd_ev,
390 AST_ExprValue);
392 this->pd_ev->et = t;
393 this->pd_ev->u.ullval = ulv;
396 // An AST_Expression denoting a 32-bit floating point number.
397 AST_Expression::AST_Expression (ACE_CDR::Float fv)
398 : pd_ec (EC_none),
399 pd_ev (0),
400 pd_v1 (0),
401 pd_v2 (0),
402 pd_n (0),
403 tdef (0),
404 param_holder_ (0)
406 this->fill_definition_details ();
408 ACE_NEW (this->pd_ev,
409 AST_ExprValue);
411 this->pd_ev->et = EV_float;
412 this->pd_ev->u.fval = fv;
415 // An AST_Expression denoting a 64-bit floating point number.
416 AST_Expression::AST_Expression (ACE_CDR::Double dv)
417 : pd_ec (EC_none),
418 pd_ev (0),
419 pd_v1 (0),
420 pd_v2 (0),
421 pd_n (0),
422 tdef (0),
423 param_holder_ (0)
425 this->fill_definition_details ();
427 ACE_NEW (this->pd_ev,
428 AST_ExprValue);
430 this->pd_ev->et = EV_double;
431 this->pd_ev->u.dval = dv;
434 // An AST_Expression denoting a character.
435 AST_Expression::AST_Expression (ACE_CDR::Char cv)
436 : pd_ec (EC_none),
437 pd_ev (0),
438 pd_v1 (0),
439 pd_v2 (0),
440 pd_n (0),
441 tdef (0),
442 param_holder_ (0)
444 this->fill_definition_details ();
446 ACE_NEW (this->pd_ev,
447 AST_ExprValue);
449 this->pd_ev->et = EV_char;
450 this->pd_ev->u.cval = cv;
453 // An AST_Expression denoting a wide character.
454 AST_Expression::AST_Expression (ACE_OutputCDR::from_wchar wcv)
455 : pd_ec (EC_none),
456 pd_ev (0),
457 pd_v1 (0),
458 pd_v2 (0),
459 pd_n (0),
460 tdef (0),
461 param_holder_ (0)
463 this->fill_definition_details ();
465 ACE_NEW (this->pd_ev,
466 AST_ExprValue);
468 this->pd_ev->et = EV_wchar;
469 this->pd_ev->u.wcval = wcv.val_;
472 // An AST_Expression denoting an octet (unsigned char).
473 AST_Expression::AST_Expression (ACE_CDR::Octet ov)
474 : pd_ec (EC_none),
475 pd_ev (0),
476 pd_v1 (0),
477 pd_v2 (0),
478 pd_n (0),
479 tdef (0),
480 param_holder_ (0)
482 this->fill_definition_details ();
484 ACE_NEW (this->pd_ev,
485 AST_ExprValue);
487 this->pd_ev->et = EV_octet;
488 this->pd_ev->u.oval = ov;
491 // An AST_Expression denoting a string (char * encapsulated as a String).
492 AST_Expression::AST_Expression (UTL_String *sv)
493 : pd_ec (EC_none),
494 pd_ev (0),
495 pd_v1 (0),
496 pd_v2 (0),
497 pd_n (0),
498 tdef (0),
499 param_holder_ (0)
501 this->fill_definition_details ();
503 ACE_NEW (this->pd_ev,
504 AST_ExprValue);
506 UTL_String *new_str = 0;
507 ACE_NEW (new_str,
508 UTL_String (sv, true));
510 this->pd_ev->u.strval = new_str;
511 this->pd_ev->et = EV_string;
514 // An AST_Expression denoting a wide string.
515 AST_Expression::AST_Expression (char *sv)
516 : pd_ec (EC_none),
517 pd_ev (0),
518 pd_v1 (0),
519 pd_v2 (0),
520 pd_n (0),
521 tdef (0),
522 param_holder_ (0)
524 this->fill_definition_details ();
526 ACE_NEW (this->pd_ev,
527 AST_ExprValue);
529 this->pd_ev->et = EV_wstring;
530 this->pd_ev->u.wstrval = ACE::strnew (sv);
533 AST_Expression::AST_Expression (const ACE_CDR::Fixed &f)
534 : pd_ec (EC_none),
535 pd_ev (0),
536 pd_v1 (0),
537 pd_v2 (0),
538 pd_n (0),
539 tdef (0),
540 param_holder_ (0)
542 this->fill_definition_details ();
544 ACE_NEW (this->pd_ev,
545 AST_ExprValue);
547 this->pd_ev->et = EV_fixed;
548 this->pd_ev->u.fixedval = f;
551 AST_Expression::~AST_Expression (void)
553 destroy ();
556 AST_Expression::AST_ExprValue::AST_ExprValue (void)
558 this->u.ulval = 0UL;
559 this->et = AST_Expression::EV_none;
562 // Static operations.
564 // Perform the coercion from the given AST_ExprValue to the requested
565 // ExprType. Return an AST_ExprValue if successful, 0 if failed.
566 static AST_Expression::AST_ExprValue *
567 coerce_value (AST_Expression::AST_ExprValue *ev,
568 AST_Expression::ExprType t)
570 if (ev == 0)
572 return 0;
575 if (t == ev->et)
577 return ev;
580 // Avoid Coverity issue with "Assignment of overlapping memory" by using a
581 // temporary value and setting ev->u at the end if there wasn't an error.
582 AST_Expression::AST_ExprValue::Value tmp;
583 switch (t)
585 case AST_Expression::EV_short:
586 switch (ev->et)
588 case AST_Expression::EV_ushort:
589 if (ev->u.usval > (ACE_CDR::UShort) ACE_INT16_MAX)
591 return 0;
594 tmp.sval = (ACE_CDR::Short) ev->u.usval;
595 break;
596 case AST_Expression::EV_long:
597 if (ev->u.lval > (ACE_CDR::Long) ACE_INT16_MAX
598 || ev->u.lval < (ACE_CDR::Long) ACE_INT16_MIN)
600 return 0;
603 tmp.sval = (ACE_CDR::Short) ev->u.lval;
604 break;
605 case AST_Expression::EV_ulong:
606 if (ev->u.ulval > (ACE_CDR::ULong) ACE_INT16_MAX)
608 return 0;
611 tmp.sval = (ACE_CDR::Short) ev->u.ulval;
612 break;
613 case AST_Expression::EV_longlong:
614 if (ev->u.llval > (ACE_CDR::LongLong) ACE_INT16_MAX
615 || ev->u.llval < (ACE_CDR::LongLong) ACE_INT16_MIN)
617 return 0;
620 tmp.sval = (ACE_CDR::Short) ev->u.llval;
621 break;
622 case AST_Expression::EV_ulonglong:
623 if ((ev->u.ullval & ACE_INT16_MAX) != ev->u.ullval)
625 return 0;
628 tmp.sval = (ACE_CDR::Short) ev->u.ullval;
629 break;
630 case AST_Expression::EV_bool:
631 tmp.sval = (ACE_CDR::Short) ev->u.bval;
632 break;
633 case AST_Expression::EV_float:
634 if (ev->u.fval > (ACE_CDR::Float) ACE_INT16_MAX
635 || ev->u.fval < (ACE_CDR::Float) ACE_INT16_MIN)
637 return 0;
640 tmp.sval = (ACE_CDR::Short) ev->u.fval;
641 break;
642 case AST_Expression::EV_double:
643 if (ev->u.dval > (ACE_CDR::Double) ACE_INT16_MAX
644 || ev->u.dval < (ACE_CDR::Double) ACE_INT16_MIN)
646 return 0;
649 tmp.sval = (ACE_CDR::Short) ev->u.dval;
650 break;
651 case AST_Expression::EV_char:
652 tmp.sval = (ACE_CDR::Short) ev->u.cval;
653 break;
654 case AST_Expression::EV_wchar:
655 if (ev->u.wcval > (ACE_CDR::WChar) ACE_INT16_MAX)
657 return 0;
660 tmp.sval = (ACE_CDR::Short) ev->u.wcval;
661 break;
662 case AST_Expression::EV_octet:
663 tmp.sval = (ACE_CDR::Short) ev->u.oval;
664 break;
665 case AST_Expression::EV_int8:
666 tmp.sval = (ACE_CDR::Short) ev->u.int8val;
667 break;
668 case AST_Expression::EV_uint8:
669 tmp.sval = (ACE_CDR::Short) ev->u.uint8val;
670 break;
671 default:
672 return 0;
674 break;
676 case AST_Expression::EV_ushort:
677 switch (ev->et)
679 case AST_Expression::EV_short:
680 if (ev->u.sval < 0)
682 return 0;
685 tmp.usval = (ACE_CDR::UShort) ev->u.sval;
686 break;
687 case AST_Expression::EV_long:
688 if (ev->u.lval > (ACE_CDR::Long) ACE_UINT16_MAX
689 || ev->u.lval < 0)
691 return 0;
694 tmp.usval = (ACE_CDR::UShort) ev->u.lval;
695 break;
696 case AST_Expression::EV_ulong:
697 if (ev->u.ulval > (ACE_CDR::ULong) ACE_UINT16_MAX)
699 return 0;
702 tmp.usval = (ACE_CDR::UShort) ev->u.ulval;
703 break;
704 case AST_Expression::EV_longlong:
705 if (ev->u.llval > (ACE_CDR::LongLong) ACE_UINT16_MAX
706 || ev->u.llval < 0)
708 return 0;
711 tmp.usval = (ACE_CDR::UShort) ev->u.llval;
712 break;
713 case AST_Expression::EV_ulonglong:
714 if ((ev->u.ullval & ACE_UINT16_MAX) != ev->u.ullval)
716 return 0;
719 tmp.usval = (ACE_CDR::UShort) ev->u.ullval;
720 break;
721 case AST_Expression::EV_bool:
722 tmp.usval = (ACE_CDR::UShort) ev->u.bval;
723 break;
724 case AST_Expression::EV_float:
725 if (ev->u.fval < 0.0
726 || ev->u.fval > (ACE_CDR::Float) ACE_UINT16_MAX)
728 return 0;
731 tmp.usval = (ACE_CDR::UShort) ev->u.fval;
732 break;
733 case AST_Expression::EV_double:
734 if (ev->u.dval < 0.0
735 || ev->u.dval > (ACE_CDR::Double) ACE_UINT16_MAX)
737 return 0;
740 tmp.usval = (ACE_CDR::UShort) ev->u.dval;
741 break;
742 case AST_Expression::EV_char:
743 if ((signed char) ev->u.cval < 0)
745 return 0;
748 tmp.usval = (ACE_CDR::UShort) ev->u.cval;
749 break;
750 case AST_Expression::EV_wchar:
751 tmp.usval = (ACE_CDR::UShort) ev->u.wcval;
752 break;
753 case AST_Expression::EV_octet:
754 tmp.usval = (ACE_CDR::UShort) ev->u.oval;
755 break;
756 case AST_Expression::EV_int8:
757 if (ev->u.int8val < 0) return 0;
758 tmp.usval = static_cast<ACE_CDR::UShort> (ev->u.int8val);
759 break;
760 case AST_Expression::EV_uint8:
761 tmp.usval = static_cast<ACE_CDR::UShort> (ev->u.uint8val);
762 break;
763 default:
764 return 0;
766 break;
768 case AST_Expression::EV_long:
769 switch (ev->et)
771 case AST_Expression::EV_short:
772 tmp.lval = (ACE_CDR::Long) ev->u.sval;
773 break;
774 case AST_Expression::EV_ushort:
775 tmp.lval = (ACE_CDR::Long) ev->u.usval;
776 break;
777 case AST_Expression::EV_ulong:
778 if (ev->u.ulval > (ACE_CDR::ULong) ACE_INT32_MAX)
780 return 0;
783 tmp.lval = (ACE_CDR::Long) ev->u.ulval;
784 break;
785 case AST_Expression::EV_longlong:
786 if (ev->u.llval > (ACE_CDR::LongLong) ACE_INT32_MAX
787 || ev->u.llval < (ACE_CDR::LongLong) ACE_INT32_MIN)
789 return 0;
791 tmp.lval = (ACE_CDR::Long) ev->u.llval;
792 break;
793 case AST_Expression::EV_ulonglong:
794 if ((ev->u.ullval & ACE_INT32_MAX) != ev->u.ullval)
796 return 0;
799 tmp.lval = (ACE_CDR::Long) ev->u.ullval;
800 break;
801 case AST_Expression::EV_bool:
802 tmp.lval = (ACE_CDR::Long) ev->u.bval;
803 break;
804 case AST_Expression::EV_float:
805 if (ev->u.fval > (ACE_CDR::Float) ACE_INT32_MAX
806 || ev->u.fval < (ACE_CDR::Float) ACE_INT32_MIN)
808 return 0;
811 tmp.lval = (ACE_CDR::Long) ev->u.fval;
812 break;
813 case AST_Expression::EV_double:
814 if (ev->u.dval > (ACE_CDR::Double) ACE_INT32_MAX
815 || ev->u.dval < (ACE_CDR::Double) ACE_INT32_MIN)
817 return 0;
820 tmp.lval = (ACE_CDR::Long) ev->u.dval;
821 break;
822 case AST_Expression::EV_char:
823 tmp.lval = (ACE_CDR::Long) ev->u.cval;
824 break;
825 case AST_Expression::EV_wchar:
826 tmp.lval = (ACE_CDR::Long) ev->u.wcval;
827 break;
828 case AST_Expression::EV_octet:
829 tmp.lval = (ACE_CDR::Long) ev->u.oval;
830 break;
831 case AST_Expression::EV_int8:
832 tmp.lval = static_cast<ACE_CDR::Long> (ev->u.int8val);
833 break;
834 case AST_Expression::EV_uint8:
835 tmp.lval = static_cast<ACE_CDR::Long> (ev->u.uint8val);
836 break;
837 default:
838 return 0;
840 break;
842 case AST_Expression::EV_ulong:
843 switch (ev->et)
845 case AST_Expression::EV_short:
846 if (ev->u.sval < 0)
848 return 0;
851 tmp.ulval = (ACE_CDR::ULong) ev->u.sval;
852 break;
853 case AST_Expression::EV_ushort:
854 tmp.ulval = (ACE_CDR::ULong) ev->u.usval;
855 break;
856 case AST_Expression::EV_long:
857 if (ev->u.lval < 0)
859 return 0;
862 tmp.ulval = (ACE_CDR::ULong) ev->u.lval;
863 break;
864 case AST_Expression::EV_longlong:
865 if (ev->u.llval > (ACE_CDR::LongLong) ACE_UINT32_MAX
866 || ev->u.llval < 0)
868 return 0;
871 tmp.ulval = (ACE_CDR::ULong) ev->u.llval;
872 break;
873 case AST_Expression::EV_ulonglong:
874 if ((ev->u.ullval & ACE_UINT32_MAX) != ev->u.ullval)
876 return 0;
878 tmp.ulval = (ACE_CDR::ULong) ev->u.ullval;
879 break;
880 case AST_Expression::EV_bool:
881 tmp.ulval = (ACE_CDR::ULong) ev->u.bval;
882 break;
883 case AST_Expression::EV_float:
884 if (ev->u.fval < 0.0
885 || ev->u.fval > (ACE_CDR::Float) ACE_UINT32_MAX)
887 return 0;
890 tmp.ulval = (ACE_CDR::ULong) ev->u.fval;
891 break;
892 case AST_Expression::EV_double:
893 if (ev->u.dval < 0.0
894 || ev->u.dval > (ACE_CDR::Double) ACE_UINT32_MAX)
896 return 0;
899 tmp.ulval = (ACE_CDR::ULong) ev->u.dval;
900 break;
901 case AST_Expression::EV_char:
902 if ((signed char) ev->u.cval < 0)
904 return 0;
907 tmp.ulval = (ACE_CDR::ULong) ev->u.cval;
908 break;
909 case AST_Expression::EV_wchar:
910 tmp.ulval = (ACE_CDR::ULong) ev->u.wcval;
911 break;
912 case AST_Expression::EV_octet:
913 tmp.ulval = (ACE_CDR::ULong) ev->u.oval;
914 break;
915 case AST_Expression::EV_int8:
916 if (ev->u.int8val < 0) return 0;
917 tmp.ulval = static_cast<ACE_CDR::ULong> (ev->u.int8val);
918 break;
919 case AST_Expression::EV_uint8:
920 tmp.ulval = static_cast<ACE_CDR::ULong> (ev->u.uint8val);
921 break;
922 default:
923 return 0;
925 break;
927 case AST_Expression::EV_longlong:
928 switch (ev->et)
930 case AST_Expression::EV_short:
931 tmp.llval = (ACE_CDR::LongLong) ev->u.sval;
932 break;
933 case AST_Expression::EV_ushort:
934 tmp.llval = (ACE_CDR::LongLong) ev->u.usval;
935 break;
936 case AST_Expression::EV_long:
937 tmp.llval = (ACE_CDR::LongLong) ev->u.lval;
938 break;
939 case AST_Expression::EV_ulong:
940 tmp.llval = (ACE_CDR::LongLong) ev->u.ulval;
941 break;
942 case AST_Expression::EV_ulonglong:
943 if (ev->u.ullval > ACE_INT64_MAX)
945 return 0;
948 tmp.llval = static_cast<ACE_CDR::LongLong> (ev->u.ullval);
949 break;
950 case AST_Expression::EV_bool:
951 tmp.llval = (ACE_CDR::LongLong) ev->u.bval;
952 break;
953 case AST_Expression::EV_float:
954 if (ev->u.fval > (ACE_CDR::Float) ACE_INT64_MAX
955 || ev->u.fval < (ACE_CDR::Float) ACE_INT64_MIN)
957 return 0;
960 tmp.llval = (ACE_CDR::LongLong) ev->u.fval;
961 break;
962 case AST_Expression::EV_double:
963 if (ev->u.dval > (ACE_CDR::Double) ACE_INT64_MAX
964 || ev->u.dval < (ACE_CDR::Double) ACE_INT64_MIN)
966 return 0;
969 tmp.llval = (ACE_CDR::LongLong) ev->u.dval;
970 break;
971 case AST_Expression::EV_char:
972 tmp.llval = (ACE_CDR::LongLong) ev->u.cval;
973 break;
974 case AST_Expression::EV_wchar:
975 tmp.llval = (ACE_CDR::LongLong) ev->u.wcval;
976 break;
977 case AST_Expression::EV_octet:
978 tmp.llval = (ACE_CDR::LongLong) ev->u.oval;
979 break;
980 case AST_Expression::EV_int8:
981 tmp.llval = static_cast<ACE_CDR::LongLong> (ev->u.int8val);
982 break;
983 case AST_Expression::EV_uint8:
984 tmp.llval = static_cast<ACE_CDR::LongLong> (ev->u.int8val);
985 break;
986 default:
987 return 0;
989 break;
991 case AST_Expression::EV_ulonglong:
992 switch (ev->et)
994 case AST_Expression::EV_short:
995 if (ev->u.sval < 0)
997 return 0;
1000 tmp.ullval = ev->u.sval;
1001 break;
1002 case AST_Expression::EV_ushort:
1003 tmp.ullval = ev->u.usval;
1004 break;
1005 case AST_Expression::EV_long:
1006 if (ev->u.lval < 0)
1008 return 0;
1011 tmp.ullval = ev->u.lval;
1012 break;
1013 case AST_Expression::EV_ulong:
1014 tmp.ullval = ev->u.ulval;
1015 break;
1016 case AST_Expression::EV_longlong:
1017 if (ev->u.llval < 0)
1019 return 0;
1022 tmp.ullval = static_cast<ACE_CDR::LongLong> (ev->u.llval);
1023 break;
1024 case AST_Expression::EV_bool:
1025 tmp.ullval = ev->u.bval;
1026 break;
1027 case AST_Expression::EV_float:
1028 if (ev->u.fval < 0.0
1029 || ev->u.fval > (ACE_CDR::Float) ACE_UINT64_MAX)
1031 return 0;
1034 tmp.ullval = static_cast<ACE_CDR::ULongLong> (ev->u.fval);
1035 break;
1036 case AST_Expression::EV_double:
1037 if (ev->u.dval < 0.0
1038 || ev->u.dval > (ACE_CDR::Double) ACE_UINT64_MAX)
1040 return 0;
1043 tmp.ullval = static_cast<ACE_CDR::ULongLong> (ev->u.dval);
1044 break;
1045 case AST_Expression::EV_char:
1046 if ((signed char) ev->u.cval < 0)
1048 return 0;
1051 tmp.ullval = ev->u.cval;
1052 break;
1053 case AST_Expression::EV_wchar:
1054 tmp.ullval = ev->u.wcval;
1055 break;
1056 case AST_Expression::EV_octet:
1057 tmp.ullval = ev->u.oval;
1058 break;
1059 case AST_Expression::EV_int8:
1060 if (ev->u.int8val < 0) return 0;
1061 tmp.ullval = static_cast<ACE_CDR::ULongLong> (ev->u.int8val);
1062 break;
1063 case AST_Expression::EV_uint8:
1064 tmp.ullval = static_cast<ACE_CDR::ULongLong> (ev->u.uint8val);
1065 break;
1066 default:
1067 return 0;
1069 break;
1071 case AST_Expression::EV_bool:
1072 switch (ev->et)
1074 case AST_Expression::EV_short:
1075 tmp.bval = (ev->u.sval == 0) ? false : true;
1076 break;
1077 case AST_Expression::EV_ushort:
1078 tmp.bval = (ev->u.usval == 0) ? false : true;
1079 break;
1080 case AST_Expression::EV_long:
1081 tmp.bval = (ev->u.lval == 0) ? false : true;
1082 break;
1083 case AST_Expression::EV_ulong:
1084 tmp.bval = (ev->u.ulval == 0) ? false : true;
1085 break;
1086 case AST_Expression::EV_longlong:
1087 tmp.bval = (ev->u.llval == 0) ? false : true;
1088 break;
1089 case AST_Expression::EV_ulonglong:
1090 tmp.bval = (ev->u.ullval == 0) ? false : true;
1091 break;
1092 case AST_Expression::EV_float:
1093 tmp.bval = ACE::is_equal (ev->u.fval, 0.0f) ? false : true;
1094 break;
1095 case AST_Expression::EV_double:
1096 tmp.bval = ACE::is_equal (ev->u.dval, 0.0) ? false : true;
1097 break;
1098 case AST_Expression::EV_char:
1099 tmp.bval = (ev->u.cval == 0) ? false : true;
1100 break;
1101 case AST_Expression::EV_wchar:
1102 tmp.bval = (ev->u.wcval == 0) ? false : true;
1103 break;
1104 case AST_Expression::EV_octet:
1105 tmp.bval = (ev->u.oval == 0) ? false : true;
1106 break;
1107 case AST_Expression::EV_int8:
1108 tmp.bval = ev->u.int8val ? true : false;
1109 break;
1110 case AST_Expression::EV_uint8:
1111 tmp.bval = ev->u.uint8val ? true : false;
1112 break;
1113 default:
1114 return 0;
1116 break;
1118 case AST_Expression::EV_float:
1119 switch (ev->et)
1121 case AST_Expression::EV_short:
1122 tmp.fval = (ACE_CDR::Float) ev->u.sval;
1123 break;
1124 case AST_Expression::EV_ushort:
1125 tmp.fval = (ACE_CDR::Float) ev->u.usval;
1126 break;
1127 case AST_Expression::EV_long:
1128 tmp.fval = (ACE_CDR::Float) ev->u.lval;
1129 break;
1130 case AST_Expression::EV_ulong:
1131 tmp.fval = (ACE_CDR::Float) ev->u.ulval;
1132 break;
1133 case AST_Expression::EV_longlong:
1134 if (ev->u.llval > ACE_FLT_MAX
1135 || ev->u.llval < ACE_FLT_LOWEST)
1137 return 0;
1139 tmp.fval = (ACE_CDR::Float) ev->u.llval;
1140 break;
1141 case AST_Expression::EV_ulonglong:
1142 tmp.fval = (ACE_CDR::Float) ((ACE_CDR::LongLong) ev->u.ullval);
1143 break;
1144 case AST_Expression::EV_bool:
1145 tmp.fval = ev->u.bval ? 1.0f : 0.0f;
1146 break;
1147 case AST_Expression::EV_double:
1148 if (ev->u.dval > ACE_FLT_MAX
1149 || ev->u.dval < ACE_FLT_LOWEST)
1151 return 0;
1154 tmp.fval = (ACE_CDR::Float) ev->u.dval;
1155 break;
1156 case AST_Expression::EV_char:
1157 tmp.fval = (ACE_CDR::Float) ev->u.cval;
1158 break;
1159 case AST_Expression::EV_wchar:
1160 tmp.fval = (ACE_CDR::Float) ev->u.wcval;
1161 break;
1162 case AST_Expression::EV_octet:
1163 tmp.fval = (ACE_CDR::Float) ev->u.oval;
1164 break;
1165 case AST_Expression::EV_int8:
1166 tmp.fval = static_cast<ACE_CDR::Float> (ev->u.int8val);
1167 break;
1168 case AST_Expression::EV_uint8:
1169 tmp.fval = static_cast<ACE_CDR::Float> (ev->u.uint8val);
1170 break;
1171 default:
1172 return 0;
1174 break;
1176 case AST_Expression::EV_double:
1177 switch (ev->et)
1179 case AST_Expression::EV_short:
1180 tmp.dval = (ACE_CDR::Double) ev->u.sval;
1181 break;
1182 case AST_Expression::EV_ushort:
1183 tmp.dval = (ACE_CDR::Double) ev->u.usval;
1184 break;
1185 case AST_Expression::EV_long:
1186 tmp.dval = (ACE_CDR::Double) ev->u.lval;
1187 break;
1188 case AST_Expression::EV_ulong:
1189 tmp.dval = (ACE_CDR::Double) ev->u.ulval;
1190 break;
1191 case AST_Expression::EV_longlong:
1192 tmp.dval = (ACE_CDR::Double) ev->u.llval;
1193 break;
1194 case AST_Expression::EV_ulonglong:
1195 // Some compilers don't implement unsigned 64-bit to double
1196 // conversions, so we are stuck with the signed 64-bit max value.
1197 if (ev->u.ullval > ACE_INT64_MAX)
1199 return 0;
1202 tmp.dval = (ACE_CDR::Double) ((ACE_CDR::LongLong) ev->u.ullval);
1203 break;
1204 case AST_Expression::EV_bool:
1205 tmp.dval = ev->u.bval ? 1.0 : 0.0;
1206 break;
1207 case AST_Expression::EV_float:
1208 tmp.dval = (ACE_CDR::Double) ev->u.fval;
1209 break;
1210 case AST_Expression::EV_char:
1211 tmp.dval = (ACE_CDR::Double) ev->u.cval;
1212 break;
1213 case AST_Expression::EV_wchar:
1214 tmp.dval = (ACE_CDR::Double) ev->u.wcval;
1215 break;
1216 case AST_Expression::EV_octet:
1217 tmp.dval = (ACE_CDR::Double) ev->u.oval;
1218 break;
1219 case AST_Expression::EV_int8:
1220 tmp.dval = static_cast<ACE_CDR::Double> (ev->u.int8val);
1221 break;
1222 case AST_Expression::EV_uint8:
1223 tmp.dval = static_cast<ACE_CDR::Double> (ev->u.uint8val);
1224 break;
1225 default:
1226 return 0;
1228 break;
1230 case AST_Expression::EV_int8:
1231 case AST_Expression::EV_char:
1232 switch (ev->et)
1234 case AST_Expression::EV_short:
1235 if (ev->u.sval > (ACE_CDR::Short) ACE_CHAR_MAX
1236 || ev->u.sval < (ACE_CDR::Short) ACE_CHAR_MIN)
1238 return 0;
1241 tmp.cval = (ACE_CDR::Char) ev->u.sval;
1242 break;
1243 case AST_Expression::EV_ushort:
1244 if (ev->u.usval > (ACE_CDR::UShort) ACE_CHAR_MAX)
1246 return 0;
1249 tmp.cval = (ACE_CDR::Char) ev->u.usval;
1250 break;
1251 case AST_Expression::EV_long:
1252 if (ev->u.lval > (ACE_CDR::Long) ACE_CHAR_MAX
1253 || ev->u.lval < (ACE_CDR::Long) ACE_CHAR_MIN)
1255 return 0;
1258 tmp.cval = (ACE_CDR::Char) ev->u.lval;
1259 break;
1260 case AST_Expression::EV_ulong:
1261 if (ev->u.ulval > (ACE_CDR::ULong) ACE_CHAR_MAX)
1263 return 0;
1266 tmp.cval = (ACE_CDR::Char) ev->u.ulval;
1267 break;
1268 case AST_Expression::EV_longlong:
1269 if (ev->u.llval > (ACE_CDR::LongLong) ACE_CHAR_MAX
1270 || ev->u.llval < (ACE_CDR::LongLong) ACE_CHAR_MIN)
1272 return 0;
1275 tmp.cval = (ACE_CDR::Char) ev->u.llval;
1276 break;
1277 case AST_Expression::EV_ulonglong:
1278 if ((ev->u.ullval & ACE_CHAR_MAX) != ev->u.ullval)
1280 return 0;
1283 tmp.cval = (ACE_CDR::Char) ev->u.ullval;
1284 break;
1285 case AST_Expression::EV_bool:
1286 tmp.cval = (ACE_CDR::Char) ev->u.bval;
1287 break;
1288 case AST_Expression::EV_float:
1289 if (ev->u.fval > (ACE_CDR::Float) ACE_CHAR_MAX
1290 || ev->u.fval < (ACE_CDR::Float) ACE_CHAR_MIN)
1292 return 0;
1295 tmp.cval = (ACE_CDR::Char) ev->u.fval;
1296 break;
1297 case AST_Expression::EV_double:
1298 if (ev->u.dval > (ACE_CDR::Double) ACE_CHAR_MAX
1299 || ev->u.dval < (ACE_CDR::Double) ACE_CHAR_MIN)
1301 return 0;
1304 tmp.cval = (ACE_CDR::Char) ev->u.dval;
1305 break;
1306 case AST_Expression::EV_wchar:
1307 if (ev->u.wcval > (ACE_CDR::WChar) ACE_CHAR_MAX)
1309 return 0;
1312 tmp.cval = (ACE_CDR::Char) ev->u.wcval;
1313 break;
1314 case AST_Expression::EV_uint8:
1315 case AST_Expression::EV_octet:
1316 if (ev->u.oval > (unsigned char) ACE_CHAR_MAX)
1318 return 0;
1321 tmp.cval = (ACE_CDR::Char) ev->u.oval;
1322 break;
1323 case AST_Expression::EV_int8:
1324 case AST_Expression::EV_char:
1325 tmp = ev->u;
1326 break;
1327 default:
1328 return 0;
1330 break;
1332 case AST_Expression::EV_wchar:
1333 switch (ev->et)
1335 case AST_Expression::EV_short:
1336 if (ev->u.sval < 0)
1338 return 0;
1341 tmp.wcval = (ACE_CDR::WChar) ev->u.sval;
1342 break;
1343 case AST_Expression::EV_ushort:
1344 tmp.wcval = (ACE_CDR::WChar) ev->u.usval;
1345 break;
1346 case AST_Expression::EV_long:
1347 if (ev->u.lval < 0
1348 || ev->u.lval > ACE_WCHAR_MAX)
1350 return 0;
1353 tmp.wcval = (ACE_CDR::WChar) ev->u.lval;
1354 break;
1355 case AST_Expression::EV_ulong:
1356 if (ev->u.ulval > ACE_WCHAR_MAX)
1358 return 0;
1361 tmp.wcval = (ACE_CDR::WChar) ev->u.ulval;
1362 break;
1363 case AST_Expression::EV_longlong:
1364 if (ev->u.llval > (ACE_CDR::LongLong) ACE_WCHAR_MAX
1365 || ev->u.llval < 0)
1367 return 0;
1370 tmp.wcval = (ACE_CDR::WChar) ev->u.llval;
1371 break;
1372 case AST_Expression::EV_ulonglong:
1373 if ((ev->u.ullval & ACE_WCHAR_MAX) != ev->u.ullval)
1375 return 0;
1378 tmp.wcval = (ACE_CDR::WChar) ev->u.ullval;
1379 break;
1380 case AST_Expression::EV_bool:
1381 tmp.wcval = (ACE_CDR::WChar) ev->u.bval;
1382 break;
1383 case AST_Expression::EV_float:
1384 if (ev->u.fval > (ACE_CDR::Float) ACE_WCHAR_MAX
1385 || ev->u.fval < 0)
1387 return 0;
1390 tmp.wcval = (ACE_CDR::WChar) ev->u.fval;
1391 break;
1392 case AST_Expression::EV_double:
1393 if (ev->u.dval > (ACE_CDR::Double) ACE_WCHAR_MAX
1394 || ev->u.dval < 0)
1396 return 0;
1399 tmp.wcval = (ACE_CDR::WChar) ev->u.dval;
1400 break;
1401 case AST_Expression::EV_char:
1402 case AST_Expression::EV_int8:
1403 if ((signed char) ev->u.cval < 0)
1405 return 0;
1408 tmp.wcval = (ACE_CDR::WChar) ev->u.cval;
1409 break;
1410 case AST_Expression::EV_octet:
1411 tmp.wcval = (ACE_CDR::WChar) ev->u.oval;
1412 break;
1413 case AST_Expression::EV_uint8:
1414 tmp.wcval = static_cast<ACE_CDR::WChar> (ev->u.uint8val);
1415 break;
1416 default:
1417 return 0;
1419 break;
1421 case AST_Expression::EV_uint8:
1422 case AST_Expression::EV_octet:
1423 switch (ev->et)
1425 case AST_Expression::EV_short:
1426 if (ev->u.sval < 0
1427 || ev->u.sval > (ACE_CDR::Short) ACE_OCTET_MAX)
1429 return 0;
1432 tmp.oval = (ACE_CDR::Octet) ev->u.sval;
1433 break;
1434 case AST_Expression::EV_ushort:
1435 if (ev->u.usval > (ACE_CDR::UShort) ACE_OCTET_MAX)
1437 return 0;
1440 tmp.oval = (ACE_CDR::Octet) ev->u.usval;
1441 break;
1442 case AST_Expression::EV_long:
1443 if (ev->u.lval < 0
1444 || ev->u.lval > (ACE_CDR::Long) ACE_OCTET_MAX)
1446 return 0;
1449 tmp.oval = (ACE_CDR::Octet) ev->u.lval;
1450 break;
1451 case AST_Expression::EV_ulong:
1452 if (ev->u.ulval > (ACE_CDR::ULong) ACE_OCTET_MAX)
1454 return 0;
1457 tmp.oval = (ACE_CDR::Octet) ev->u.ulval;
1458 break;
1459 case AST_Expression::EV_longlong:
1460 if (ev->u.llval < 0
1461 || ev->u.llval > (ACE_CDR::LongLong) ACE_OCTET_MAX)
1463 return 0;
1466 tmp.oval = (ACE_CDR::Octet) ev->u.llval;
1467 break;
1468 case AST_Expression::EV_ulonglong:
1469 if ((ev->u.ullval & ACE_OCTET_MAX) != ev->u.ullval)
1471 return 0;
1474 tmp.oval = (ACE_CDR::Octet) ev->u.ullval;
1475 break;
1476 case AST_Expression::EV_bool:
1477 tmp.oval = (ACE_CDR::Octet) ev->u.bval ? 1 : 0;
1478 break;
1479 case AST_Expression::EV_float:
1480 if (ev->u.fval < 0.0
1481 || ev->u.fval > (ACE_CDR::Float) ACE_OCTET_MAX)
1483 return 0;
1486 tmp.oval = (ACE_CDR::Octet) ev->u.fval;
1487 break;
1488 case AST_Expression::EV_double:
1489 if (ev->u.dval < 0.0
1490 || ev->u.dval > (ACE_CDR::Double) ACE_OCTET_MAX)
1492 return 0;
1495 tmp.oval = (ACE_CDR::Octet) ev->u.dval;
1496 break;
1497 case AST_Expression::EV_int8:
1498 case AST_Expression::EV_char:
1499 if ((signed char) ev->u.cval < 0)
1501 return 0;
1504 tmp.oval = (ACE_CDR::Octet) ev->u.cval;
1505 break;
1506 case AST_Expression::EV_wchar:
1507 if (ev->u.wcval > (ACE_CDR::WChar) ACE_OCTET_MAX)
1509 return 0;
1512 tmp.oval = (ACE_CDR::Octet) ev->u.wcval;
1513 break;
1514 case AST_Expression::EV_uint8:
1515 case AST_Expression::EV_octet:
1516 tmp = ev->u;
1517 break;
1518 default:
1519 return 0;
1521 break;
1523 case AST_Expression::EV_enum:
1524 switch (ev->et)
1526 case AST_Expression::EV_ulong:
1527 return ev;
1528 default:
1529 return 0;
1531 default:
1532 return 0;
1535 ev->et = t;
1536 ev->u = tmp;
1537 return ev;
1540 // Integer literals may not be assigned to floating point constants,
1541 // and vice versa.
1542 static bool
1543 incompatible_types (AST_Expression::ExprType t1,
1544 AST_Expression::ExprType t2)
1546 switch (t1)
1548 case AST_Expression::EV_short:
1549 case AST_Expression::EV_ushort:
1550 case AST_Expression::EV_long:
1551 case AST_Expression::EV_ulong:
1552 case AST_Expression::EV_longlong:
1553 case AST_Expression::EV_ulonglong:
1554 case AST_Expression::EV_octet:
1555 case AST_Expression::EV_bool:
1556 case AST_Expression::EV_int8:
1557 case AST_Expression::EV_uint8:
1558 switch (t2)
1560 case AST_Expression::EV_short:
1561 case AST_Expression::EV_ushort:
1562 case AST_Expression::EV_long:
1563 case AST_Expression::EV_ulong:
1564 case AST_Expression::EV_longlong:
1565 case AST_Expression::EV_ulonglong:
1566 case AST_Expression::EV_octet:
1567 case AST_Expression::EV_bool:
1568 case AST_Expression::EV_int8:
1569 case AST_Expression::EV_uint8:
1570 return false;
1571 default:
1572 return true;
1574 case AST_Expression::EV_float:
1575 case AST_Expression::EV_double:
1576 case AST_Expression::EV_longdouble:
1577 switch (t2)
1579 case AST_Expression::EV_float:
1580 case AST_Expression::EV_double:
1581 case AST_Expression::EV_longdouble:
1582 return false;
1583 default:
1584 return true;
1586 case AST_Expression::EV_fixed:
1587 return t2 != AST_Expression::EV_fixed;
1588 default:
1589 return false;
1593 // Evaluate the expression wrt the evaluation kind requested. Supported
1594 // evaluation kinds are
1595 // - EK_const: The expression must evaluate to a constant
1596 // - EK_positive_int: The expression must further evaluate to a
1597 // positive integer
1599 // @@(JP) This just maps one enum to another. It's a temporary fix,
1600 // but AST_Expression::EvalKind should go eventually.
1601 static AST_Expression::AST_ExprValue *
1602 eval_kind (AST_Expression::AST_ExprValue *ev, AST_Expression::EvalKind ek)
1604 // Make a copy to simplify the memory management logic.
1605 AST_Expression::AST_ExprValue *newval = 0;
1606 ACE_NEW_RETURN (newval,
1607 AST_Expression::AST_ExprValue,
1610 if (ev != 0)
1612 *newval = *ev;
1615 AST_Expression::AST_ExprValue *retval = 0;
1617 switch (ek)
1619 case AST_Expression::EK_const:
1620 retval = newval;
1621 break;
1622 case AST_Expression::EK_positive_int:
1623 retval = coerce_value (newval, AST_Expression::EV_ulong);
1624 break;
1625 case AST_Expression::EK_short:
1626 retval = coerce_value (newval, AST_Expression::EV_short);
1627 break;
1628 case AST_Expression::EK_ushort:
1629 retval = coerce_value (newval, AST_Expression::EV_ushort);
1630 break;
1631 case AST_Expression::EK_long:
1632 retval = coerce_value (newval, AST_Expression::EV_long);
1633 break;
1634 case AST_Expression::EK_ulong:
1635 retval = coerce_value (newval, AST_Expression::EV_ulong);
1636 break;
1637 case AST_Expression::EK_longlong:
1638 retval = coerce_value (newval, AST_Expression::EV_longlong);
1639 break;
1640 case AST_Expression::EK_ulonglong:
1641 retval = coerce_value (newval, AST_Expression::EV_ulonglong);
1642 break;
1643 case AST_Expression::EK_octet:
1644 retval = coerce_value (newval, AST_Expression::EV_octet);
1645 break;
1646 case AST_Expression::EK_bool:
1647 retval = coerce_value (newval, AST_Expression::EV_bool);
1648 break;
1649 case AST_Expression::EK_fixed_point:
1650 retval = coerce_value (newval, AST_Expression::EV_fixed);
1651 break;
1652 case AST_Expression::EK_int8:
1653 retval = coerce_value (newval, AST_Expression::EV_int8);
1654 break;
1655 case AST_Expression::EK_uint8:
1656 retval = coerce_value (newval, AST_Expression::EV_uint8);
1657 break;
1658 case AST_Expression::EK_floating_point:
1659 retval = coerce_value (newval, AST_Expression::EV_double);
1660 break;
1663 // Sometimes the call above to coerce_value() will return an
1664 // evaluated newval, other times 0. But a heap-allocated
1665 // ExprValue is not always passed to coerce_value(), so we
1666 // have to manage it here, where we know it is always a 'new'.
1667 if (retval != newval)
1669 delete newval;
1670 newval = 0;
1673 return retval;
1676 // Private operations.
1678 // @@@ (JP) CORBA 2.6 and earlier say that in a constant expression,
1679 // each subexpression must fall within the range of the assigned type.
1680 // However, this may be hard for the compiler in some cases (must
1681 // evaluate all grouping possibilities). So there is an outstanding
1682 // issue, #1139, and the best guess is that it will ultimately be
1683 // decided that only the final value must fall within the range of
1684 // the assigned type. So there are no checks here, only in coerce().
1686 // Apply binary operators to an AST_Expression after evaluating
1687 // its sub-expressions.
1688 // Operations supported: '+', '-', '*', '/'
1689 template <typename Type>
1690 bool
1691 do_eval_bin_op (AST_Expression::ExprComb op, Type a, Type b, Type &result)
1693 switch (op)
1695 case AST_Expression::EC_add:
1696 result = a + b;
1697 break;
1698 case AST_Expression::EC_minus:
1699 result = a - b;
1700 break;
1701 case AST_Expression::EC_mul:
1702 result = a * b;
1703 break;
1704 case AST_Expression::EC_div:
1705 if (!b) return false;
1706 result = a / b;
1707 break;
1708 default:
1709 return false;
1712 return true;
1715 template <typename Type>
1716 bool
1717 do_eval_bin_op_float (AST_Expression::ExprComb op, Type a, Type b, Type &result)
1719 if (op == AST_Expression::EC_div)
1721 result = a / b;
1722 return true;
1724 return do_eval_bin_op (op, a, b, result);
1727 AST_Expression::AST_ExprValue *
1728 AST_Expression::eval_bin_op (AST_Expression::EvalKind ek)
1730 AST_ExprValue *retval = 0;
1732 if (this->pd_v1 == 0 || this->pd_v2 == 0)
1734 return 0;
1737 this->pd_v1->set_ev (this->pd_v1->eval_internal (ek));
1738 this->pd_v2->set_ev (this->pd_v2->eval_internal (ek));
1740 if (this->pd_v1->ev () == 0 || this->pd_v2->ev () == 0)
1742 return 0;
1745 ExprType const expr_type = ek == EK_const ?
1746 pd_v1->ev ()->et : eval_kind_to_expr_type (ek);
1747 if (expr_type == EV_none) return 0;
1749 ACE_NEW_RETURN (retval,
1750 AST_ExprValue,
1753 pd_v1->set_ev (pd_v1->coerce (expr_type));
1754 pd_v2->set_ev (pd_v2->coerce (expr_type));
1755 retval->et = expr_type;
1757 bool success = false;
1758 switch (expr_type)
1760 case EV_int8:
1761 success = do_eval_bin_op<ACE_CDR::Int8> (pd_ec,
1762 pd_v1->ev ()->u.int8val, pd_v2->ev ()->u.int8val, retval->u.int8val);
1763 break;
1765 case EV_uint8:
1766 success = do_eval_bin_op<ACE_CDR::UInt8> (pd_ec,
1767 pd_v1->ev ()->u.uint8val, pd_v2->ev ()->u.uint8val, retval->u.uint8val);
1768 break;
1770 case EV_short:
1771 success = do_eval_bin_op<ACE_CDR::Short> (pd_ec,
1772 pd_v1->ev ()->u.sval, pd_v2->ev ()->u.sval, retval->u.sval);
1773 break;
1775 case EV_ushort:
1776 success = do_eval_bin_op<ACE_CDR::UShort> (pd_ec,
1777 pd_v1->ev ()->u.usval, pd_v2->ev ()->u.usval, retval->u.usval);
1778 break;
1780 case EV_long:
1781 success = do_eval_bin_op<ACE_CDR::Long> (pd_ec,
1782 pd_v1->ev ()->u.lval, pd_v2->ev ()->u.lval, retval->u.lval);
1783 break;
1785 case EV_ulong:
1786 success = do_eval_bin_op<ACE_CDR::ULong> (pd_ec,
1787 pd_v1->ev ()->u.ulval, pd_v2->ev ()->u.ulval, retval->u.ulval);
1788 break;
1790 case EV_longlong:
1791 success = do_eval_bin_op<ACE_CDR::LongLong> (pd_ec,
1792 pd_v1->ev ()->u.llval, pd_v2->ev ()->u.llval, retval->u.llval);
1793 break;
1795 case EV_ulonglong:
1796 success = do_eval_bin_op<ACE_CDR::ULongLong> (pd_ec,
1797 pd_v1->ev ()->u.ullval, pd_v2->ev ()->u.ullval, retval->u.ullval);
1798 break;
1800 case EV_octet:
1801 success = do_eval_bin_op<ACE_CDR::Octet> (pd_ec,
1802 pd_v1->ev ()->u.oval, pd_v2->ev ()->u.oval, retval->u.oval);
1803 break;
1805 case EV_double:
1806 success = do_eval_bin_op_float<ACE_CDR::Double> (pd_ec,
1807 pd_v1->ev ()->u.dval, pd_v2->ev ()->u.dval, retval->u.dval);
1808 break;
1810 case EV_fixed:
1811 success = do_eval_bin_op<ACE_CDR::Fixed> (pd_ec,
1812 pd_v1->ev ()->u.fixedval, pd_v2->ev ()->u.fixedval, retval->u.fixedval);
1813 break;
1815 default:
1816 break;
1819 if (!success)
1821 delete retval;
1822 retval = 0;
1825 return retval;
1828 template <typename Type>
1829 bool
1830 do_eval_mod_op (Type a, Type b, Type &result)
1832 if (b == 0) {
1833 return false;
1835 result = a % b;
1836 return true;
1839 // Apply binary operators to an AST_Expression after evaluating
1840 // its sub-expressions.
1841 // Operations supported: '%'
1842 AST_Expression::AST_ExprValue *
1843 AST_Expression::eval_mod_op (AST_Expression::EvalKind ek)
1845 AST_ExprValue *retval = 0;
1847 if (this->pd_v1 == 0 || this->pd_v2 == 0)
1849 return 0;
1852 this->pd_v1->set_ev (this->pd_v1->eval_internal (ek));
1853 this->pd_v2->set_ev (this->pd_v2->eval_internal (ek));
1855 if (this->pd_v1->ev () == 0 || this->pd_v2->ev () == 0)
1857 return 0;
1860 ExprType const expr_type = ek == EK_const ?
1861 pd_v1->ev ()->et : eval_kind_to_expr_type (ek);
1862 if (expr_type == EV_none) return 0;
1864 ACE_NEW_RETURN (retval,
1865 AST_ExprValue,
1868 pd_v1->set_ev (pd_v1->coerce (expr_type));
1869 pd_v2->set_ev (pd_v2->coerce (expr_type));
1870 retval->et = expr_type;
1872 bool success = false;
1873 switch (expr_type)
1875 case EV_int8:
1876 success = do_eval_mod_op<ACE_CDR::Int8> (
1877 pd_v1->ev ()->u.int8val, pd_v2->ev ()->u.int8val, retval->u.int8val);
1878 break;
1880 case EV_uint8:
1881 success = do_eval_mod_op<ACE_CDR::UInt8> (
1882 pd_v1->ev ()->u.uint8val, pd_v2->ev ()->u.uint8val, retval->u.uint8val);
1883 break;
1885 case EV_short:
1886 success = do_eval_mod_op<ACE_CDR::Short> (
1887 pd_v1->ev ()->u.sval, pd_v2->ev ()->u.sval, retval->u.sval);
1888 break;
1890 case EV_ushort:
1891 success = do_eval_mod_op<ACE_CDR::UShort> (
1892 pd_v1->ev ()->u.usval, pd_v2->ev ()->u.usval, retval->u.usval);
1893 break;
1895 case EV_long:
1896 success = do_eval_mod_op<ACE_CDR::Long> (
1897 pd_v1->ev ()->u.lval, pd_v2->ev ()->u.lval, retval->u.lval);
1898 break;
1900 case EV_ulong:
1901 success = do_eval_mod_op<ACE_CDR::ULong> (
1902 pd_v1->ev ()->u.ulval, pd_v2->ev ()->u.ulval, retval->u.ulval);
1903 break;
1905 case EV_longlong:
1906 success = do_eval_mod_op<ACE_CDR::LongLong> (
1907 pd_v1->ev ()->u.llval, pd_v2->ev ()->u.llval, retval->u.llval);
1908 break;
1910 case EV_ulonglong:
1911 success = do_eval_mod_op<ACE_CDR::ULongLong> (
1912 pd_v1->ev ()->u.ullval, pd_v2->ev ()->u.ullval, retval->u.ullval);
1913 break;
1915 case EV_octet:
1916 success = do_eval_mod_op<ACE_CDR::Octet> (
1917 pd_v1->ev ()->u.oval, pd_v2->ev ()->u.oval, retval->u.oval);
1918 break;
1920 default:
1921 break;
1924 if (!success)
1926 delete retval;
1927 retval = 0;
1930 return retval;
1933 // Apply bitwise operations to an AST_Expression after evaluating
1934 // its sub-expressions.
1935 // Operations supported: '%', '|', '&', '^', '<<', '>>'
1937 template <typename Type>
1938 bool
1939 do_eval_bit_op_no_shift (AST_Expression::ExprComb op, Type a, Type b, Type &result)
1941 switch (op)
1943 case AST_Expression::EC_or:
1944 result = a | b;
1945 break;
1946 case AST_Expression::EC_xor:
1947 result = a ^ b;
1948 break;
1949 case AST_Expression::EC_and:
1950 result = a & b;
1951 break;
1952 default:
1953 return false;
1956 return true;
1959 template <typename Type>
1960 bool
1961 do_eval_bit_op (AST_Expression::ExprComb op, Type a, Type b, Type &result)
1963 switch (op)
1965 case AST_Expression::EC_left:
1966 result = a << b;
1967 break;
1968 case AST_Expression::EC_right:
1969 result = a >> b;
1970 break;
1971 default:
1972 return do_eval_bit_op_no_shift (op, a, b, result);
1975 return true;
1978 AST_Expression::AST_ExprValue *
1979 AST_Expression::eval_bit_op (AST_Expression::EvalKind ek)
1981 AST_Expression::AST_ExprValue *retval = 0;
1983 if (this->pd_v1 == 0 || this->pd_v2 == 0)
1985 return 0;
1988 this->pd_v1->set_ev (this->pd_v1->eval_internal (ek));
1989 this->pd_v2->set_ev (this->pd_v2->eval_internal (ek));
1991 if (this->pd_v1->ev () == 0 || this->pd_v2->ev () == 0)
1993 return 0;
1996 ExprType const expr_type = eval_kind_to_expr_type (ek);
1997 if (expr_type == EV_none) return 0;
1999 ACE_NEW_RETURN (retval,
2000 AST_ExprValue,
2003 pd_v1->set_ev (pd_v1->coerce (expr_type));
2004 pd_v2->set_ev (pd_v2->coerce (expr_type));
2005 retval->et = expr_type;
2007 bool success = false;
2008 switch (expr_type)
2010 case EV_int8:
2011 success = do_eval_bit_op<ACE_CDR::Int8> (pd_ec,
2012 pd_v1->ev ()->u.int8val, pd_v2->ev ()->u.int8val, retval->u.int8val);
2013 break;
2015 case EV_uint8:
2016 success = do_eval_bit_op<ACE_CDR::UInt8> (pd_ec,
2017 pd_v1->ev ()->u.uint8val, pd_v2->ev ()->u.uint8val, retval->u.uint8val);
2018 break;
2020 case EV_short:
2021 success = do_eval_bit_op<ACE_CDR::Short> (pd_ec,
2022 pd_v1->ev ()->u.sval, pd_v2->ev ()->u.sval, retval->u.sval);
2023 break;
2025 case EV_ushort:
2026 success = do_eval_bit_op<ACE_CDR::UShort> (pd_ec,
2027 pd_v1->ev ()->u.usval, pd_v2->ev ()->u.usval, retval->u.usval);
2028 break;
2030 case EV_long:
2031 success = do_eval_bit_op<ACE_CDR::Long> (pd_ec,
2032 pd_v1->ev ()->u.lval, pd_v2->ev ()->u.lval, retval->u.lval);
2033 break;
2035 case EV_ulong:
2036 success = do_eval_bit_op<ACE_CDR::ULong> (pd_ec,
2037 pd_v1->ev ()->u.ulval, pd_v2->ev ()->u.ulval, retval->u.ulval);
2038 break;
2040 case EV_longlong:
2041 success = do_eval_bit_op<ACE_CDR::LongLong> (pd_ec,
2042 pd_v1->ev ()->u.llval, pd_v2->ev ()->u.llval, retval->u.llval);
2043 break;
2045 case EV_ulonglong:
2046 success = do_eval_bit_op<ACE_CDR::ULongLong> (pd_ec,
2047 pd_v1->ev ()->u.ullval, pd_v2->ev ()->u.ullval, retval->u.ullval);
2048 break;
2050 case EV_octet:
2051 success = do_eval_bit_op<ACE_CDR::Octet> (pd_ec,
2052 pd_v1->ev ()->u.oval, pd_v2->ev ()->u.oval, retval->u.oval);
2053 break;
2055 case EV_bool:
2056 success = do_eval_bit_op_no_shift<ACE_CDR::Boolean> (pd_ec,
2057 pd_v1->ev ()->u.bval, pd_v2->ev ()->u.bval, retval->u.bval);
2058 break;
2060 default:
2061 break;
2064 if (!success)
2066 delete retval;
2067 retval = 0;
2070 return retval;
2073 // Apply unary operators to an AST_Expression after evaluating its
2074 // sub-expression.
2075 // Operations supported: '-', '+', '~'
2076 AST_Expression::AST_ExprValue *
2077 AST_Expression::eval_un_op (AST_Expression::EvalKind ek)
2079 AST_ExprValue *retval = 0;
2081 if (this->pd_ev != 0)
2083 return this->pd_ev;
2086 if (this->pd_v1 == 0)
2088 return 0;
2091 this->pd_v1->set_ev (this->pd_v1->eval_internal (ek));
2093 if (this->pd_v1->ev () == 0)
2095 return 0;
2098 ACE_NEW_RETURN (retval,
2099 AST_ExprValue,
2102 if (this->pd_v1->ev ()->et == EV_fixed)
2104 retval->et = EV_fixed;
2105 switch (this->pd_ec)
2107 case EC_u_plus:
2108 retval->u.fixedval = this->pd_v1->ev ()->u.fixedval;
2109 break;
2110 case EC_u_minus:
2111 retval->u.fixedval = -this->pd_v1->ev ()->u.fixedval;
2112 break;
2113 default:
2114 delete retval;
2115 return 0;
2117 return retval;
2120 retval->et = EV_double;
2122 switch (this->pd_ec)
2124 case EC_u_plus:
2125 this->pd_v1->set_ev (this->pd_v1->coerce (EV_double));
2127 if (this->pd_v1->ev () == 0)
2129 delete retval;
2130 retval = 0;
2131 return 0;
2134 retval->u.dval = this->pd_v1->ev ()->u.dval;
2135 break;
2136 case EC_u_minus:
2137 this->pd_v1->set_ev (this->pd_v1->coerce (EV_double));
2139 if (this->pd_v1->ev () == 0)
2141 delete retval;
2142 retval = 0;
2143 return 0;
2146 retval->u.dval = -(this->pd_v1->ev ()->u.dval);
2147 break;
2148 case EC_bit_neg:
2149 if (this->pd_v1->ev () == 0)
2151 delete retval;
2152 retval = 0;
2153 return 0;
2156 switch (this->pd_v1->ev ()->et)
2158 case EV_short:
2159 retval->u.sval = ~this->pd_v1->ev ()->u.sval;
2160 break;
2161 case EV_ushort:
2162 retval->u.usval = ~this->pd_v1->ev ()->u.usval;
2163 break;
2164 case EV_long:
2165 retval->u.lval = ~this->pd_v1->ev ()->u.lval;
2166 break;
2167 case EV_ulong:
2168 retval->u.ulval = ~this->pd_v1->ev ()->u.ulval;
2169 break;
2170 case EV_longlong:
2171 retval->u.llval = ~this->pd_v1->ev ()->u.llval;
2172 break;
2173 case EV_ulonglong:
2174 retval->u.ullval = ~this->pd_v1->ev ()->u.ullval;
2175 break;
2176 case EV_octet:
2177 retval->u.oval = ~this->pd_v1->ev ()->u.oval;
2178 break;
2179 case EV_int8:
2180 retval->u.int8val = ~pd_v1->ev ()->u.int8val;
2181 break;
2182 case EV_uint8:
2183 retval->u.uint8val = ~pd_v1->ev ()->u.uint8val;
2184 break;
2185 default:
2186 delete retval;
2187 retval = 0;
2188 return 0;
2190 retval->et = pd_v1->ev ()->et;
2192 break;
2193 default:
2194 delete retval;
2195 retval = 0;
2196 return 0;
2199 return retval;
2202 // Evaluate a symbolic AST_Expression by looking up the named
2203 // symbol.
2204 AST_Expression::AST_ExprValue *
2205 AST_Expression::eval_symbol (AST_Expression::EvalKind ek)
2207 UTL_Scope *s = 0;
2208 AST_Decl *d = 0;
2209 AST_Constant *c = 0;
2211 // Is there a symbol stored?
2212 if (this->pd_n == 0)
2214 idl_global->err ()->eval_error (this);
2215 return 0;
2218 // Get current scope for lookup.
2219 if (idl_global->scopes ().depth () > 0)
2221 s = idl_global->scopes ().top_non_null ();
2224 if (s == 0)
2226 idl_global->err ()->lookup_error (this->pd_n);
2227 return 0;
2230 // If we are a template parameter placeholder, just skip the
2231 // rest - nothing needs to be evaluated until instantiation
2232 // time.
2233 if (this->param_holder_ != 0)
2235 return 0;
2238 // Do lookup.
2239 d = s->lookup_by_name (this->pd_n,
2240 true);
2242 if (d == 0)
2244 idl_global->err ()->lookup_error (this->pd_n);
2245 return 0;
2248 // Is it a constant?
2249 if (d->node_type () != AST_Decl::NT_const
2250 && d->node_type () != AST_Decl::NT_enum_val)
2252 idl_global->err ()->constant_expected (this->pd_n,
2254 return 0;
2257 if (d->node_type () == AST_Decl::NT_enum_val)
2259 AST_EnumVal *enumval = dynamic_cast<AST_EnumVal*> (d);
2260 enum_parent (enumval->enum_parent ());
2263 // OK, now evaluate the constant we just got, to produce its value.
2264 c = dynamic_cast<AST_Constant*> (d);
2266 if (c == 0)
2268 return 0;
2271 return c->constant_value ()->eval_internal (ek);
2274 bool
2275 AST_Expression::type_mismatch (AST_Expression::ExprType t)
2277 if (this->pd_ev != 0)
2279 return incompatible_types (this->pd_ev->et, t);
2282 bool v1_mismatch = 0;
2283 bool v2_mismatch = 0;
2285 if (this->pd_v1 != 0)
2287 v1_mismatch = this->pd_v1->type_mismatch (t);
2290 if (this->pd_v2 != 0)
2292 v2_mismatch = this->pd_v2->type_mismatch (t);
2295 return v1_mismatch | v2_mismatch;
2298 // Coerce "this" to the ExprType required. Returns a copy of the
2299 // original ExprValue with the coercion applied, if successful, or
2300 // 0 if failed.
2301 AST_Expression::AST_ExprValue *
2302 AST_Expression::check_and_coerce (AST_Expression::ExprType t,
2303 AST_Decl *d)
2305 if (d != 0)
2307 AST_Decl *enum_val =
2308 idl_global->scopes ().top_non_null ()->lookup_by_name (this->pd_n,
2311 if (enum_val != 0)
2313 AST_Decl *enum_decl = ScopeAsDecl (enum_val->defined_in ());
2315 if (d->node_type () == AST_Decl::NT_typedef)
2317 AST_Typedef *td = dynamic_cast<AST_Typedef*> (d);
2318 d = td->primitive_base_type ();
2321 if (d != enum_decl)
2323 idl_global->err ()->incompatible_type_error (this);
2324 return 0;
2329 if (this->type_mismatch (t))
2331 idl_global->err ()->incompatible_type_error (this);
2332 return 0;
2335 if (d != 0 && d->node_type () == AST_Decl::NT_typedef)
2337 this->tdef = d;
2340 return this->coerce (t);
2343 // Coerce "this" to the ExprType required. Returns a copy of the
2344 // original ExprValue with the coercion applied, if successful, or
2345 // 0 if failed.
2346 AST_Expression::AST_ExprValue *
2347 AST_Expression::coerce (AST_Expression::ExprType t)
2349 AST_ExprValue *tmp = 0;
2351 // First, evaluate it, then try to coerce result type.
2352 // If already evaluated, return the result.
2353 switch (t)
2355 case EV_int8:
2356 tmp = this->eval_internal (EK_int8);
2357 break;
2358 case EV_uint8:
2359 tmp = this->eval_internal (EK_uint8);
2360 break;
2361 case EV_short:
2362 tmp = this->eval_internal (EK_short);
2363 break;
2364 case EV_ushort:
2365 tmp = this->eval_internal (EK_ushort);
2366 break;
2367 case EV_long:
2368 tmp = this->eval_internal (EK_long);
2369 break;
2370 case EV_ulong:
2371 tmp = this->eval_internal (EK_ulong);
2372 break;
2373 case EV_longlong:
2374 tmp = this->eval_internal (EK_longlong);
2375 break;
2376 case EV_ulonglong:
2377 tmp = this->eval_internal (EK_ulonglong);
2378 break;
2379 case EV_octet:
2380 tmp = this->eval_internal (EK_octet);
2381 break;
2382 case EV_bool:
2383 tmp = this->eval_internal (EK_bool);
2384 break;
2385 case EV_fixed:
2386 tmp = this->eval_internal (EK_fixed_point);
2387 break;
2388 case EV_float:
2389 case EV_double:
2390 case EV_longdouble:
2391 tmp = eval_internal (EK_floating_point);
2392 break;
2393 case EV_char:
2394 case EV_wchar:
2395 case EV_string:
2396 case EV_wstring:
2397 case EV_enum:
2398 case EV_any:
2399 case EV_object:
2400 case EV_void:
2401 tmp = eval_internal (EK_const);
2402 case EV_none:
2403 break;
2406 if (tmp == 0)
2408 return 0;
2410 else
2412 delete this->pd_ev;
2413 this->pd_ev = tmp;
2416 // Create a copy to contain coercion result.
2417 AST_ExprValue *copy = 0;
2418 ACE_NEW_RETURN (copy,
2419 AST_ExprValue,
2422 copy->et = this->pd_ev->et;
2424 switch (this->pd_ev->et)
2426 case EV_longdouble:
2427 case EV_void:
2428 case EV_none:
2429 delete copy;
2430 copy = 0;
2431 return 0;
2432 case EV_enum:
2433 copy->u.ulval = this->pd_ev->u.ulval;
2434 break;
2435 case EV_short:
2436 copy->u.sval = this->pd_ev->u.sval;
2437 break;
2438 case EV_ushort:
2439 copy->u.usval = this->pd_ev->u.usval;
2440 break;
2441 case EV_long:
2442 copy->u.lval = this->pd_ev->u.lval;
2443 break;
2444 case EV_ulong:
2445 copy->u.ulval = this->pd_ev->u.ulval;
2446 break;
2447 case EV_longlong:
2448 copy->u.llval = this->pd_ev->u.llval;
2449 break;
2450 case EV_ulonglong:
2451 copy->u.ullval = this->pd_ev->u.ullval;
2452 break;
2453 case EV_bool:
2454 copy->u.bval = this->pd_ev->u.bval;
2455 break;
2456 case EV_float:
2457 copy->u.fval = this->pd_ev->u.fval;
2458 break;
2459 case EV_double:
2460 copy->u.dval = this->pd_ev->u.dval;
2461 break;
2462 case EV_char:
2463 copy->u.cval = this->pd_ev->u.cval;
2464 break;
2465 case EV_wchar:
2466 copy->u.wcval = this->pd_ev->u.wcval;
2467 break;
2468 case EV_octet:
2469 copy->u.oval = this->pd_ev->u.oval;
2470 break;
2471 case EV_string:
2472 copy->u.strval = this->pd_ev->u.strval;
2473 break;
2474 case EV_wstring:
2475 copy->u.wstrval = this->pd_ev->u.wstrval;
2476 break;
2477 case EV_fixed:
2478 copy->u.fixedval = this->pd_ev->u.fixedval;
2479 break;
2480 case EV_int8:
2481 copy->u.int8val = this->pd_ev->u.int8val;
2482 break;
2483 case EV_uint8:
2484 copy->u.uint8val = this->pd_ev->u.uint8val;
2485 break;
2486 case EV_any:
2487 case EV_object:
2488 break;
2491 if (this->pd_ev->et == t)
2493 return copy;
2495 else
2497 AST_ExprValue *ev = coerce_value (copy, t);
2499 if (ev == 0)
2501 // coerce_value() will either return 'copy' or
2502 // return 0 without freeing 'copy'. It's
2503 // deleted here instead of putting a zillion
2504 // delete statments inside coerce_value().
2505 delete copy;
2506 copy = 0;
2509 return ev;
2513 // Eval used internally.
2514 AST_Expression::AST_ExprValue *
2515 AST_Expression::eval_internal (AST_Expression::EvalKind ek)
2517 // Already evaluated?
2518 if (this->pd_ev != 0)
2520 return eval_kind (this->pd_ev,
2521 ek);
2524 // OK, must evaluate operator.
2525 switch (this->pd_ec)
2527 case EC_add:
2528 case EC_minus:
2529 case EC_mul:
2530 case EC_div:
2531 this->pd_ev = this->eval_bin_op (ek);
2532 return eval_kind (this->pd_ev,
2533 ek);
2534 case EC_mod:
2535 this->pd_ev = this->eval_mod_op (ek);
2536 return eval_kind (this->pd_ev,
2537 ek);
2538 case EC_or:
2539 case EC_xor:
2540 case EC_and:
2541 case EC_left:
2542 case EC_right:
2543 this->pd_ev = this->eval_bit_op (ek);
2544 return eval_kind (this->pd_ev,
2545 ek);
2546 case EC_u_plus:
2547 case EC_u_minus:
2548 case EC_bit_neg:
2549 this->pd_ev = this->eval_un_op (ek);
2550 return eval_kind (this->pd_ev,
2551 ek);
2552 case EC_symbol:
2553 this->pd_ev = this->eval_symbol (ek);
2554 return eval_kind (this->pd_ev,
2555 ek);
2556 case EC_none:
2557 return 0;
2560 return 0;
2563 // Public operations.
2565 // Evaluate "this", assigning the value to the pd_ev field.
2566 void
2567 AST_Expression::evaluate (EvalKind ek)
2569 AST_ExprValue *tmp = eval_kind (this->pd_ev, ek);
2570 delete this->pd_ev;
2571 this->pd_ev = tmp;
2573 // Artifact of expressions doing double duty for all template
2574 // args. At this point, we have knowledge that we must be an
2575 // enum constant, so we set the expression type here, rather
2576 // than at the point of creation.
2577 if (ek == AST_Expression::EK_const && this->pd_n != 0)
2579 this->pd_ev->et = AST_Expression::EV_enum;
2583 // Expression equality comparison operator.
2584 bool
2585 AST_Expression::operator== (AST_Expression *vc)
2587 return compare (vc);
2590 bool
2591 AST_Expression::compare (AST_Expression *vc)
2593 if (this->pd_ec != vc->ec ())
2595 return false;
2598 this->evaluate (EK_const);
2599 vc->evaluate (EK_const);
2601 if (pd_ev == 0 || vc->ev () == 0)
2603 return false;
2606 if (this->pd_ev->et != vc->ev ()->et)
2608 return false;
2611 switch (pd_ev->et)
2613 case EV_short:
2614 return this->pd_ev->u.sval == vc->ev ()->u.sval;
2615 case EV_ushort:
2616 return this->pd_ev->u.usval == vc->ev ()->u.usval;
2617 case EV_long:
2618 return this->pd_ev->u.lval == vc->ev ()->u.lval;
2619 case EV_ulong:
2620 return this->pd_ev->u.ulval == vc->ev ()->u.ulval;
2621 case EV_float:
2622 return ACE::is_equal (this->pd_ev->u.fval, vc->ev ()->u.fval);
2623 case EV_double:
2624 return ACE::is_equal (this->pd_ev->u.dval, vc->ev ()->u.dval);
2625 case EV_char:
2626 return this->pd_ev->u.cval == vc->ev ()->u.cval;
2627 case EV_wchar:
2628 return this->pd_ev->u.wcval == vc->ev ()->u.wcval;
2629 case EV_octet:
2630 return this->pd_ev->u.oval == vc->ev ()->u.oval;
2631 case EV_bool:
2632 return this->pd_ev->u.lval == vc->ev ()->u.lval;
2633 case EV_string:
2634 return !ACE_OS::strcmp (pd_ev->u.strval->get_string (),
2635 vc->ev ()->u.strval->get_string ());
2636 case EV_wstring:
2637 return !ACE_OS::strcmp (pd_ev->u.wstrval, vc->ev ()->u.wstrval);
2638 case EV_longlong:
2639 return pd_ev->u.llval == vc->ev ()->u.llval;
2640 case EV_ulonglong:
2641 return pd_ev->u.ullval == vc->ev ()->u.ullval;
2642 case EV_fixed:
2643 return pd_ev->u.fixedval == vc->ev ()->u.fixedval;
2644 case EV_enum:
2645 return pd_ev->u.eval == vc->ev ()->u.eval;
2646 case EV_int8:
2647 return pd_ev->u.int8val == vc->ev ()->u.int8val;
2648 case EV_uint8:
2649 return pd_ev->u.uint8val == vc->ev ()->u.uint8val;
2650 default:
2651 return false;
2655 AST_Decl *
2656 AST_Expression::get_tdef (void) const
2658 return this->tdef;
2661 AST_Param_Holder *
2662 AST_Expression::param_holder (void) const
2664 return this->param_holder_;
2667 // Helper functions for expression dumpers.
2669 // Dump this binary AST_Expression node to the ostream o.
2670 static void
2671 dump_binary_expr (ACE_OSTREAM_TYPE &o,
2672 const char *s,
2673 AST_Expression *n1,
2674 AST_Expression *n2)
2676 if (n1 != 0)
2678 n1->dump (o);
2681 o << " " << s << " ";
2683 if (n2 != 0)
2685 n2->dump (o);
2689 // Dump this unary AST_Expression node to the ostream o.
2690 static void
2691 dump_unary_expr (ACE_OSTREAM_TYPE &o,
2692 const char *s,
2693 AST_Expression *e)
2695 o << s;
2696 e->dump (o);
2699 // Dump the supplied AST_ExprValue to the ostream o.
2700 static void
2701 dump_expr_val (ACE_OSTREAM_TYPE &o, AST_Expression::AST_ExprValue *ev)
2703 switch (ev->et)
2705 case AST_Expression::EV_short:
2706 o << ev->u.sval;
2707 return;
2708 case AST_Expression::EV_ushort:
2709 o << ev->u.usval;
2710 return;
2711 case AST_Expression::EV_long:
2712 o << ev->u.lval;
2713 return;
2714 case AST_Expression::EV_ulong:
2715 case AST_Expression::EV_enum:
2716 o << ev->u.ulval;
2717 return;
2718 case AST_Expression::EV_float:
2719 o << ev->u.fval;
2720 return;
2721 case AST_Expression::EV_double:
2722 o << ev->u.dval;
2723 return;
2724 case AST_Expression::EV_char:
2725 o << ev->u.cval;
2726 return;
2727 case AST_Expression::EV_wchar:
2728 #ifdef ACE_HAS_CPP20
2729 break;
2730 #else
2731 o << ev->u.wcval;
2732 return;
2733 #endif
2734 case AST_Expression::EV_octet:
2736 std::ios saved (0);
2737 saved.copyfmt (o);
2738 o << "0x" << std::hex << std::setw (2) << std::setfill ('0') << unsigned (ev->u.oval);
2739 o.copyfmt (saved);
2741 return;
2742 case AST_Expression::EV_bool:
2743 o << (ev->u.bval == true ? "TRUE" : "FALSE");
2744 return;
2745 case AST_Expression::EV_string:
2746 if (ev->u.strval != 0)
2748 ev->u.strval->dump (o);
2750 else
2752 o << "(null string)";
2754 return;
2755 case AST_Expression::EV_longlong:
2756 o << ev->u.llval;
2757 return;
2758 case AST_Expression::EV_ulonglong:
2759 o << ev->u.ullval;
2760 return;
2761 case AST_Expression::EV_fixed:
2762 o << ev->u.fixedval;
2763 return;
2764 case AST_Expression::EV_int8:
2765 o << static_cast<short> (ev->u.int8val);
2766 return;
2767 case AST_Expression::EV_uint8:
2768 o << static_cast<unsigned short> (ev->u.uint8val);
2769 return;
2770 case AST_Expression::EV_longdouble:
2771 case AST_Expression::EV_wstring:
2772 case AST_Expression::EV_any:
2773 case AST_Expression::EV_object:
2774 case AST_Expression::EV_void:
2775 case AST_Expression::EV_none:
2776 break;
2779 o << "(Can not dump type " << AST_Expression::exprtype_to_string (ev->et) << ")";
2782 // Dump an AST_Expression node to the ostream o.
2783 void
2784 AST_Expression::dump (ACE_OSTREAM_TYPE &o)
2786 // See if it was a constant or was evaluated already.
2787 if (this->pd_ev != 0)
2789 dump_expr_val (o, this->pd_ev);
2790 return;
2793 // OK, must print out an expression.
2794 switch (this->pd_ec)
2796 // Binary expressions:
2797 case EC_add:
2798 dump_binary_expr (o,
2799 "+",
2800 this->pd_v1,
2801 this->pd_v2);
2802 break;
2803 case EC_minus:
2804 dump_binary_expr (o,
2805 "-",
2806 this->pd_v1,
2807 this->pd_v2);
2808 break;
2809 case EC_mul:
2810 dump_binary_expr (o,
2811 "*",
2812 this->pd_v1,
2813 this->pd_v2);
2814 break;
2815 case EC_div:
2816 dump_binary_expr (o,
2817 "/",
2818 this->pd_v1,
2819 this->pd_v2);
2820 break;
2821 case EC_mod:
2822 dump_binary_expr (o,
2823 "%",
2824 this->pd_v1,
2825 this->pd_v2);
2826 break;
2827 case EC_or:
2828 dump_binary_expr (o,
2829 "|",
2830 this->pd_v1,
2831 this->pd_v2);
2832 break;
2833 case EC_xor:
2834 dump_binary_expr (o,
2835 "^",
2836 this->pd_v1,
2837 this->pd_v2);
2838 break;
2839 case EC_and:
2840 dump_binary_expr (o,
2841 "&",
2842 this->pd_v1,
2843 this->pd_v2);
2844 break;
2845 case EC_left:
2846 dump_binary_expr (o,
2847 "<<",
2848 this->pd_v1,
2849 this->pd_v2);
2850 break;
2851 case EC_right:
2852 dump_binary_expr (o,
2853 ">>",
2854 this->pd_v1,
2855 this->pd_v2);
2856 break;
2857 // Unary expressions.
2858 case EC_u_plus:
2859 dump_unary_expr (o,
2860 "+",
2861 this->pd_v1);
2862 break;
2863 case EC_u_minus:
2864 dump_unary_expr (o,
2865 "-",
2866 this->pd_v1);
2867 break;
2868 case EC_bit_neg:
2869 dump_unary_expr (o,
2870 "~",
2871 this->pd_v1);
2872 break;
2873 // Unevaluated symbol.
2874 case EC_symbol:
2875 if (this->pd_n)
2876 this->pd_n->dump (o);
2877 else
2878 o << ACE_TEXT ("(nil symbolic name)");
2879 break;
2880 case EC_none:
2881 o << ACE_TEXT ("(none)");
2882 break;
2883 default:
2884 o << ACE_TEXT ("unsupported dump mode for expression with ec == ")
2885 << (int) this->pd_ec ;
2886 break;
2891 AST_Expression::ast_accept (ast_visitor *visitor)
2893 return visitor->visit_expression (this);
2896 void
2897 AST_Expression::destroy (void)
2899 if (0 != this->pd_ev)
2901 if (EV_string == this->pd_ev->et)
2903 this->pd_ev->u.strval->destroy ();
2904 delete this->pd_ev->u.strval;
2905 this->pd_ev->u.strval = 0;
2907 else if (EV_wstring == this->pd_ev->et)
2909 ACE::strdelete (this->pd_ev->u.wstrval);
2910 this->pd_ev->u.wstrval = 0;
2913 delete this->pd_ev;
2914 this->pd_ev = 0;
2916 if (this->pd_v1 != 0)
2918 this->pd_v1->destroy ();
2920 delete this->pd_v1;
2921 this->pd_v1 = 0;
2923 if (this->pd_v2 != 0)
2925 this->pd_v2->destroy ();
2927 delete this->pd_v2;
2928 this->pd_v2 = 0;
2930 if (this->pd_n != 0)
2932 this->pd_n->destroy ();
2934 delete this->pd_n;
2935 this->pd_n = 0;
2937 if (this->param_holder_ != 0)
2939 this->param_holder_->destroy ();
2940 delete this->param_holder_;
2941 this->param_holder_ = 0;
2945 // Data accessors.
2947 UTL_Scope *
2948 AST_Expression::defined_in (void)
2950 return this->pd_defined_in;
2953 void
2954 AST_Expression::set_defined_in (UTL_Scope *d)
2956 this->pd_defined_in = d;
2959 long
2960 AST_Expression::line (void)
2962 return this->pd_line;
2965 void
2966 AST_Expression::set_line (long l)
2968 this->pd_line = l;
2971 UTL_String *
2972 AST_Expression::file_name (void)
2974 return this->pd_file_name;
2977 void
2978 AST_Expression::set_file_name (UTL_String *f)
2980 this->pd_file_name = f;
2983 AST_Expression::ExprComb
2984 AST_Expression::ec (void)
2986 return this->pd_ec;
2989 AST_Expression::AST_ExprValue *
2990 AST_Expression::ev (void)
2992 return this->pd_ev;
2995 void
2996 AST_Expression::set_ev (AST_Expression::AST_ExprValue *new_ev)
2998 delete this->pd_ev;
2999 this->pd_ev = new_ev;
3002 AST_Expression *
3003 AST_Expression::v1 (void)
3005 return this->pd_v1;
3008 void
3009 AST_Expression::set_v1 (AST_Expression *e)
3011 this->pd_v1 = e;
3014 AST_Expression *
3015 AST_Expression::v2 (void)
3017 return this->pd_v2;
3020 void
3021 AST_Expression::set_v2 (AST_Expression *e)
3023 this->pd_v2 = e;
3026 UTL_ScopedName *
3027 AST_Expression::n (void)
3029 return this->pd_n;
3032 void
3033 AST_Expression::set_n (UTL_ScopedName *new_n)
3035 this->pd_n = new_n;
3038 const char *
3039 AST_Expression::exprtype_to_string (ExprType t)
3041 switch (t) {
3042 case AST_Expression::EV_short:
3043 return "short";
3044 case AST_Expression::EV_ushort:
3045 return "unsigned short";
3046 case AST_Expression::EV_long:
3047 return "long";
3048 case AST_Expression::EV_ulong:
3049 return "unsigned long";
3050 case AST_Expression::EV_float:
3051 return "float";
3052 case AST_Expression::EV_double:
3053 return "double";
3054 case AST_Expression::EV_char:
3055 return "char";
3056 case AST_Expression::EV_octet:
3057 return "octet";
3058 case AST_Expression::EV_bool:
3059 return "boolean";
3060 case AST_Expression::EV_string:
3061 return "string";
3062 case AST_Expression::EV_enum:
3063 return "enum";
3064 case AST_Expression::EV_void:
3065 return "void";
3066 case AST_Expression::EV_none:
3067 return "none";
3068 case AST_Expression::EV_wchar:
3069 return "wchar";
3070 case AST_Expression::EV_longlong:
3071 return "longlong";
3072 case AST_Expression::EV_ulonglong:
3073 return "ulonglong";
3074 case AST_Expression::EV_longdouble:
3075 return "longdouble";
3076 case AST_Expression::EV_wstring:
3077 return "wstring";
3078 case AST_Expression::EV_any:
3079 return "any";
3080 case AST_Expression::EV_object:
3081 return "object";
3082 case AST_Expression::EV_fixed:
3083 return "fixed";
3084 case AST_Expression::EV_uint8:
3085 return "uint8";
3086 case AST_Expression::EV_int8:
3087 return "int8";
3090 return "<UNKNOWN TYPE>";
3093 AST_Enum *
3094 AST_Expression::enum_parent ()
3096 return enum_parent_;
3099 void
3100 AST_Expression::enum_parent (AST_Enum *node)
3102 enum_parent_ = node;