Fix Broken Floating Point Expressions
[ACE_TAO.git] / TAO / TAO_IDL / ast / ast_expression.cpp
blob0434ad4167172757dbd4d8c2977c00141073cf46
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 ()
131 this->pd_defined_in = idl_global->scopes ().depth () > 0
132 ? idl_global->scopes ().top ()
133 : nullptr ;
134 this->pd_line = idl_global->lineno ();
135 this->pd_file_name = idl_global->filename ();
136 enum_parent (nullptr);
139 // An AST_Expression denoting a symbolic name.
140 AST_Expression::AST_Expression (UTL_ScopedName *nm)
141 : pd_ec (EC_symbol),
142 pd_ev (nullptr),
143 pd_v1 (nullptr),
144 pd_v2 (nullptr),
145 pd_n (nm),
146 tdef (nullptr),
147 param_holder_ (nullptr)
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 (nullptr),
167 pd_v1 (nullptr),
168 pd_v2 (nullptr),
169 pd_n (nullptr),
170 tdef (nullptr),
171 param_holder_ (nullptr)
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 == nullptr)
202 this->pd_ev = v->coerce (t);
204 if (this->pd_ev == nullptr)
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 (nullptr != 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 (nullptr),
234 pd_v1 (ev1),
235 pd_v2 (ev2),
236 pd_n (nullptr),
237 tdef (nullptr),
238 param_holder_ (nullptr)
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 (nullptr),
247 pd_v1 (nullptr),
248 pd_v2 (nullptr),
249 pd_n (nullptr),
250 tdef (nullptr),
251 param_holder_ (nullptr)
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 (nullptr),
266 pd_v1 (nullptr),
267 pd_v2 (nullptr),
268 pd_n (nullptr),
269 tdef (nullptr),
270 param_holder_ (nullptr)
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 (nullptr),
285 pd_v1 (nullptr),
286 pd_v2 (nullptr),
287 pd_n (nullptr),
288 tdef (nullptr),
289 param_holder_ (nullptr)
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 (nullptr),
304 pd_v1 (nullptr),
305 pd_v2 (nullptr),
306 pd_n (nullptr),
307 tdef (nullptr),
308 param_holder_ (nullptr)
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 (nullptr),
323 pd_v1 (nullptr),
324 pd_v2 (nullptr),
325 pd_n (nullptr),
326 tdef (nullptr),
327 param_holder_ (nullptr)
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 (nullptr),
342 pd_v1 (nullptr),
343 pd_v2 (nullptr),
344 pd_n (nullptr),
345 tdef (nullptr),
346 param_holder_ (nullptr)
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 (nullptr),
361 pd_v1 (nullptr),
362 pd_v2 (nullptr),
363 pd_n (nullptr),
364 tdef (nullptr),
365 param_holder_ (nullptr)
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 (nullptr),
381 pd_v1 (nullptr),
382 pd_v2 (nullptr),
383 pd_n (nullptr),
384 tdef (nullptr),
385 param_holder_ (nullptr)
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 (nullptr),
400 pd_v1 (nullptr),
401 pd_v2 (nullptr),
402 pd_n (nullptr),
403 tdef (nullptr),
404 param_holder_ (nullptr)
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 (nullptr),
419 pd_v1 (nullptr),
420 pd_v2 (nullptr),
421 pd_n (nullptr),
422 tdef (nullptr),
423 param_holder_ (nullptr)
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 (nullptr),
438 pd_v1 (nullptr),
439 pd_v2 (nullptr),
440 pd_n (nullptr),
441 tdef (nullptr),
442 param_holder_ (nullptr)
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 (nullptr),
457 pd_v1 (nullptr),
458 pd_v2 (nullptr),
459 pd_n (nullptr),
460 tdef (nullptr),
461 param_holder_ (nullptr)
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 (nullptr),
476 pd_v1 (nullptr),
477 pd_v2 (nullptr),
478 pd_n (nullptr),
479 tdef (nullptr),
480 param_holder_ (nullptr)
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 (nullptr),
495 pd_v1 (nullptr),
496 pd_v2 (nullptr),
497 pd_n (nullptr),
498 tdef (nullptr),
499 param_holder_ (nullptr)
501 this->fill_definition_details ();
503 ACE_NEW (this->pd_ev,
504 AST_ExprValue);
506 UTL_String *new_str = nullptr;
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 (nullptr),
518 pd_v1 (nullptr),
519 pd_v2 (nullptr),
520 pd_n (nullptr),
521 tdef (nullptr),
522 param_holder_ (nullptr)
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 (nullptr),
536 pd_v1 (nullptr),
537 pd_v2 (nullptr),
538 pd_n (nullptr),
539 tdef (nullptr),
540 param_holder_ (nullptr)
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 ()
553 destroy ();
556 AST_Expression::AST_ExprValue::AST_ExprValue ()
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 == nullptr)
572 return nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
1531 default:
1532 return nullptr;
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 = nullptr;
1606 ACE_NEW_RETURN (newval,
1607 AST_Expression::AST_ExprValue,
1608 nullptr);
1610 if (ev != nullptr)
1612 *newval = *ev;
1615 AST_Expression::AST_ExprValue *retval = nullptr;
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 = nullptr;
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 = nullptr;
1732 if (this->pd_v1 == nullptr || this->pd_v2 == nullptr)
1734 return nullptr;
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 () == nullptr || this->pd_v2->ev () == nullptr)
1742 return nullptr;
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 nullptr;
1749 ACE_NEW_RETURN (retval,
1750 AST_ExprValue,
1751 nullptr);
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 = nullptr;
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 = nullptr;
1847 if (this->pd_v1 == nullptr || this->pd_v2 == nullptr)
1849 return nullptr;
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 () == nullptr || this->pd_v2->ev () == nullptr)
1857 return nullptr;
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 nullptr;
1864 ACE_NEW_RETURN (retval,
1865 AST_ExprValue,
1866 nullptr);
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 case EV_bool:
1921 success = do_eval_mod_op<ACE_CDR::Boolean> (
1922 pd_v1->ev ()->u.bval, pd_v2->ev ()->u.bval, retval->u.bval);
1923 break;
1925 default:
1926 break;
1929 if (!success)
1931 delete retval;
1932 retval = nullptr;
1935 return retval;
1938 // Apply bitwise operations to an AST_Expression after evaluating
1939 // its sub-expressions.
1940 // Operations supported: '%', '|', '&', '^', '<<', '>>'
1942 template <typename Type>
1943 bool
1944 do_eval_bit_op_no_shift (AST_Expression::ExprComb op, Type a, Type b, Type &result)
1946 switch (op)
1948 case AST_Expression::EC_or:
1949 result = a | b;
1950 break;
1951 case AST_Expression::EC_xor:
1952 result = a ^ b;
1953 break;
1954 case AST_Expression::EC_and:
1955 result = a & b;
1956 break;
1957 default:
1958 return false;
1961 return true;
1964 template <typename Type>
1965 bool
1966 do_eval_bit_op (AST_Expression::ExprComb op, Type a, Type b, Type &result)
1968 switch (op)
1970 case AST_Expression::EC_left:
1971 result = a << b;
1972 break;
1973 case AST_Expression::EC_right:
1974 result = a >> b;
1975 break;
1976 default:
1977 return do_eval_bit_op_no_shift (op, a, b, result);
1980 return true;
1983 AST_Expression::AST_ExprValue *
1984 AST_Expression::eval_bit_op (AST_Expression::EvalKind ek)
1986 AST_Expression::AST_ExprValue *retval = nullptr;
1988 if (this->pd_v1 == nullptr || this->pd_v2 == nullptr)
1990 return nullptr;
1993 this->pd_v1->set_ev (this->pd_v1->eval_internal (ek));
1994 this->pd_v2->set_ev (this->pd_v2->eval_internal (ek));
1996 if (this->pd_v1->ev () == nullptr || this->pd_v2->ev () == nullptr)
1998 return nullptr;
2001 ExprType const expr_type = eval_kind_to_expr_type (ek);
2002 if (expr_type == EV_none) return nullptr;
2004 ACE_NEW_RETURN (retval,
2005 AST_ExprValue,
2006 nullptr);
2008 pd_v1->set_ev (pd_v1->coerce (expr_type));
2009 pd_v2->set_ev (pd_v2->coerce (expr_type));
2010 retval->et = expr_type;
2012 bool success = false;
2013 switch (expr_type)
2015 case EV_int8:
2016 success = do_eval_bit_op<ACE_CDR::Int8> (pd_ec,
2017 pd_v1->ev ()->u.int8val, pd_v2->ev ()->u.int8val, retval->u.int8val);
2018 break;
2020 case EV_uint8:
2021 success = do_eval_bit_op<ACE_CDR::UInt8> (pd_ec,
2022 pd_v1->ev ()->u.uint8val, pd_v2->ev ()->u.uint8val, retval->u.uint8val);
2023 break;
2025 case EV_short:
2026 success = do_eval_bit_op<ACE_CDR::Short> (pd_ec,
2027 pd_v1->ev ()->u.sval, pd_v2->ev ()->u.sval, retval->u.sval);
2028 break;
2030 case EV_ushort:
2031 success = do_eval_bit_op<ACE_CDR::UShort> (pd_ec,
2032 pd_v1->ev ()->u.usval, pd_v2->ev ()->u.usval, retval->u.usval);
2033 break;
2035 case EV_long:
2036 success = do_eval_bit_op<ACE_CDR::Long> (pd_ec,
2037 pd_v1->ev ()->u.lval, pd_v2->ev ()->u.lval, retval->u.lval);
2038 break;
2040 case EV_ulong:
2041 success = do_eval_bit_op<ACE_CDR::ULong> (pd_ec,
2042 pd_v1->ev ()->u.ulval, pd_v2->ev ()->u.ulval, retval->u.ulval);
2043 break;
2045 case EV_longlong:
2046 success = do_eval_bit_op<ACE_CDR::LongLong> (pd_ec,
2047 pd_v1->ev ()->u.llval, pd_v2->ev ()->u.llval, retval->u.llval);
2048 break;
2050 case EV_ulonglong:
2051 success = do_eval_bit_op<ACE_CDR::ULongLong> (pd_ec,
2052 pd_v1->ev ()->u.ullval, pd_v2->ev ()->u.ullval, retval->u.ullval);
2053 break;
2055 case EV_octet:
2056 success = do_eval_bit_op<ACE_CDR::Octet> (pd_ec,
2057 pd_v1->ev ()->u.oval, pd_v2->ev ()->u.oval, retval->u.oval);
2058 break;
2060 case EV_bool:
2061 success = do_eval_bit_op_no_shift<ACE_CDR::Boolean> (pd_ec,
2062 pd_v1->ev ()->u.bval, pd_v2->ev ()->u.bval, retval->u.bval);
2063 break;
2065 default:
2066 break;
2069 if (!success)
2071 delete retval;
2072 retval = nullptr;
2075 return retval;
2078 // Apply unary operators to an AST_Expression after evaluating its
2079 // sub-expression.
2080 // Operations supported: '-', '+', '~'
2081 AST_Expression::AST_ExprValue *
2082 AST_Expression::eval_un_op (AST_Expression::EvalKind ek)
2084 AST_ExprValue *retval = nullptr;
2086 if (this->pd_ev != nullptr)
2088 return this->pd_ev;
2091 if (this->pd_v1 == nullptr)
2093 return nullptr;
2096 this->pd_v1->set_ev (this->pd_v1->eval_internal (ek));
2098 if (this->pd_v1->ev () == nullptr)
2100 return nullptr;
2103 ACE_NEW_RETURN (retval,
2104 AST_ExprValue,
2105 nullptr);
2107 if (this->pd_v1->ev ()->et == EV_fixed)
2109 retval->et = EV_fixed;
2110 switch (this->pd_ec)
2112 case EC_u_plus:
2113 retval->u.fixedval = this->pd_v1->ev ()->u.fixedval;
2114 break;
2115 case EC_u_minus:
2116 retval->u.fixedval = -this->pd_v1->ev ()->u.fixedval;
2117 break;
2118 default:
2119 delete retval;
2120 return nullptr;
2122 return retval;
2125 retval->et = EV_double;
2127 switch (this->pd_ec)
2129 case EC_u_plus:
2130 this->pd_v1->set_ev (this->pd_v1->coerce (EV_double));
2132 if (this->pd_v1->ev () == nullptr)
2134 delete retval;
2135 retval = nullptr;
2136 return nullptr;
2139 retval->u.dval = this->pd_v1->ev ()->u.dval;
2140 break;
2141 case EC_u_minus:
2142 this->pd_v1->set_ev (this->pd_v1->coerce (EV_double));
2144 if (this->pd_v1->ev () == nullptr)
2146 delete retval;
2147 retval = nullptr;
2148 return nullptr;
2151 retval->u.dval = -(this->pd_v1->ev ()->u.dval);
2152 break;
2153 case EC_bit_neg:
2154 if (this->pd_v1->ev () == nullptr)
2156 delete retval;
2157 retval = nullptr;
2158 return nullptr;
2161 switch (this->pd_v1->ev ()->et)
2163 case EV_short:
2164 retval->u.sval = ~this->pd_v1->ev ()->u.sval;
2165 break;
2166 case EV_ushort:
2167 retval->u.usval = ~this->pd_v1->ev ()->u.usval;
2168 break;
2169 case EV_long:
2170 retval->u.lval = ~this->pd_v1->ev ()->u.lval;
2171 break;
2172 case EV_ulong:
2173 retval->u.ulval = ~this->pd_v1->ev ()->u.ulval;
2174 break;
2175 case EV_longlong:
2176 retval->u.llval = ~this->pd_v1->ev ()->u.llval;
2177 break;
2178 case EV_ulonglong:
2179 retval->u.ullval = ~this->pd_v1->ev ()->u.ullval;
2180 break;
2181 case EV_octet:
2182 retval->u.oval = ~this->pd_v1->ev ()->u.oval;
2183 break;
2184 case EV_int8:
2185 retval->u.int8val = ~pd_v1->ev ()->u.int8val;
2186 break;
2187 case EV_uint8:
2188 retval->u.uint8val = ~pd_v1->ev ()->u.uint8val;
2189 break;
2190 default:
2191 delete retval;
2192 retval = nullptr;
2193 return nullptr;
2195 retval->et = pd_v1->ev ()->et;
2197 break;
2198 default:
2199 delete retval;
2200 retval = nullptr;
2201 return nullptr;
2204 return retval;
2207 // Evaluate a symbolic AST_Expression by looking up the named
2208 // symbol.
2209 AST_Expression::AST_ExprValue *
2210 AST_Expression::eval_symbol (AST_Expression::EvalKind ek)
2212 UTL_Scope *s = nullptr;
2213 AST_Decl *d = nullptr;
2214 AST_Constant *c = nullptr;
2216 // Is there a symbol stored?
2217 if (this->pd_n == nullptr)
2219 idl_global->err ()->eval_error (this);
2220 return nullptr;
2223 // Get current scope for lookup.
2224 if (idl_global->scopes ().depth () > 0)
2226 s = idl_global->scopes ().top_non_null ();
2229 if (s == nullptr)
2231 idl_global->err ()->lookup_error (this->pd_n);
2232 return nullptr;
2235 // If we are a template parameter placeholder, just skip the
2236 // rest - nothing needs to be evaluated until instantiation
2237 // time.
2238 if (this->param_holder_ != nullptr)
2240 return nullptr;
2243 // Do lookup.
2244 d = s->lookup_by_name (this->pd_n,
2245 true);
2247 if (d == nullptr)
2249 idl_global->err ()->lookup_error (this->pd_n);
2250 return nullptr;
2253 // Is it a constant?
2254 if (d->node_type () != AST_Decl::NT_const
2255 && d->node_type () != AST_Decl::NT_enum_val)
2257 idl_global->err ()->constant_expected (this->pd_n,
2259 return nullptr;
2262 if (d->node_type () == AST_Decl::NT_enum_val)
2264 AST_EnumVal *enumval = dynamic_cast<AST_EnumVal*> (d);
2265 enum_parent (enumval->enum_parent ());
2268 // OK, now evaluate the constant we just got, to produce its value.
2269 c = dynamic_cast<AST_Constant*> (d);
2271 if (c == nullptr)
2273 return nullptr;
2276 return c->constant_value ()->eval_internal (ek);
2279 bool
2280 AST_Expression::type_mismatch (AST_Expression::ExprType t)
2282 if (this->pd_ev != nullptr)
2284 return incompatible_types (this->pd_ev->et, t);
2287 bool v1_mismatch = false;
2288 bool v2_mismatch = false;
2290 if (this->pd_v1 != nullptr)
2292 v1_mismatch = this->pd_v1->type_mismatch (t);
2295 if (this->pd_v2 != nullptr)
2297 v2_mismatch = this->pd_v2->type_mismatch (t);
2300 return v1_mismatch | v2_mismatch;
2303 // Coerce "this" to the ExprType required. Returns a copy of the
2304 // original ExprValue with the coercion applied, if successful, or
2305 // 0 if failed.
2306 AST_Expression::AST_ExprValue *
2307 AST_Expression::check_and_coerce (AST_Expression::ExprType t,
2308 AST_Decl *d)
2310 if (d != nullptr)
2312 AST_Decl *enum_val =
2313 idl_global->scopes ().top_non_null ()->lookup_by_name (this->pd_n,
2314 true);
2316 if (enum_val != nullptr)
2318 AST_Decl *enum_decl = ScopeAsDecl (enum_val->defined_in ());
2320 if (d->node_type () == AST_Decl::NT_typedef)
2322 AST_Typedef *td = dynamic_cast<AST_Typedef*> (d);
2323 d = td->primitive_base_type ();
2326 if (d != enum_decl)
2328 idl_global->err ()->incompatible_type_error (this);
2329 return nullptr;
2334 if (this->type_mismatch (t))
2336 idl_global->err ()->incompatible_type_error (this);
2337 return nullptr;
2340 if (d != nullptr && d->node_type () == AST_Decl::NT_typedef)
2342 this->tdef = d;
2345 return this->coerce (t);
2348 // Coerce "this" to the ExprType required. Returns a copy of the
2349 // original ExprValue with the coercion applied, if successful, or
2350 // 0 if failed.
2351 AST_Expression::AST_ExprValue *
2352 AST_Expression::coerce (AST_Expression::ExprType t)
2354 AST_ExprValue *tmp = nullptr;
2356 // First, evaluate it, then try to coerce result type.
2357 // If already evaluated, return the result.
2358 switch (t)
2360 case EV_int8:
2361 tmp = this->eval_internal (EK_int8);
2362 break;
2363 case EV_uint8:
2364 tmp = this->eval_internal (EK_uint8);
2365 break;
2366 case EV_short:
2367 tmp = this->eval_internal (EK_short);
2368 break;
2369 case EV_ushort:
2370 tmp = this->eval_internal (EK_ushort);
2371 break;
2372 case EV_long:
2373 tmp = this->eval_internal (EK_long);
2374 break;
2375 case EV_ulong:
2376 tmp = this->eval_internal (EK_ulong);
2377 break;
2378 case EV_longlong:
2379 tmp = this->eval_internal (EK_longlong);
2380 break;
2381 case EV_ulonglong:
2382 tmp = this->eval_internal (EK_ulonglong);
2383 break;
2384 case EV_octet:
2385 tmp = this->eval_internal (EK_octet);
2386 break;
2387 case EV_bool:
2388 tmp = this->eval_internal (EK_bool);
2389 break;
2390 case EV_fixed:
2391 tmp = this->eval_internal (EK_fixed_point);
2392 break;
2393 case EV_float:
2394 case EV_double:
2395 case EV_longdouble:
2396 tmp = eval_internal (EK_floating_point);
2397 break;
2398 case EV_char:
2399 case EV_wchar:
2400 case EV_string:
2401 case EV_wstring:
2402 case EV_enum:
2403 case EV_any:
2404 case EV_object:
2405 case EV_void:
2406 tmp = eval_internal (EK_const);
2407 case EV_none:
2408 break;
2411 if (tmp == nullptr)
2413 return nullptr;
2415 else
2417 delete this->pd_ev;
2418 this->pd_ev = tmp;
2421 // Create a copy to contain coercion result.
2422 AST_ExprValue *copy = nullptr;
2423 ACE_NEW_RETURN (copy,
2424 AST_ExprValue,
2425 nullptr);
2427 copy->et = this->pd_ev->et;
2429 switch (this->pd_ev->et)
2431 case EV_longdouble:
2432 case EV_void:
2433 case EV_none:
2434 delete copy;
2435 copy = nullptr;
2436 return nullptr;
2437 case EV_enum:
2438 copy->u.ulval = this->pd_ev->u.ulval;
2439 break;
2440 case EV_short:
2441 copy->u.sval = this->pd_ev->u.sval;
2442 break;
2443 case EV_ushort:
2444 copy->u.usval = this->pd_ev->u.usval;
2445 break;
2446 case EV_long:
2447 copy->u.lval = this->pd_ev->u.lval;
2448 break;
2449 case EV_ulong:
2450 copy->u.ulval = this->pd_ev->u.ulval;
2451 break;
2452 case EV_longlong:
2453 copy->u.llval = this->pd_ev->u.llval;
2454 break;
2455 case EV_ulonglong:
2456 copy->u.ullval = this->pd_ev->u.ullval;
2457 break;
2458 case EV_bool:
2459 copy->u.bval = this->pd_ev->u.bval;
2460 break;
2461 case EV_float:
2462 copy->u.fval = this->pd_ev->u.fval;
2463 break;
2464 case EV_double:
2465 copy->u.dval = this->pd_ev->u.dval;
2466 break;
2467 case EV_char:
2468 copy->u.cval = this->pd_ev->u.cval;
2469 break;
2470 case EV_wchar:
2471 copy->u.wcval = this->pd_ev->u.wcval;
2472 break;
2473 case EV_octet:
2474 copy->u.oval = this->pd_ev->u.oval;
2475 break;
2476 case EV_string:
2477 copy->u.strval = this->pd_ev->u.strval;
2478 break;
2479 case EV_wstring:
2480 copy->u.wstrval = this->pd_ev->u.wstrval;
2481 break;
2482 case EV_fixed:
2483 copy->u.fixedval = this->pd_ev->u.fixedval;
2484 break;
2485 case EV_int8:
2486 copy->u.int8val = this->pd_ev->u.int8val;
2487 break;
2488 case EV_uint8:
2489 copy->u.uint8val = this->pd_ev->u.uint8val;
2490 break;
2491 case EV_any:
2492 case EV_object:
2493 break;
2496 if (this->pd_ev->et == t)
2498 return copy;
2500 else
2502 AST_ExprValue *ev = coerce_value (copy, t);
2504 if (ev == nullptr)
2506 // coerce_value() will either return 'copy' or
2507 // return 0 without freeing 'copy'. It's
2508 // deleted here instead of putting a zillion
2509 // delete statments inside coerce_value().
2510 delete copy;
2511 copy = nullptr;
2514 return ev;
2518 // Eval used internally.
2519 AST_Expression::AST_ExprValue *
2520 AST_Expression::eval_internal (AST_Expression::EvalKind ek)
2522 // Already evaluated?
2523 if (this->pd_ev != nullptr)
2525 return eval_kind (this->pd_ev,
2526 ek);
2529 // OK, must evaluate operator.
2530 switch (this->pd_ec)
2532 case EC_add:
2533 case EC_minus:
2534 case EC_mul:
2535 case EC_div:
2536 this->pd_ev = this->eval_bin_op (ek);
2537 return eval_kind (this->pd_ev,
2538 ek);
2539 case EC_mod:
2540 this->pd_ev = this->eval_mod_op (ek);
2541 return eval_kind (this->pd_ev,
2542 ek);
2543 case EC_or:
2544 case EC_xor:
2545 case EC_and:
2546 case EC_left:
2547 case EC_right:
2548 this->pd_ev = this->eval_bit_op (ek);
2549 return eval_kind (this->pd_ev,
2550 ek);
2551 case EC_u_plus:
2552 case EC_u_minus:
2553 case EC_bit_neg:
2554 this->pd_ev = this->eval_un_op (ek);
2555 return eval_kind (this->pd_ev,
2556 ek);
2557 case EC_symbol:
2558 this->pd_ev = this->eval_symbol (ek);
2559 return eval_kind (this->pd_ev,
2560 ek);
2561 case EC_none:
2562 return nullptr;
2565 return nullptr;
2568 // Public operations.
2570 // Evaluate "this", assigning the value to the pd_ev field.
2571 void
2572 AST_Expression::evaluate (EvalKind ek)
2574 AST_ExprValue *tmp = eval_kind (this->pd_ev, ek);
2575 delete this->pd_ev;
2576 this->pd_ev = tmp;
2578 // Artifact of expressions doing double duty for all template
2579 // args. At this point, we have knowledge that we must be an
2580 // enum constant, so we set the expression type here, rather
2581 // than at the point of creation.
2582 if (ek == AST_Expression::EK_const && this->pd_n != nullptr)
2584 this->pd_ev->et = AST_Expression::EV_enum;
2588 // Expression equality comparison operator.
2589 bool
2590 AST_Expression::operator== (AST_Expression *vc)
2592 return compare (vc);
2595 bool
2596 AST_Expression::compare (AST_Expression *vc)
2598 if (this->pd_ec != vc->ec ())
2600 return false;
2603 this->evaluate (EK_const);
2604 vc->evaluate (EK_const);
2606 if (pd_ev == nullptr || vc->ev () == nullptr)
2608 return false;
2611 if (this->pd_ev->et != vc->ev ()->et)
2613 return false;
2616 switch (pd_ev->et)
2618 case EV_short:
2619 return this->pd_ev->u.sval == vc->ev ()->u.sval;
2620 case EV_ushort:
2621 return this->pd_ev->u.usval == vc->ev ()->u.usval;
2622 case EV_long:
2623 return this->pd_ev->u.lval == vc->ev ()->u.lval;
2624 case EV_ulong:
2625 return this->pd_ev->u.ulval == vc->ev ()->u.ulval;
2626 case EV_float:
2627 return ACE::is_equal (this->pd_ev->u.fval, vc->ev ()->u.fval);
2628 case EV_double:
2629 return ACE::is_equal (this->pd_ev->u.dval, vc->ev ()->u.dval);
2630 case EV_char:
2631 return this->pd_ev->u.cval == vc->ev ()->u.cval;
2632 case EV_wchar:
2633 return this->pd_ev->u.wcval == vc->ev ()->u.wcval;
2634 case EV_octet:
2635 return this->pd_ev->u.oval == vc->ev ()->u.oval;
2636 case EV_bool:
2637 return this->pd_ev->u.lval == vc->ev ()->u.lval;
2638 case EV_string:
2639 return !ACE_OS::strcmp (pd_ev->u.strval->get_string (),
2640 vc->ev ()->u.strval->get_string ());
2641 case EV_wstring:
2642 return !ACE_OS::strcmp (pd_ev->u.wstrval, vc->ev ()->u.wstrval);
2643 case EV_longlong:
2644 return pd_ev->u.llval == vc->ev ()->u.llval;
2645 case EV_ulonglong:
2646 return pd_ev->u.ullval == vc->ev ()->u.ullval;
2647 case EV_fixed:
2648 return pd_ev->u.fixedval == vc->ev ()->u.fixedval;
2649 case EV_enum:
2650 return pd_ev->u.eval == vc->ev ()->u.eval;
2651 case EV_int8:
2652 return pd_ev->u.int8val == vc->ev ()->u.int8val;
2653 case EV_uint8:
2654 return pd_ev->u.uint8val == vc->ev ()->u.uint8val;
2655 default:
2656 return false;
2660 AST_Decl *
2661 AST_Expression::get_tdef () const
2663 return this->tdef;
2666 AST_Param_Holder *
2667 AST_Expression::param_holder () const
2669 return this->param_holder_;
2672 // Helper functions for expression dumpers.
2674 // Dump this binary AST_Expression node to the ostream o.
2675 static void
2676 dump_binary_expr (ACE_OSTREAM_TYPE &o,
2677 const char *s,
2678 AST_Expression *n1,
2679 AST_Expression *n2)
2681 if (n1 != nullptr)
2683 n1->dump (o);
2686 o << " " << s << " ";
2688 if (n2 != nullptr)
2690 n2->dump (o);
2694 // Dump this unary AST_Expression node to the ostream o.
2695 static void
2696 dump_unary_expr (ACE_OSTREAM_TYPE &o,
2697 const char *s,
2698 AST_Expression *e)
2700 o << s;
2701 e->dump (o);
2704 // Dump the supplied AST_ExprValue to the ostream o.
2705 static void
2706 dump_expr_val (ACE_OSTREAM_TYPE &o, AST_Expression::AST_ExprValue *ev)
2708 switch (ev->et)
2710 case AST_Expression::EV_short:
2711 o << ev->u.sval;
2712 return;
2713 case AST_Expression::EV_ushort:
2714 o << ev->u.usval;
2715 return;
2716 case AST_Expression::EV_long:
2717 o << ev->u.lval;
2718 return;
2719 case AST_Expression::EV_ulong:
2720 case AST_Expression::EV_enum:
2721 o << ev->u.ulval;
2722 return;
2723 case AST_Expression::EV_float:
2724 o << ev->u.fval;
2725 return;
2726 case AST_Expression::EV_double:
2727 o << ev->u.dval;
2728 return;
2729 case AST_Expression::EV_char:
2730 o << ev->u.cval;
2731 return;
2732 case AST_Expression::EV_wchar:
2733 o << ev->u.wcval;
2734 return;
2735 case AST_Expression::EV_octet:
2737 std::ios saved (nullptr);
2738 saved.copyfmt (o);
2739 o << "0x" << std::hex << std::setw (2) << std::setfill ('0') << unsigned (ev->u.oval);
2740 o.copyfmt (saved);
2742 return;
2743 case AST_Expression::EV_bool:
2744 o << (ev->u.bval == true ? "TRUE" : "FALSE");
2745 return;
2746 case AST_Expression::EV_string:
2747 if (ev->u.strval != nullptr)
2749 ev->u.strval->dump (o);
2751 else
2753 o << "(null string)";
2755 return;
2756 case AST_Expression::EV_longlong:
2757 o << ev->u.llval;
2758 return;
2759 case AST_Expression::EV_ulonglong:
2760 o << ev->u.ullval;
2761 return;
2762 case AST_Expression::EV_fixed:
2763 o << ev->u.fixedval;
2764 return;
2765 case AST_Expression::EV_int8:
2766 o << static_cast<short> (ev->u.int8val);
2767 return;
2768 case AST_Expression::EV_uint8:
2769 o << static_cast<unsigned short> (ev->u.uint8val);
2770 return;
2771 case AST_Expression::EV_longdouble:
2772 case AST_Expression::EV_wstring:
2773 case AST_Expression::EV_any:
2774 case AST_Expression::EV_object:
2775 case AST_Expression::EV_void:
2776 case AST_Expression::EV_none:
2777 break;
2780 o << "(Can not dump type " << AST_Expression::exprtype_to_string (ev->et) << ")";
2783 // Dump an AST_Expression node to the ostream o.
2784 void
2785 AST_Expression::dump (ACE_OSTREAM_TYPE &o)
2787 // See if it was a constant or was evaluated already.
2788 if (this->pd_ev != nullptr)
2790 dump_expr_val (o, this->pd_ev);
2791 return;
2794 // OK, must print out an expression.
2795 switch (this->pd_ec)
2797 // Binary expressions:
2798 case EC_add:
2799 dump_binary_expr (o,
2800 "+",
2801 this->pd_v1,
2802 this->pd_v2);
2803 break;
2804 case EC_minus:
2805 dump_binary_expr (o,
2806 "-",
2807 this->pd_v1,
2808 this->pd_v2);
2809 break;
2810 case EC_mul:
2811 dump_binary_expr (o,
2812 "*",
2813 this->pd_v1,
2814 this->pd_v2);
2815 break;
2816 case EC_div:
2817 dump_binary_expr (o,
2818 "/",
2819 this->pd_v1,
2820 this->pd_v2);
2821 break;
2822 case EC_mod:
2823 dump_binary_expr (o,
2824 "%",
2825 this->pd_v1,
2826 this->pd_v2);
2827 break;
2828 case EC_or:
2829 dump_binary_expr (o,
2830 "|",
2831 this->pd_v1,
2832 this->pd_v2);
2833 break;
2834 case EC_xor:
2835 dump_binary_expr (o,
2836 "^",
2837 this->pd_v1,
2838 this->pd_v2);
2839 break;
2840 case EC_and:
2841 dump_binary_expr (o,
2842 "&",
2843 this->pd_v1,
2844 this->pd_v2);
2845 break;
2846 case EC_left:
2847 dump_binary_expr (o,
2848 "<<",
2849 this->pd_v1,
2850 this->pd_v2);
2851 break;
2852 case EC_right:
2853 dump_binary_expr (o,
2854 ">>",
2855 this->pd_v1,
2856 this->pd_v2);
2857 break;
2858 // Unary expressions.
2859 case EC_u_plus:
2860 dump_unary_expr (o,
2861 "+",
2862 this->pd_v1);
2863 break;
2864 case EC_u_minus:
2865 dump_unary_expr (o,
2866 "-",
2867 this->pd_v1);
2868 break;
2869 case EC_bit_neg:
2870 dump_unary_expr (o,
2871 "~",
2872 this->pd_v1);
2873 break;
2874 // Unevaluated symbol.
2875 case EC_symbol:
2876 if (this->pd_n)
2877 this->pd_n->dump (o);
2878 else
2879 o << ACE_TEXT ("(nil symbolic name)");
2880 break;
2881 case EC_none:
2882 o << ACE_TEXT ("(none)");
2883 break;
2884 default:
2885 o << ACE_TEXT ("unsupported dump mode for expression with ec == ")
2886 << (int) this->pd_ec ;
2887 break;
2892 AST_Expression::ast_accept (ast_visitor *visitor)
2894 return visitor->visit_expression (this);
2897 void
2898 AST_Expression::destroy ()
2900 if (nullptr != this->pd_ev)
2902 if (EV_string == this->pd_ev->et)
2904 this->pd_ev->u.strval->destroy ();
2905 delete this->pd_ev->u.strval;
2906 this->pd_ev->u.strval = nullptr;
2908 else if (EV_wstring == this->pd_ev->et)
2910 ACE::strdelete (this->pd_ev->u.wstrval);
2911 this->pd_ev->u.wstrval = nullptr;
2914 delete this->pd_ev;
2915 this->pd_ev = nullptr;
2917 if (this->pd_v1 != nullptr)
2919 this->pd_v1->destroy ();
2921 delete this->pd_v1;
2922 this->pd_v1 = nullptr;
2924 if (this->pd_v2 != nullptr)
2926 this->pd_v2->destroy ();
2928 delete this->pd_v2;
2929 this->pd_v2 = nullptr;
2931 if (this->pd_n != nullptr)
2933 this->pd_n->destroy ();
2935 delete this->pd_n;
2936 this->pd_n = nullptr;
2938 if (this->param_holder_ != nullptr)
2940 this->param_holder_->destroy ();
2941 delete this->param_holder_;
2942 this->param_holder_ = nullptr;
2946 // Data accessors.
2948 UTL_Scope *
2949 AST_Expression::defined_in ()
2951 return this->pd_defined_in;
2954 void
2955 AST_Expression::set_defined_in (UTL_Scope *d)
2957 this->pd_defined_in = d;
2960 long
2961 AST_Expression::line ()
2963 return this->pd_line;
2966 void
2967 AST_Expression::set_line (long l)
2969 this->pd_line = l;
2972 UTL_String *
2973 AST_Expression::file_name ()
2975 return this->pd_file_name;
2978 void
2979 AST_Expression::set_file_name (UTL_String *f)
2981 this->pd_file_name = f;
2984 AST_Expression::ExprComb
2985 AST_Expression::ec ()
2987 return this->pd_ec;
2990 AST_Expression::AST_ExprValue *
2991 AST_Expression::ev ()
2993 return this->pd_ev;
2996 void
2997 AST_Expression::set_ev (AST_Expression::AST_ExprValue *new_ev)
2999 delete this->pd_ev;
3000 this->pd_ev = new_ev;
3003 AST_Expression *
3004 AST_Expression::v1 ()
3006 return this->pd_v1;
3009 void
3010 AST_Expression::set_v1 (AST_Expression *e)
3012 this->pd_v1 = e;
3015 AST_Expression *
3016 AST_Expression::v2 ()
3018 return this->pd_v2;
3021 void
3022 AST_Expression::set_v2 (AST_Expression *e)
3024 this->pd_v2 = e;
3027 UTL_ScopedName *
3028 AST_Expression::n ()
3030 return this->pd_n;
3033 void
3034 AST_Expression::set_n (UTL_ScopedName *new_n)
3036 this->pd_n = new_n;
3039 const char *
3040 AST_Expression::exprtype_to_string (ExprType t)
3042 switch (t) {
3043 case AST_Expression::EV_short:
3044 return "short";
3045 case AST_Expression::EV_ushort:
3046 return "unsigned short";
3047 case AST_Expression::EV_long:
3048 return "long";
3049 case AST_Expression::EV_ulong:
3050 return "unsigned long";
3051 case AST_Expression::EV_float:
3052 return "float";
3053 case AST_Expression::EV_double:
3054 return "double";
3055 case AST_Expression::EV_char:
3056 return "char";
3057 case AST_Expression::EV_octet:
3058 return "octet";
3059 case AST_Expression::EV_bool:
3060 return "boolean";
3061 case AST_Expression::EV_string:
3062 return "string";
3063 case AST_Expression::EV_enum:
3064 return "enum";
3065 case AST_Expression::EV_void:
3066 return "void";
3067 case AST_Expression::EV_none:
3068 return "none";
3069 case AST_Expression::EV_wchar:
3070 return "wchar";
3071 case AST_Expression::EV_longlong:
3072 return "longlong";
3073 case AST_Expression::EV_ulonglong:
3074 return "ulonglong";
3075 case AST_Expression::EV_longdouble:
3076 return "longdouble";
3077 case AST_Expression::EV_wstring:
3078 return "wstring";
3079 case AST_Expression::EV_any:
3080 return "any";
3081 case AST_Expression::EV_object:
3082 return "object";
3083 case AST_Expression::EV_fixed:
3084 return "fixed";
3085 case AST_Expression::EV_uint8:
3086 return "uint8";
3087 case AST_Expression::EV_int8:
3088 return "int8";
3091 return "<UNKNOWN TYPE>";
3094 AST_Enum *
3095 AST_Expression::enum_parent ()
3097 return enum_parent_;
3100 void
3101 AST_Expression::enum_parent (AST_Enum *node)
3103 enum_parent_ = node;