bump product version to 7.2.5.1
[LibreOffice.git] / idlc / source / astexpression.cxx
blob1afa91dbf8b4f6df9456f3644d59986115b681cd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <astexpression.hxx>
21 #include <astconstant.hxx>
22 #include <astscope.hxx>
23 #include <errorhandler.hxx>
25 #include <o3tl/float_int_conversion.hxx>
26 #include <osl/diagnose.h>
28 #include <limits.h>
29 #include <float.h>
30 #include <memory>
32 AstExpression::AstExpression(ExprComb c, AstExpression* pExpr1, AstExpression* pExpr2)
33 : m_combOperator(c)
34 , m_subExpr1(pExpr1)
35 , m_subExpr2(pExpr2)
39 AstExpression::AstExpression(sal_Int32 l)
40 : m_combOperator(ExprComb::NONE)
42 m_exprValue.reset(new AstExprValue);
43 m_exprValue->et = ET_long;
44 m_exprValue->u.lval = l;
47 AstExpression::AstExpression(sal_Int32 l, ExprType et)
48 : m_combOperator(ExprComb::NONE)
50 m_exprValue.reset(new AstExprValue);
51 m_exprValue->et = et;
52 m_exprValue->u.lval = l;
55 AstExpression::AstExpression(sal_Int64 h)
56 : m_combOperator(ExprComb::NONE)
58 m_exprValue.reset(new AstExprValue);
59 m_exprValue->et = ET_hyper;
60 m_exprValue->u.hval = h;
63 AstExpression::AstExpression(sal_uInt64 uh)
64 : m_combOperator(ExprComb::NONE)
66 m_exprValue.reset(new AstExprValue);
67 m_exprValue->et = ET_uhyper;
68 m_exprValue->u.uhval = uh;
71 AstExpression::AstExpression(double d)
72 : m_combOperator(ExprComb::NONE)
74 m_exprValue.reset(new AstExprValue);
75 m_exprValue->et = ET_double;
76 m_exprValue->u.dval = d;
79 AstExpression::AstExpression(OString* scopedName)
80 : m_combOperator(ExprComb::Symbol)
82 if (scopedName)
83 m_xSymbolicName = *scopedName;
86 AstExpression::~AstExpression() {}
89 * Perform the coercion from the given AstExprValue to the requested
90 * ExprType. Return an AstExprValue if successful, NULL if failed.
91 * must be done for hyper, uhyper
93 static bool coerce_value(AstExprValue* ev, ExprType t)
95 if (ev == nullptr)
96 return false;
98 switch (t)
100 case ET_short:
101 switch (ev->et)
103 case ET_short:
104 return true;
105 case ET_ushort:
107 if (ev->u.usval > SAL_MAX_INT16)
108 return false;
109 auto tmp = static_cast<sal_Int16>(ev->u.usval);
110 ev->u.sval = tmp;
111 ev->et = ET_short;
112 return true;
114 case ET_long:
116 if (ev->u.lval < SAL_MIN_INT16 || ev->u.lval > SAL_MAX_INT16)
117 return false;
118 auto tmp = static_cast<sal_Int16>(ev->u.lval);
119 ev->u.sval = tmp;
120 ev->et = ET_short;
121 return true;
123 case ET_ulong:
125 if (ev->u.ulval > SAL_MAX_INT16)
126 return false;
127 auto tmp = static_cast<sal_Int16>(ev->u.ulval);
128 ev->u.sval = tmp;
129 ev->et = ET_short;
130 return true;
132 case ET_hyper:
134 if (ev->u.hval < SAL_MIN_INT16 || ev->u.hval > SAL_MAX_INT16)
135 return false;
136 auto tmp = static_cast<sal_Int16>(ev->u.hval);
137 ev->u.sval = tmp;
138 ev->et = ET_short;
139 return true;
141 case ET_uhyper:
143 if (ev->u.uhval > SAL_MAX_INT16)
144 return false;
145 auto tmp = static_cast<sal_Int16>(ev->u.uhval);
146 ev->u.sval = tmp;
147 ev->et = ET_short;
148 return true;
150 case ET_boolean:
152 auto tmp = static_cast<sal_Int16>(ev->u.bval);
153 ev->u.sval = tmp;
154 ev->et = ET_short;
155 return true;
157 case ET_float:
159 if (!(o3tl::convertsToAtLeast(o3tl::roundAway(ev->u.fval), SAL_MIN_INT16)
160 && o3tl::convertsToAtMost(o3tl::roundAway(ev->u.fval), SAL_MAX_INT16)))
162 return false;
164 auto tmp = static_cast<sal_Int16>(ev->u.fval);
165 ev->u.sval = tmp;
166 ev->et = ET_short;
167 return true;
169 case ET_double:
171 if (!(o3tl::convertsToAtLeast(o3tl::roundAway(ev->u.dval), SAL_MIN_INT16)
172 && o3tl::convertsToAtMost(o3tl::roundAway(ev->u.dval), SAL_MAX_INT16)))
174 return false;
176 auto tmp = static_cast<sal_Int16>(ev->u.dval);
177 ev->u.sval = tmp;
178 ev->et = ET_short;
179 return true;
181 case ET_byte:
183 auto tmp = static_cast<sal_Int16>(ev->u.byval);
184 ev->u.sval = tmp;
185 ev->et = ET_short;
186 return true;
188 default:
189 OSL_ASSERT(false);
190 return false;
192 case ET_ushort:
193 switch (ev->et)
195 case ET_short:
197 if (ev->u.sval < 0)
198 return false;
199 auto tmp = static_cast<sal_uInt16>(ev->u.sval);
200 ev->u.usval = tmp;
201 ev->et = ET_ushort;
202 return true;
204 case ET_ushort:
205 return true;
206 case ET_long:
208 if (ev->u.lval < 0 || ev->u.lval > SAL_MAX_UINT16)
209 return false;
210 auto tmp = static_cast<sal_uInt16>(ev->u.lval);
211 ev->u.usval = tmp;
212 ev->et = ET_ushort;
213 return true;
215 case ET_ulong:
217 if (ev->u.ulval > SAL_MAX_UINT16)
218 return false;
219 auto tmp = static_cast<sal_uInt16>(ev->u.ulval);
220 ev->u.usval = tmp;
221 ev->et = ET_ushort;
222 return true;
224 case ET_hyper:
226 if (ev->u.hval < 0 || ev->u.hval > SAL_MAX_UINT16)
227 return false;
228 auto tmp = static_cast<sal_uInt16>(ev->u.hval);
229 ev->u.usval = tmp;
230 ev->et = ET_ushort;
231 return true;
233 case ET_uhyper:
235 if (ev->u.uhval > SAL_MAX_UINT16)
236 return false;
237 auto tmp = static_cast<sal_uInt16>(ev->u.uhval);
238 ev->u.usval = tmp;
239 ev->et = ET_ushort;
240 return true;
242 case ET_boolean:
244 auto tmp = static_cast<sal_uInt16>(ev->u.bval);
245 ev->u.usval = tmp;
246 ev->et = ET_short;
247 return true;
249 case ET_float:
251 if (ev->u.fval < 0.0
252 || !o3tl::convertsToAtMost(o3tl::roundAway(ev->u.fval), SAL_MAX_UINT16))
254 return false;
256 auto tmp = static_cast<sal_uInt16>(ev->u.fval);
257 ev->u.usval = tmp;
258 ev->et = ET_short;
259 return true;
261 case ET_double:
263 if (ev->u.dval < 0.0
264 || !o3tl::convertsToAtMost(o3tl::roundAway(ev->u.dval), SAL_MAX_UINT16))
266 return false;
268 auto tmp = static_cast<sal_uInt16>(ev->u.dval);
269 ev->u.usval = tmp;
270 ev->et = ET_short;
271 return true;
273 case ET_byte:
275 auto tmp = static_cast<sal_uInt16>(ev->u.byval);
276 ev->u.usval = tmp;
277 ev->et = ET_ushort;
278 return true;
280 default:
281 OSL_ASSERT(false);
282 return false;
284 case ET_long:
285 switch (ev->et)
287 case ET_short:
289 auto tmp = static_cast<sal_Int32>(ev->u.sval);
290 ev->u.lval = tmp;
291 ev->et = ET_long;
292 return true;
294 case ET_ushort:
296 auto tmp = static_cast<sal_Int32>(ev->u.usval);
297 ev->u.lval = tmp;
298 ev->et = ET_long;
299 return true;
301 case ET_long:
302 return true;
303 case ET_ulong:
305 if (ev->u.ulval > SAL_MAX_INT32)
306 return false;
307 auto tmp = static_cast<sal_Int32>(ev->u.ulval);
308 ev->u.lval = tmp;
309 ev->et = ET_long;
310 return true;
312 case ET_hyper:
314 if (ev->u.hval < SAL_MIN_INT32 || ev->u.hval > SAL_MAX_INT32)
315 return false;
316 auto tmp = static_cast<sal_Int32>(ev->u.hval);
317 ev->u.lval = tmp;
318 ev->et = ET_long;
319 return true;
321 case ET_uhyper:
323 if (ev->u.uhval > SAL_MAX_INT32)
324 return false;
325 auto tmp = static_cast<sal_Int32>(ev->u.uhval);
326 ev->u.lval = tmp;
327 ev->et = ET_long;
328 return true;
330 case ET_boolean:
332 auto tmp = static_cast<sal_Int32>(ev->u.bval);
333 ev->u.lval = tmp;
334 ev->et = ET_long;
335 return true;
337 case ET_float:
339 if (!(o3tl::convertsToAtLeast(o3tl::roundAway(ev->u.fval), SAL_MIN_INT32)
340 && o3tl::convertsToAtMost(o3tl::roundAway(ev->u.fval), SAL_MAX_INT32)))
342 return false;
344 auto tmp = static_cast<sal_Int32>(ev->u.fval);
345 ev->u.lval = tmp;
346 ev->et = ET_long;
347 return true;
349 case ET_double:
351 if (!(o3tl::convertsToAtLeast(o3tl::roundAway(ev->u.dval), SAL_MIN_INT32)
352 && o3tl::convertsToAtMost(o3tl::roundAway(ev->u.dval), SAL_MAX_INT32)))
354 return false;
356 auto tmp = static_cast<sal_Int32>(ev->u.dval);
357 ev->u.lval = tmp;
358 ev->et = ET_long;
359 return true;
361 case ET_byte:
363 auto tmp = static_cast<sal_Int32>(ev->u.byval);
364 ev->u.lval = tmp;
365 ev->et = ET_long;
366 return true;
368 default:
369 OSL_ASSERT(false);
370 return false;
372 case ET_ulong:
373 switch (ev->et)
375 case ET_short:
377 if (ev->u.sval < 0)
378 return false;
379 auto tmp = static_cast<sal_uInt32>(ev->u.sval);
380 ev->u.ulval = tmp;
381 ev->et = ET_ulong;
382 return true;
384 case ET_ushort:
386 auto tmp = static_cast<sal_uInt32>(ev->u.usval);
387 ev->u.ulval = tmp;
388 ev->et = ET_ulong;
389 return true;
391 case ET_long:
393 if (ev->u.lval < 0)
394 return false;
395 auto tmp = static_cast<sal_uInt32>(ev->u.lval);
396 ev->u.ulval = tmp;
397 ev->et = ET_ulong;
398 return true;
400 case ET_ulong:
401 return true;
402 case ET_hyper:
404 if (ev->u.hval < 0 || ev->u.hval > SAL_MAX_UINT32)
405 return false;
406 auto tmp = static_cast<sal_uInt32>(ev->u.hval);
407 ev->u.lval = tmp;
408 ev->et = ET_ulong;
409 return true;
411 case ET_uhyper:
413 if (ev->u.uhval > SAL_MAX_UINT32)
414 return false;
415 auto tmp = static_cast<sal_uInt32>(ev->u.uhval);
416 ev->u.ulval = tmp;
417 ev->et = ET_ulong;
418 return true;
420 case ET_boolean:
422 auto tmp = static_cast<sal_uInt32>(ev->u.bval);
423 ev->u.ulval = tmp;
424 ev->et = ET_ulong;
425 return true;
427 case ET_float:
429 if (ev->u.fval < 0.0
430 || !o3tl::convertsToAtMost(o3tl::roundAway(ev->u.fval), SAL_MAX_UINT32))
432 return false;
434 auto tmp = static_cast<sal_uInt32>(ev->u.fval);
435 ev->u.ulval = tmp;
436 ev->et = ET_ulong;
437 return true;
439 case ET_double:
441 if (ev->u.dval < 0.0
442 || !o3tl::convertsToAtMost(o3tl::roundAway(ev->u.dval), SAL_MAX_UINT32))
444 return false;
446 auto tmp = static_cast<sal_uInt32>(ev->u.dval);
447 ev->u.ulval = tmp;
448 ev->et = ET_ulong;
449 return true;
451 case ET_byte:
453 auto tmp = static_cast<sal_uInt32>(ev->u.byval);
454 ev->u.ulval = tmp;
455 ev->et = ET_ulong;
456 return true;
458 default:
459 OSL_ASSERT(false);
460 return false;
462 case ET_hyper:
463 switch (ev->et)
465 case ET_short:
467 auto tmp = static_cast<sal_Int64>(ev->u.sval);
468 ev->u.hval = tmp;
469 ev->et = ET_hyper;
470 return true;
472 case ET_ushort:
474 auto tmp = static_cast<sal_Int64>(ev->u.usval);
475 ev->u.hval = tmp;
476 ev->et = ET_hyper;
477 return true;
479 case ET_long:
481 auto tmp = static_cast<sal_Int64>(ev->u.lval);
482 ev->u.hval = tmp;
483 ev->et = ET_hyper;
484 return true;
486 case ET_ulong:
488 auto tmp = static_cast<sal_Int64>(ev->u.ulval);
489 ev->u.hval = tmp;
490 ev->et = ET_hyper;
491 return true;
493 case ET_hyper:
494 return true;
495 case ET_uhyper:
497 if (ev->u.uhval > SAL_MAX_INT64)
498 return false;
499 auto tmp = static_cast<sal_Int64>(ev->u.uhval);
500 ev->u.hval = tmp;
501 ev->et = ET_long;
502 return true;
504 case ET_boolean:
506 auto tmp = static_cast<sal_Int64>(ev->u.bval);
507 ev->u.hval = tmp;
508 ev->et = ET_hyper;
509 return true;
511 case ET_float:
513 if (!(o3tl::convertsToAtLeast(o3tl::roundAway(ev->u.fval), SAL_MIN_INT64)
514 && o3tl::convertsToAtMost(o3tl::roundAway(ev->u.fval), SAL_MAX_INT64)))
516 return false;
518 auto tmp = static_cast<sal_Int64>(ev->u.fval);
519 ev->u.hval = tmp;
520 ev->et = ET_hyper;
521 return true;
523 case ET_double:
525 if (!(o3tl::convertsToAtLeast(o3tl::roundAway(ev->u.dval), SAL_MIN_INT64)
526 && o3tl::convertsToAtMost(o3tl::roundAway(ev->u.dval), SAL_MAX_INT64)))
528 return false;
530 auto tmp = static_cast<sal_Int64>(ev->u.dval);
531 ev->u.hval = tmp;
532 ev->et = ET_hyper;
533 return true;
535 case ET_byte:
537 auto tmp = static_cast<sal_Int64>(ev->u.byval);
538 ev->u.hval = tmp;
539 ev->et = ET_hyper;
540 return true;
542 default:
543 OSL_ASSERT(false);
544 return false;
546 case ET_uhyper:
547 switch (ev->et)
549 case ET_short:
551 if (ev->u.sval < 0)
552 return false;
553 auto tmp = static_cast<sal_uInt64>(ev->u.sval);
554 ev->u.uhval = tmp;
555 ev->et = ET_uhyper;
556 return true;
558 case ET_ushort:
560 auto tmp = static_cast<sal_uInt64>(ev->u.usval);
561 ev->u.uhval = tmp;
562 ev->et = ET_uhyper;
563 return true;
565 case ET_long:
567 if (ev->u.lval < 0)
568 return false;
569 auto tmp = static_cast<sal_uInt64>(ev->u.lval);
570 ev->u.uhval = tmp;
571 ev->et = ET_uhyper;
572 return true;
574 case ET_ulong:
576 auto tmp = static_cast<sal_uInt64>(ev->u.ulval);
577 ev->u.uhval = tmp;
578 ev->et = ET_uhyper;
579 return true;
581 case ET_hyper:
583 if (ev->u.hval < 0)
584 return false;
585 auto tmp = static_cast<sal_uInt64>(ev->u.hval);
586 ev->u.uhval = tmp;
587 ev->et = ET_uhyper;
588 return true;
590 case ET_uhyper:
591 return true;
592 case ET_boolean:
594 auto tmp = static_cast<sal_uInt64>(ev->u.bval);
595 ev->u.uhval = tmp;
596 ev->et = ET_uhyper;
597 return true;
599 case ET_float:
601 if (ev->u.fval < 0.0
602 || !o3tl::convertsToAtMost(o3tl::roundAway(ev->u.fval), SAL_MAX_UINT64))
604 return false;
606 auto tmp = static_cast<sal_uInt64>(ev->u.fval);
607 ev->u.uhval = tmp;
608 ev->et = ET_uhyper;
609 return true;
611 case ET_double:
613 if (ev->u.dval < 0.0
614 || !o3tl::convertsToAtMost(o3tl::roundAway(ev->u.dval), SAL_MAX_UINT64))
616 return false;
618 auto tmp = static_cast<sal_uInt64>(ev->u.dval);
619 ev->u.uhval = tmp;
620 ev->et = ET_uhyper;
621 return true;
623 case ET_byte:
625 auto tmp = static_cast<sal_uInt64>(ev->u.byval);
626 ev->u.uhval = tmp;
627 ev->et = ET_uhyper;
628 return true;
630 default:
631 OSL_ASSERT(false);
632 return false;
634 case ET_boolean:
635 switch (ev->et)
637 case ET_short:
638 ev->u.bval = ev->u.sval != 0;
639 ev->et = ET_boolean;
640 return true;
641 case ET_ushort:
642 ev->u.bval = ev->u.usval != 0;
643 ev->et = ET_boolean;
644 return true;
645 case ET_long:
646 ev->u.bval = ev->u.lval != 0;
647 ev->et = ET_boolean;
648 return true;
649 case ET_ulong:
650 ev->u.bval = ev->u.ulval != 0;
651 ev->et = ET_boolean;
652 return true;
653 case ET_hyper:
654 ev->u.bval = ev->u.hval != 0;
655 ev->et = ET_boolean;
656 return true;
657 case ET_uhyper:
658 ev->u.bval = ev->u.uhval != 0;
659 ev->et = ET_boolean;
660 return true;
661 case ET_boolean:
662 return true;
663 case ET_float:
664 ev->u.bval = ev->u.fval != 0.0;
665 ev->et = ET_boolean;
666 return true;
667 case ET_double:
668 ev->u.bval = ev->u.dval != 0.0;
669 ev->et = ET_boolean;
670 return true;
671 case ET_byte:
672 ev->u.bval = ev->u.byval != 0;
673 ev->et = ET_boolean;
674 return true;
675 default:
676 OSL_ASSERT(false);
677 return false;
679 case ET_float:
680 switch (ev->et)
682 case ET_short:
684 auto tmp = static_cast<float>(ev->u.sval);
685 ev->u.fval = tmp;
686 ev->et = ET_float;
687 return true;
689 case ET_ushort:
691 auto tmp = static_cast<float>(ev->u.usval);
692 ev->u.fval = tmp;
693 ev->et = ET_float;
694 return true;
696 case ET_long:
698 auto tmp = static_cast<float>(ev->u.lval);
699 ev->u.fval = tmp;
700 ev->et = ET_float;
701 return true;
703 case ET_ulong:
705 auto tmp = static_cast<float>(ev->u.ulval);
706 ev->u.fval = tmp;
707 ev->et = ET_float;
708 return true;
710 case ET_hyper:
712 auto tmp = static_cast<float>(ev->u.hval);
713 ev->u.fval = tmp;
714 ev->et = ET_float;
715 return true;
717 case ET_uhyper:
719 if (static_cast<float>(ev->u.ulval) > FLT_MAX)
720 return false;
721 auto tmp = static_cast<float>(ev->u.ulval);
722 ev->u.fval = tmp;
723 ev->et = ET_float;
724 return true;
726 case ET_boolean:
727 ev->u.fval = ev->u.bval ? 1.0f : 0.0f;
728 ev->et = ET_float;
729 return true;
730 case ET_float:
731 return true;
732 case ET_double:
734 if (static_cast<float>(ev->u.dval) > FLT_MAX
735 || static_cast<float>(ev->u.dval) < -FLT_MAX)
736 return false;
737 auto tmp = static_cast<float>(ev->u.dval);
738 ev->u.fval = tmp;
739 ev->et = ET_float;
740 return true;
742 case ET_byte:
744 auto tmp = static_cast<float>(ev->u.byval);
745 ev->u.fval = tmp;
746 ev->et = ET_float;
747 return true;
749 default:
750 OSL_ASSERT(false);
751 return false;
753 case ET_double:
754 switch (ev->et)
756 case ET_short:
758 auto tmp = static_cast<double>(ev->u.sval);
759 ev->u.dval = tmp;
760 ev->et = ET_double;
761 return true;
763 case ET_ushort:
765 auto tmp = static_cast<double>(ev->u.usval);
766 ev->u.dval = tmp;
767 ev->et = ET_double;
768 return true;
770 case ET_long:
772 auto tmp = static_cast<double>(ev->u.lval);
773 ev->u.dval = tmp;
774 ev->et = ET_double;
775 return true;
777 case ET_ulong:
779 auto tmp = static_cast<double>(ev->u.ulval);
780 ev->u.dval = tmp;
781 ev->et = ET_double;
782 return true;
784 case ET_hyper:
786 auto tmp = static_cast<double>(ev->u.hval);
787 ev->u.dval = tmp;
788 ev->et = ET_double;
789 return true;
791 case ET_uhyper:
793 if (ev->u.dval > FLT_MAX || ev->u.dval < -FLT_MAX)
794 return false;
795 auto tmp = static_cast<double>(ev->u.ulval);
796 ev->u.dval = tmp;
797 ev->et = ET_double;
798 return true;
800 case ET_boolean:
801 ev->u.dval = ev->u.bval ? 1.0 : 0.0;
802 ev->et = ET_double;
803 return true;
804 case ET_float:
806 auto tmp = static_cast<double>(ev->u.fval);
807 ev->u.dval = tmp;
808 ev->et = ET_double;
809 return true;
811 case ET_double:
812 return true;
813 case ET_byte:
815 auto tmp = static_cast<double>(ev->u.byval);
816 ev->u.dval = tmp;
817 ev->et = ET_double;
818 return true;
820 default:
821 OSL_ASSERT(false);
822 return false;
824 case ET_byte:
825 switch (ev->et)
827 case ET_short:
829 if (ev->u.sval < SAL_MIN_INT8 || ev->u.sval > SAL_MAX_UINT8)
830 return false;
831 auto tmp = static_cast<unsigned char>(ev->u.sval);
832 ev->u.byval = tmp;
833 ev->et = ET_byte;
834 return true;
836 case ET_ushort:
838 if (ev->u.usval > SAL_MAX_UINT8)
839 return false;
840 auto tmp = static_cast<unsigned char>(ev->u.usval);
841 ev->u.byval = tmp;
842 ev->et = ET_byte;
843 return true;
845 case ET_long:
847 if (ev->u.lval < SAL_MIN_INT8 || ev->u.lval > SAL_MAX_UINT8)
848 return false;
849 auto tmp = static_cast<unsigned char>(ev->u.lval);
850 ev->u.byval = tmp;
851 ev->et = ET_byte;
852 return true;
854 case ET_ulong:
856 if (ev->u.ulval > SAL_MAX_UINT8)
857 return false;
858 auto tmp = static_cast<unsigned char>(ev->u.ulval);
859 ev->u.byval = tmp;
860 ev->et = ET_byte;
861 return true;
863 case ET_hyper:
865 if (ev->u.hval < SAL_MIN_INT8 || ev->u.hval > SAL_MAX_UINT8)
866 return false;
867 auto tmp = static_cast<unsigned char>(ev->u.hval);
868 ev->u.byval = tmp;
869 ev->et = ET_byte;
870 return true;
872 case ET_uhyper:
874 if (ev->u.uhval > SAL_MAX_UINT8)
875 return false;
876 auto tmp = static_cast<unsigned char>(ev->u.uhval);
877 ev->u.byval = tmp;
878 ev->et = ET_byte;
879 return true;
881 case ET_boolean:
882 ev->u.byval = ev->u.bval ? 1 : 0;
883 ev->et = ET_byte;
884 return true;
885 case ET_float:
887 if (!(o3tl::convertsToAtLeast(o3tl::roundAway(ev->u.fval), SAL_MIN_INT8)
888 && o3tl::convertsToAtMost(o3tl::roundAway(ev->u.fval), SAL_MAX_UINT8)))
890 return false;
892 auto tmp = static_cast<unsigned char>(static_cast<sal_Int32>(ev->u.fval));
893 ev->u.byval = tmp;
894 ev->et = ET_byte;
895 return true;
897 case ET_double:
899 if (!(o3tl::convertsToAtLeast(o3tl::roundAway(ev->u.dval), SAL_MIN_INT8)
900 && o3tl::convertsToAtMost(o3tl::roundAway(ev->u.dval), SAL_MAX_UINT8)))
902 return false;
904 auto tmp = static_cast<unsigned char>(static_cast<sal_Int32>(ev->u.dval));
905 ev->u.byval = tmp;
906 ev->et = ET_byte;
907 return true;
909 case ET_byte:
910 return true;
911 default:
912 OSL_ASSERT(false);
913 return false;
915 default:
916 OSL_ASSERT(false);
917 return false;
921 bool AstExpression::coerce(ExprType t)
924 * Is it already of the right type?
926 if (m_exprValue != nullptr && m_exprValue->et == t)
927 return true;
929 * OK, must coerce
931 * First, evaluate it, then try to coerce result type
932 * If already evaluated, return the result
934 evaluate();
935 if (m_exprValue == nullptr)
936 return false;
938 if (!coerce_value(m_exprValue.get(), t))
939 m_exprValue.reset();
941 return m_exprValue != nullptr;
944 bool AstExpression::compareLong(AstExpression* pExpr)
946 bool bRet = false;
947 if (m_combOperator != pExpr->m_combOperator)
948 return bRet;
949 evaluate();
950 pExpr->evaluate();
951 if (m_exprValue == nullptr || pExpr->getExprValue() == nullptr)
952 return bRet;
953 if (m_exprValue->et != pExpr->getExprValue()->et)
954 return bRet;
955 switch (m_exprValue->et)
957 case ET_long:
958 bRet = m_exprValue->u.lval == pExpr->getExprValue()->u.lval;
959 break;
960 default:
961 OSL_ASSERT(false);
962 bRet = false;
963 break;
965 return bRet;
968 void AstExpression::evaluate()
971 * Already evaluated?
973 if (m_exprValue != nullptr)
974 return;
976 * OK, must evaluate operator
978 switch (m_combOperator)
980 case ExprComb::Add:
981 case ExprComb::Minus:
982 case ExprComb::Mul:
983 case ExprComb::Div:
984 case ExprComb::Mod:
985 m_exprValue = eval_bin_op();
986 break;
987 case ExprComb::Or:
988 case ExprComb::Xor:
989 case ExprComb::And:
990 case ExprComb::Left:
991 case ExprComb::Right:
992 m_exprValue = eval_bit_op();
993 break;
994 case ExprComb::UPlus:
995 case ExprComb::UMinus:
996 m_exprValue = eval_un_op();
997 break;
998 case ExprComb::Symbol:
999 m_exprValue = eval_symbol();
1000 break;
1001 case ExprComb::NONE:
1002 break;
1006 std::unique_ptr<AstExprValue> AstExpression::eval_bin_op()
1008 ExprType eType = ET_double;
1010 if (m_combOperator == ExprComb::Mod)
1011 eType = ET_hyper;
1013 if (m_subExpr1 == nullptr || m_subExpr2 == nullptr)
1014 return nullptr;
1015 m_subExpr1->evaluate();
1016 if (m_subExpr1->getExprValue() == nullptr)
1017 return nullptr;
1018 if (!m_subExpr1->coerce(eType))
1019 return nullptr;
1020 m_subExpr2->evaluate();
1021 if (m_subExpr2->getExprValue() == nullptr)
1022 return nullptr;
1023 if (!m_subExpr2->coerce(eType))
1024 return nullptr;
1026 std::unique_ptr<AstExprValue> retval(new AstExprValue);
1027 retval->et = eType;
1029 switch (m_combOperator)
1031 case ExprComb::Mod:
1032 if (m_subExpr2->getExprValue()->u.hval == 0)
1033 return nullptr;
1034 retval->u.hval
1035 = m_subExpr1->getExprValue()->u.hval % m_subExpr2->getExprValue()->u.hval;
1036 break;
1037 case ExprComb::Add:
1038 retval->u.dval
1039 = m_subExpr1->getExprValue()->u.dval + m_subExpr2->getExprValue()->u.dval;
1040 break;
1041 case ExprComb::Minus:
1042 retval->u.dval
1043 = m_subExpr1->getExprValue()->u.dval - m_subExpr2->getExprValue()->u.dval;
1044 break;
1045 case ExprComb::Mul:
1046 retval->u.dval
1047 = m_subExpr1->getExprValue()->u.dval * m_subExpr2->getExprValue()->u.dval;
1048 break;
1049 case ExprComb::Div:
1050 if (m_subExpr2->getExprValue()->u.dval == 0.0)
1051 return nullptr;
1052 retval->u.dval
1053 = m_subExpr1->getExprValue()->u.dval / m_subExpr2->getExprValue()->u.dval;
1054 break;
1055 default:
1056 return nullptr;
1059 return retval;
1062 std::unique_ptr<AstExprValue> AstExpression::eval_bit_op()
1064 if (m_subExpr1 == nullptr || m_subExpr2 == nullptr)
1065 return nullptr;
1066 m_subExpr1->evaluate();
1067 if (m_subExpr1->getExprValue() == nullptr)
1068 return nullptr;
1069 if (!m_subExpr1->coerce(ET_long))
1070 return nullptr;
1071 m_subExpr2->evaluate();
1072 if (m_subExpr2->getExprValue() == nullptr)
1073 return nullptr;
1074 if (!m_subExpr2->coerce(ET_long))
1075 return nullptr;
1077 std::unique_ptr<AstExprValue> retval(new AstExprValue);
1078 retval->et = ET_long;
1080 switch (m_combOperator)
1082 case ExprComb::Or:
1083 retval->u.lval
1084 = m_subExpr1->getExprValue()->u.lval | m_subExpr2->getExprValue()->u.lval;
1085 break;
1086 case ExprComb::Xor:
1087 retval->u.lval
1088 = m_subExpr1->getExprValue()->u.lval ^ m_subExpr2->getExprValue()->u.lval;
1089 break;
1090 case ExprComb::And:
1091 retval->u.lval
1092 = m_subExpr1->getExprValue()->u.lval & m_subExpr2->getExprValue()->u.lval;
1093 break;
1094 case ExprComb::Left:
1095 retval->u.lval = m_subExpr1->getExprValue()->u.lval
1096 << m_subExpr2->getExprValue()->u.lval;
1097 break;
1098 case ExprComb::Right:
1099 retval->u.lval
1100 = m_subExpr1->getExprValue()->u.lval >> m_subExpr2->getExprValue()->u.lval;
1101 break;
1102 default:
1103 return nullptr;
1106 return retval;
1109 std::unique_ptr<AstExprValue> AstExpression::eval_un_op()
1111 if (m_subExpr1 == nullptr)
1112 return nullptr;
1113 m_subExpr1->evaluate();
1114 if (m_subExpr1->getExprValue() == nullptr)
1115 return nullptr;
1116 if (!m_subExpr1->coerce(ET_double))
1117 return nullptr;
1119 std::unique_ptr<AstExprValue> retval(new AstExprValue);
1120 retval->et = ET_double;
1122 switch (m_combOperator)
1124 case ExprComb::UPlus:
1125 retval->u.lval = m_subExpr1->getExprValue()->u.lval;
1126 break;
1127 case ExprComb::UMinus:
1128 retval->u.lval = -(m_subExpr1->getExprValue()->u.lval);
1129 break;
1130 default:
1131 return nullptr;
1134 return retval;
1137 std::unique_ptr<AstExprValue> AstExpression::eval_symbol()
1139 AstScope* pScope = nullptr;
1140 AstDeclaration* pDecl;
1141 AstConstant* pConst;
1144 * Is there a symbol stored?
1146 if (!m_xSymbolicName)
1148 ErrorHandler::evalError(this);
1149 return nullptr;
1152 * Get current scope for lookup
1154 if (idlc()->scopes()->depth() > 0)
1155 pScope = idlc()->scopes()->topNonNull();
1156 if (!pScope)
1158 ErrorHandler::lookupError(*m_xSymbolicName);
1159 return nullptr;
1162 * Do lookup
1164 pDecl = pScope->lookupByName(*m_xSymbolicName);
1165 if (pDecl == nullptr)
1167 ErrorHandler::lookupError(*m_xSymbolicName);
1168 return nullptr;
1171 * Is it a constant?
1173 if (pDecl->getNodeType() != NT_const && pDecl->getNodeType() != NT_enum_val)
1175 ErrorHandler::constantExpected(pDecl, *m_xSymbolicName);
1176 return nullptr;
1178 if (!ErrorHandler::checkPublished(pDecl))
1180 return nullptr;
1183 * OK, now evaluate the constant we just got, to produce its value
1185 pConst = static_cast<AstConstant*>(pDecl);
1186 pConst->getConstValue()->evaluate();
1187 auto const val = pConst->getConstValue()->getExprValue();
1188 return val == nullptr ? nullptr : std::make_unique<AstExprValue>(*val);
1191 OString AstExpression::toString()
1193 OString exprStr;
1194 if (m_combOperator == ExprComb::Symbol)
1195 return m_xSymbolicName ? *m_xSymbolicName : OString("<Undefined Name>");
1197 if (m_exprValue)
1199 switch (m_exprValue->et)
1201 case ET_short:
1202 return OString::number(m_exprValue->u.sval);
1203 case ET_ushort:
1204 return OString::number(m_exprValue->u.usval);
1205 case ET_long:
1206 return OString::number(m_exprValue->u.lval);
1207 case ET_ulong:
1208 return OString::number(m_exprValue->u.ulval);
1209 case ET_hyper:
1210 return OString::number(m_exprValue->u.hval);
1211 case ET_uhyper:
1212 return OString::number(m_exprValue->u.uhval);
1213 case ET_float:
1214 return OString::number(m_exprValue->u.fval);
1215 case ET_double:
1216 return OString::number(m_exprValue->u.dval);
1217 case ET_byte:
1218 return OString::number(m_exprValue->u.byval);
1219 case ET_boolean:
1220 if (m_exprValue->u.lval == 0)
1221 return "FALSE";
1222 else
1223 return "TRUE";
1224 default:
1225 OSL_ASSERT(false);
1226 return OString();
1230 switch (m_combOperator)
1232 case ExprComb::UPlus:
1233 exprStr += "+";
1234 break;
1235 case ExprComb::UMinus:
1236 exprStr += "-";
1237 break;
1238 default:
1239 break;
1241 if (m_subExpr1)
1242 exprStr += m_subExpr1->toString();
1243 switch (m_combOperator)
1245 case ExprComb::Add:
1246 exprStr += " + ";
1247 break;
1248 case ExprComb::Minus:
1249 exprStr += " - ";
1250 break;
1251 case ExprComb::Mul:
1252 exprStr += " * ";
1253 break;
1254 case ExprComb::Div:
1255 exprStr += " / ";
1256 break;
1257 case ExprComb::Mod:
1258 exprStr += " % ";
1259 break;
1260 case ExprComb::Or:
1261 exprStr += " | ";
1262 break;
1263 case ExprComb::Xor:
1264 exprStr += " ^ ";
1265 break;
1266 case ExprComb::And:
1267 exprStr += " & ";
1268 break;
1269 case ExprComb::Left:
1270 exprStr += " << ";
1271 break;
1272 case ExprComb::Right:
1273 exprStr += " >> ";
1274 break;
1275 default:
1276 break;
1279 if (m_subExpr2)
1280 exprStr += m_subExpr2->toString();
1282 return exprStr;
1285 // Convert the type of an AST_Expression to a char *
1286 const char* exprTypeToString(ExprType t)
1288 switch (t)
1290 case ET_short:
1291 return "short";
1292 case ET_ushort:
1293 return "unsigned short";
1294 case ET_long:
1295 return "long";
1296 case ET_ulong:
1297 return "unsigned long";
1298 case ET_hyper:
1299 return "hyper";
1300 case ET_uhyper:
1301 return "unsigned hyper";
1302 case ET_float:
1303 return "float";
1304 case ET_double:
1305 return "double";
1306 case ET_char:
1307 return "char";
1308 case ET_byte:
1309 return "byte";
1310 case ET_boolean:
1311 return "boolean";
1312 case ET_string:
1313 return "string";
1314 case ET_any:
1315 return "any";
1316 case ET_type:
1317 return "type";
1318 case ET_void:
1319 return "void";
1320 case ET_none:
1321 return "none";
1324 return "unknown";
1327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */