bump product version to 6.3.0.0.beta1
[LibreOffice.git] / idlc / source / astexpression.cxx
blob48963d43a0ac651eebc49cf826c5a677aeafd126
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 .
21 #include <astexpression.hxx>
22 #include <astconstant.hxx>
23 #include <astscope.hxx>
24 #include <errorhandler.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()
91 * Perform the coercion from the given AstExprValue to the requested
92 * ExprType. Return an AstExprValue if successful, NULL if failed.
93 * must be done for hyper, uhyper
95 static bool
96 coerce_value(AstExprValue *ev, ExprType t)
98 if (ev == nullptr)
99 return false;
101 switch (t)
103 case ET_short:
104 switch (ev->et)
106 case ET_short:
107 return true;
108 case ET_ushort:
109 if (ev->u.usval > SAL_MAX_INT16)
110 return false;
111 ev->u.sval = static_cast<sal_Int16>(ev->u.usval);
112 ev->et = ET_short;
113 return true;
114 case ET_long:
115 if (ev->u.lval < SAL_MIN_INT16 || ev->u.lval > SAL_MAX_INT16)
116 return false;
117 ev->u.sval = static_cast<sal_Int16>(ev->u.lval);
118 ev->et = ET_short;
119 return true;
120 case ET_ulong:
121 if (ev->u.ulval > SAL_MAX_INT16)
122 return false;
123 ev->u.sval = static_cast<sal_Int16>(ev->u.ulval);
124 ev->et = ET_short;
125 return true;
126 case ET_hyper:
127 if (ev->u.hval < SAL_MIN_INT16 || ev->u.hval > SAL_MAX_INT16)
128 return false;
129 ev->u.sval = static_cast<sal_Int16>(ev->u.hval);
130 ev->et = ET_short;
131 return true;
132 case ET_uhyper:
133 if (ev->u.uhval > SAL_MAX_INT16)
134 return false;
135 ev->u.sval = static_cast<sal_Int16>(ev->u.uhval);
136 ev->et = ET_short;
137 return true;
138 case ET_boolean:
139 ev->u.sval = static_cast<sal_Int16>(ev->u.bval);
140 ev->et = ET_short;
141 return true;
142 case ET_float:
143 if (ev->u.fval < SAL_MIN_INT16 || ev->u.fval > SAL_MAX_INT16)
144 return false;
145 ev->u.sval = static_cast<sal_Int16>(ev->u.fval);
146 ev->et = ET_short;
147 return true;
148 case ET_double:
149 if (ev->u.dval < SAL_MIN_INT16 || ev->u.dval > SAL_MAX_INT16)
150 return false;
151 ev->u.sval = static_cast<sal_Int16>(ev->u.dval);
152 ev->et = ET_short;
153 return true;
154 case ET_byte:
155 ev->u.sval = static_cast<sal_Int16>(ev->u.byval);
156 ev->et = ET_short;
157 return true;
158 default:
159 OSL_ASSERT(false);
160 return false;
162 case ET_ushort:
163 switch (ev->et)
165 case ET_short:
166 if (ev->u.sval < 0)
167 return false;
168 ev->u.usval = static_cast<sal_uInt16>(ev->u.sval);
169 ev->et = ET_ushort;
170 return true;
171 case ET_ushort:
172 return true;
173 case ET_long:
174 if (ev->u.lval < 0 || ev->u.lval > SAL_MAX_UINT16)
175 return false;
176 ev->u.usval = static_cast<sal_uInt16>(ev->u.lval);
177 ev->et = ET_ushort;
178 return true;
179 case ET_ulong:
180 if (ev->u.ulval > SAL_MAX_UINT16)
181 return false;
182 ev->u.usval = static_cast<sal_uInt16>(ev->u.ulval);
183 ev->et = ET_ushort;
184 return true;
185 case ET_hyper:
186 if (ev->u.hval < 0 || ev->u.hval > SAL_MAX_UINT16)
187 return false;
188 ev->u.usval = static_cast<sal_uInt16>(ev->u.hval);
189 ev->et = ET_ushort;
190 return true;
191 case ET_uhyper:
192 if (ev->u.uhval > SAL_MAX_UINT16)
193 return false;
194 ev->u.usval = static_cast<sal_uInt16>(ev->u.uhval);
195 ev->et = ET_ushort;
196 return true;
197 case ET_boolean:
198 ev->u.usval = static_cast<sal_uInt16>(ev->u.bval);
199 ev->et = ET_short;
200 return true;
201 case ET_float:
202 if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT16)
203 return false;
204 ev->u.usval = static_cast<sal_uInt16>(ev->u.fval);
205 ev->et = ET_short;
206 return true;
207 case ET_double:
208 if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT16)
209 return false;
210 ev->u.usval = static_cast<sal_uInt16>(ev->u.dval);
211 ev->et = ET_short;
212 return true;
213 case ET_byte:
214 ev->u.usval = static_cast<sal_uInt16>(ev->u.byval);
215 ev->et = ET_ushort;
216 return true;
217 default:
218 OSL_ASSERT(false);
219 return false;
221 case ET_long:
222 switch (ev->et)
224 case ET_short:
225 ev->u.lval = static_cast<sal_Int32>(ev->u.sval);
226 ev->et = ET_long;
227 return true;
228 case ET_ushort:
229 ev->u.lval = static_cast<sal_Int32>(ev->u.usval);
230 ev->et = ET_long;
231 return true;
232 case ET_long:
233 return true;
234 case ET_ulong:
235 if (ev->u.ulval > SAL_MAX_INT32)
236 return false;
237 ev->u.lval = static_cast<sal_Int32>(ev->u.ulval);
238 ev->et = ET_long;
239 return true;
240 case ET_hyper:
241 if (ev->u.hval < SAL_MIN_INT32 || ev->u.hval > SAL_MAX_INT32)
242 return false;
243 ev->u.lval = static_cast<sal_Int32>(ev->u.hval);
244 ev->et = ET_long;
245 return true;
246 case ET_uhyper:
247 if (ev->u.uhval > SAL_MAX_INT32)
248 return false;
249 ev->u.lval = static_cast<sal_Int32>(ev->u.uhval);
250 ev->et = ET_long;
251 return true;
252 case ET_boolean:
253 ev->u.lval = static_cast<sal_Int32>(ev->u.bval);
254 ev->et = ET_long;
255 return true;
256 case ET_float:
257 if (ev->u.fval < SAL_MIN_INT32 || ev->u.fval > SAL_MAX_INT32)
258 return false;
259 ev->u.lval = static_cast<sal_Int32>(ev->u.fval);
260 ev->et = ET_long;
261 return true;
262 case ET_double:
263 if (ev->u.dval < SAL_MIN_INT32 || ev->u.dval > SAL_MAX_INT32)
264 return false;
265 ev->u.lval = static_cast<sal_Int32>(ev->u.dval);
266 ev->et = ET_long;
267 return true;
268 case ET_byte:
269 ev->u.lval = static_cast<sal_Int32>(ev->u.byval);
270 ev->et = ET_long;
271 return true;
272 default:
273 OSL_ASSERT(false);
274 return false;
276 case ET_ulong:
277 switch (ev->et)
279 case ET_short:
280 if (ev->u.sval < 0)
281 return false;
282 ev->u.ulval = static_cast<sal_uInt32>(ev->u.sval);
283 ev->et = ET_ulong;
284 return true;
285 case ET_ushort:
286 ev->u.ulval = static_cast<sal_uInt32>(ev->u.usval);
287 ev->et = ET_ulong;
288 return true;
289 case ET_long:
290 if (ev->u.lval < 0)
291 return false;
292 ev->u.ulval = static_cast<sal_uInt32>(ev->u.lval);
293 ev->et = ET_ulong;
294 return true;
295 case ET_ulong:
296 return true;
297 case ET_hyper:
298 if (ev->u.hval < 0 || ev->u.hval > SAL_MAX_UINT32)
299 return false;
300 ev->u.lval = static_cast<sal_uInt32>(ev->u.hval);
301 ev->et = ET_ulong;
302 return true;
303 case ET_uhyper:
304 if (ev->u.uhval > SAL_MAX_UINT32)
305 return false;
306 ev->u.ulval = static_cast<sal_uInt32>(ev->u.uhval);
307 ev->et = ET_ulong;
308 return true;
309 case ET_boolean:
310 ev->u.ulval = static_cast<sal_uInt32>(ev->u.bval);
311 ev->et = ET_ulong;
312 return true;
313 case ET_float:
314 if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT32)
315 return false;
316 ev->u.ulval = static_cast<sal_uInt32>(ev->u.fval);
317 ev->et = ET_ulong;
318 return true;
319 case ET_double:
320 if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT32)
321 return false;
322 ev->u.ulval = static_cast<sal_uInt32>(ev->u.dval);
323 ev->et = ET_ulong;
324 return true;
325 case ET_byte:
326 ev->u.ulval = static_cast<sal_uInt32>(ev->u.byval);
327 ev->et = ET_ulong;
328 return true;
329 default:
330 OSL_ASSERT(false);
331 return false;
333 case ET_hyper:
334 switch (ev->et)
336 case ET_short:
337 ev->u.hval = static_cast<sal_Int64>(ev->u.sval);
338 ev->et = ET_hyper;
339 return true;
340 case ET_ushort:
341 ev->u.hval = static_cast<sal_Int64>(ev->u.usval);
342 ev->et = ET_hyper;
343 return true;
344 case ET_long:
345 ev->u.hval = static_cast<sal_Int64>(ev->u.lval);
346 ev->et = ET_hyper;
347 return true;
348 case ET_ulong:
349 ev->u.hval = static_cast<sal_Int64>(ev->u.ulval);
350 ev->et = ET_hyper;
351 return true;
352 case ET_hyper:
353 return true;
354 case ET_uhyper:
355 if (ev->u.uhval > SAL_MAX_INT64)
356 return false;
357 ev->u.hval = static_cast<sal_Int64>(ev->u.uhval);
358 ev->et = ET_long;
359 return true;
360 case ET_boolean:
361 ev->u.hval = static_cast<sal_Int64>(ev->u.bval);
362 ev->et = ET_hyper;
363 return true;
364 case ET_float:
365 if (ev->u.fval < SAL_MIN_INT64 || ev->u.fval > SAL_MAX_INT64)
366 return false;
367 ev->u.hval = static_cast<sal_Int64>(ev->u.fval);
368 ev->et = ET_hyper;
369 return true;
370 case ET_double:
371 if (ev->u.dval < SAL_MIN_INT64 || ev->u.dval > SAL_MAX_INT64)
372 return false;
373 ev->u.hval = static_cast<sal_Int64>(ev->u.dval);
374 ev->et = ET_hyper;
375 return true;
376 case ET_byte:
377 ev->u.hval = static_cast<sal_Int64>(ev->u.byval);
378 ev->et = ET_hyper;
379 return true;
380 default:
381 OSL_ASSERT(false);
382 return false;
384 case ET_uhyper:
385 switch (ev->et)
387 case ET_short:
388 if (ev->u.sval < 0)
389 return false;
390 ev->u.uhval = static_cast<sal_uInt64>(ev->u.sval);
391 ev->et = ET_uhyper;
392 return true;
393 case ET_ushort:
394 ev->u.uhval = static_cast<sal_uInt64>(ev->u.usval);
395 ev->et = ET_uhyper;
396 return true;
397 case ET_long:
398 if (ev->u.lval < 0)
399 return false;
400 ev->u.uhval = static_cast<sal_uInt64>(ev->u.lval);
401 ev->et = ET_uhyper;
402 return true;
403 case ET_ulong:
404 ev->u.uhval = static_cast<sal_uInt64>(ev->u.ulval);
405 ev->et = ET_uhyper;
406 return true;
407 case ET_hyper:
408 if (ev->u.hval < 0)
409 return false;
410 ev->u.uhval = static_cast<sal_uInt64>(ev->u.hval);
411 ev->et = ET_uhyper;
412 return true;
413 case ET_uhyper:
414 return true;
415 case ET_boolean:
416 ev->u.uhval = static_cast<sal_uInt64>(ev->u.bval);
417 ev->et = ET_uhyper;
418 return true;
419 case ET_float:
420 if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT64)
421 return false;
422 ev->u.uhval = static_cast<sal_uInt64>(ev->u.fval);
423 ev->et = ET_uhyper;
424 return true;
425 case ET_double:
426 if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT64)
427 return false;
428 ev->u.uhval = static_cast<sal_uInt64>(ev->u.dval);
429 ev->et = ET_uhyper;
430 return true;
431 case ET_byte:
432 ev->u.uhval = static_cast<sal_uInt64>(ev->u.byval);
433 ev->et = ET_uhyper;
434 return true;
435 default:
436 OSL_ASSERT(false);
437 return false;
439 case ET_boolean:
440 switch (ev->et)
442 case ET_short:
443 ev->u.bval = ev->u.sval != 0;
444 ev->et = ET_boolean;
445 return true;
446 case ET_ushort:
447 ev->u.bval = ev->u.usval != 0;
448 ev->et = ET_boolean;
449 return true;
450 case ET_long:
451 ev->u.bval = ev->u.lval != 0;
452 ev->et = ET_boolean;
453 return true;
454 case ET_ulong:
455 ev->u.bval = ev->u.ulval != 0;
456 ev->et = ET_boolean;
457 return true;
458 case ET_hyper:
459 ev->u.bval = ev->u.hval != 0;
460 ev->et = ET_boolean;
461 return true;
462 case ET_uhyper:
463 ev->u.bval = ev->u.uhval != 0;
464 ev->et = ET_boolean;
465 return true;
466 case ET_boolean:
467 return true;
468 case ET_float:
469 ev->u.bval = ev->u.fval != 0.0;
470 ev->et = ET_boolean;
471 return true;
472 case ET_double:
473 ev->u.bval = ev->u.dval != 0.0;
474 ev->et = ET_boolean;
475 return true;
476 case ET_byte:
477 ev->u.bval = ev->u.byval != 0;
478 ev->et = ET_boolean;
479 return true;
480 default:
481 OSL_ASSERT(false);
482 return false;
484 case ET_float:
485 switch (ev->et)
487 case ET_short:
488 ev->u.fval = static_cast<float>(ev->u.sval);
489 ev->et = ET_float;
490 return true;
491 case ET_ushort:
492 ev->u.fval = static_cast<float>(ev->u.usval);
493 ev->et = ET_float;
494 return true;
495 case ET_long:
496 ev->u.fval = static_cast<float>(ev->u.lval);
497 ev->et = ET_float;
498 return true;
499 case ET_ulong:
500 ev->u.fval = static_cast<float>(ev->u.ulval);
501 ev->et = ET_float;
502 return true;
503 case ET_hyper:
504 ev->u.fval = static_cast<float>(ev->u.hval);
505 ev->et = ET_float;
506 return true;
507 case ET_uhyper:
508 if (static_cast<float>(ev->u.ulval) > FLT_MAX)
509 return false;
510 ev->u.fval = static_cast<float>(ev->u.ulval);
511 ev->et = ET_float;
512 return true;
513 case ET_boolean:
514 ev->u.fval = ev->u.bval ? 1.0f : 0.0f;
515 ev->et = ET_float;
516 return true;
517 case ET_float:
518 return true;
519 case ET_double:
520 if (static_cast<float>(ev->u.dval) > FLT_MAX || static_cast<float>(ev->u.dval) < -FLT_MAX)
521 return false;
522 ev->u.fval = static_cast<float>(ev->u.dval);
523 ev->et = ET_float;
524 return true;
525 case ET_byte:
526 ev->u.fval = static_cast<float>(ev->u.byval);
527 ev->et = ET_float;
528 return true;
529 default:
530 OSL_ASSERT(false);
531 return false;
533 case ET_double:
534 switch (ev->et)
536 case ET_short:
537 ev->u.dval = static_cast<double>(ev->u.sval);
538 ev->et = ET_double;
539 return true;
540 case ET_ushort:
541 ev->u.dval = static_cast<double>(ev->u.usval);
542 ev->et = ET_double;
543 return true;
544 case ET_long:
545 ev->u.dval = static_cast<double>(ev->u.lval);
546 ev->et = ET_double;
547 return true;
548 case ET_ulong:
549 ev->u.dval = static_cast<double>(ev->u.ulval);
550 ev->et = ET_double;
551 return true;
552 case ET_hyper:
553 ev->u.dval = static_cast<double>(ev->u.hval);
554 ev->et = ET_double;
555 return true;
556 case ET_uhyper:
557 if (ev->u.dval > FLT_MAX || ev->u.dval < -FLT_MAX)
558 return false;
559 ev->u.dval = static_cast<double>(ev->u.ulval);
560 ev->et = ET_double;
561 return true;
562 case ET_boolean:
563 ev->u.dval = ev->u.bval ? 1.0 : 0.0;
564 ev->et = ET_double;
565 return true;
566 case ET_float:
567 ev->u.dval = static_cast<double>(ev->u.fval);
568 ev->et = ET_double;
569 return true;
570 case ET_double:
571 return true;
572 case ET_byte:
573 ev->u.dval = static_cast<double>(ev->u.byval);
574 ev->et = ET_double;
575 return true;
576 default:
577 OSL_ASSERT(false);
578 return false;
580 case ET_byte:
581 switch (ev->et)
583 case ET_short:
584 if (ev->u.sval < SAL_MIN_INT8 || ev->u.sval > SAL_MAX_UINT8)
585 return false;
586 ev->u.byval = static_cast<unsigned char>(ev->u.sval);
587 ev->et = ET_byte;
588 return true;
589 case ET_ushort:
590 if (ev->u.usval > SAL_MAX_UINT8)
591 return false;
592 ev->u.byval = static_cast<unsigned char>(ev->u.usval);
593 ev->et = ET_byte;
594 return true;
595 case ET_long:
596 if (ev->u.lval < SAL_MIN_INT8 || ev->u.lval > SAL_MAX_UINT8)
597 return false;
598 ev->u.byval = static_cast<unsigned char>(ev->u.lval);
599 ev->et = ET_byte;
600 return true;
601 case ET_ulong:
602 if (ev->u.ulval > SAL_MAX_UINT8)
603 return false;
604 ev->u.byval = static_cast<unsigned char>(ev->u.ulval);
605 ev->et = ET_byte;
606 return true;
607 case ET_hyper:
608 if (ev->u.hval < SAL_MIN_INT8 || ev->u.hval > SAL_MAX_UINT8)
609 return false;
610 ev->u.byval = static_cast<unsigned char>(ev->u.hval);
611 ev->et = ET_byte;
612 return true;
613 case ET_uhyper:
614 if (ev->u.uhval > SAL_MAX_UINT8)
615 return false;
616 ev->u.byval = static_cast<unsigned char>(ev->u.uhval);
617 ev->et = ET_byte;
618 return true;
619 case ET_boolean:
620 ev->u.byval = ev->u.bval ? 1 : 0;
621 ev->et = ET_byte;
622 return true;
623 case ET_float:
624 if (ev->u.fval < SAL_MIN_INT8 || ev->u.fval > SAL_MAX_UINT8)
625 return false;
626 ev->u.byval = static_cast<unsigned char>(ev->u.fval);
627 ev->et = ET_byte;
628 return true;
629 case ET_double:
630 if (ev->u.dval < SAL_MIN_INT8 || ev->u.dval > SAL_MAX_UINT8)
631 return false;
632 ev->u.byval = static_cast<unsigned char>(ev->u.dval);
633 ev->et = ET_byte;
634 return true;
635 case ET_byte:
636 return true;
637 default:
638 OSL_ASSERT(false);
639 return false;
641 default:
642 OSL_ASSERT(false);
643 return false;
647 bool AstExpression::coerce(ExprType t)
650 * Is it already of the right type?
652 if (m_exprValue != nullptr && m_exprValue->et == t)
653 return true;
655 * OK, must coerce
657 * First, evaluate it, then try to coerce result type
658 * If already evaluated, return the result
660 evaluate();
661 if (m_exprValue == nullptr)
662 return false;
664 if (!coerce_value(m_exprValue.get(), t))
665 m_exprValue.reset();
667 return m_exprValue != nullptr;
670 bool AstExpression::compareLong(AstExpression *pExpr)
672 bool bRet = false;
673 if (m_combOperator != pExpr->m_combOperator)
674 return bRet;
675 evaluate();
676 pExpr->evaluate();
677 if (m_exprValue == nullptr || pExpr->getExprValue() == nullptr)
678 return bRet;
679 if (m_exprValue->et != pExpr->getExprValue()->et)
680 return bRet;
681 switch (m_exprValue->et)
683 case ET_long:
684 bRet = m_exprValue->u.lval == pExpr->getExprValue()->u.lval;
685 break;
686 default:
687 OSL_ASSERT(false);
688 bRet = false;
689 break;
691 return bRet;
694 void AstExpression::evaluate()
697 * Already evaluated?
699 if ( m_exprValue != nullptr )
700 return;
702 * OK, must evaluate operator
704 switch (m_combOperator)
706 case ExprComb::Add:
707 case ExprComb::Minus:
708 case ExprComb::Mul:
709 case ExprComb::Div:
710 case ExprComb::Mod:
711 m_exprValue = eval_bin_op();
712 break;
713 case ExprComb::Or:
714 case ExprComb::Xor:
715 case ExprComb::And:
716 case ExprComb::Left:
717 case ExprComb::Right:
718 m_exprValue = eval_bit_op();
719 break;
720 case ExprComb::UPlus:
721 case ExprComb::UMinus:
722 m_exprValue = eval_un_op();
723 break;
724 case ExprComb::Symbol:
725 m_exprValue = eval_symbol();
726 break;
727 case ExprComb::NONE:
728 break;
732 std::unique_ptr<AstExprValue> AstExpression::eval_bin_op()
734 ExprType eType = ET_double;
736 if ( m_combOperator == ExprComb::Mod )
737 eType = ET_hyper;
739 if (m_subExpr1 == nullptr || m_subExpr2 == nullptr)
740 return nullptr;
741 m_subExpr1->evaluate();
742 if (m_subExpr1->getExprValue() == nullptr)
743 return nullptr;
744 if (!m_subExpr1->coerce(eType))
745 return nullptr;
746 m_subExpr2->evaluate();
747 if (m_subExpr2->getExprValue() == nullptr)
748 return nullptr;
749 if (!m_subExpr2->coerce(eType))
750 return nullptr;
752 std::unique_ptr< AstExprValue > retval(new AstExprValue);
753 retval->et = eType;
755 switch (m_combOperator)
757 case ExprComb::Mod:
758 if (m_subExpr2->getExprValue()->u.hval == 0)
759 return nullptr;
760 retval->u.hval = m_subExpr1->getExprValue()->u.hval % m_subExpr2->getExprValue()->u.hval;
761 break;
762 case ExprComb::Add:
763 retval->u.dval = m_subExpr1->getExprValue()->u.dval + m_subExpr2->getExprValue()->u.dval;
764 break;
765 case ExprComb::Minus:
766 retval->u.dval = m_subExpr1->getExprValue()->u.dval - m_subExpr2->getExprValue()->u.dval;
767 break;
768 case ExprComb::Mul:
769 retval->u.dval = m_subExpr1->getExprValue()->u.dval * m_subExpr2->getExprValue()->u.dval;
770 break;
771 case ExprComb::Div:
772 if (m_subExpr2->getExprValue()->u.dval == 0.0)
773 return nullptr;
774 retval->u.dval = m_subExpr1->getExprValue()->u.dval / m_subExpr2->getExprValue()->u.dval;
775 break;
776 default:
777 return nullptr;
780 return retval;
783 std::unique_ptr<AstExprValue> AstExpression::eval_bit_op()
785 if (m_subExpr1 == nullptr || m_subExpr2 == nullptr)
786 return nullptr;
787 m_subExpr1->evaluate();
788 if (m_subExpr1->getExprValue() == nullptr)
789 return nullptr;
790 if (!m_subExpr1->coerce(ET_long))
791 return nullptr;
792 m_subExpr2->evaluate();
793 if (m_subExpr2->getExprValue() == nullptr)
794 return nullptr;
795 if (!m_subExpr2->coerce(ET_long))
796 return nullptr;
798 std::unique_ptr< AstExprValue > retval(new AstExprValue);
799 retval->et = ET_long;
801 switch (m_combOperator)
803 case ExprComb::Or:
804 retval->u.lval = m_subExpr1->getExprValue()->u.lval | m_subExpr2->getExprValue()->u.lval;
805 break;
806 case ExprComb::Xor:
807 retval->u.lval = m_subExpr1->getExprValue()->u.lval ^ m_subExpr2->getExprValue()->u.lval;
808 break;
809 case ExprComb::And:
810 retval->u.lval = m_subExpr1->getExprValue()->u.lval & m_subExpr2->getExprValue()->u.lval;
811 break;
812 case ExprComb::Left:
813 retval->u.lval = m_subExpr1->getExprValue()->u.lval << m_subExpr2->getExprValue()->u.lval;
814 break;
815 case ExprComb::Right:
816 retval->u.lval = m_subExpr1->getExprValue()->u.lval >> m_subExpr2->getExprValue()->u.lval;
817 break;
818 default:
819 return nullptr;
822 return retval;
825 std::unique_ptr<AstExprValue> AstExpression::eval_un_op()
827 if (m_subExpr1 == nullptr)
828 return nullptr;
829 m_subExpr1->evaluate();
830 if (m_subExpr1->getExprValue() == nullptr)
831 return nullptr;
832 if (!m_subExpr1->coerce(ET_double))
833 return nullptr;
835 std::unique_ptr< AstExprValue > retval(new AstExprValue);
836 retval->et = ET_double;
838 switch (m_combOperator)
840 case ExprComb::UPlus:
841 retval->u.lval = m_subExpr1->getExprValue()->u.lval;
842 break;
843 case ExprComb::UMinus:
844 retval->u.lval = -(m_subExpr1->getExprValue()->u.lval);
845 break;
846 default:
847 return nullptr;
850 return retval;
853 std::unique_ptr<AstExprValue> AstExpression::eval_symbol()
855 AstScope *pScope = nullptr;
856 AstDeclaration *pDecl;
857 AstConstant *pConst;
860 * Is there a symbol stored?
862 if (!m_xSymbolicName)
864 ErrorHandler::evalError(this);
865 return nullptr;
868 * Get current scope for lookup
870 if (idlc()->scopes()->depth() > 0)
871 pScope = idlc()->scopes()->topNonNull();
872 if ( !pScope )
874 ErrorHandler::lookupError(*m_xSymbolicName);
875 return nullptr;
878 * Do lookup
880 pDecl = pScope->lookupByName(*m_xSymbolicName);
881 if (pDecl == nullptr)
883 ErrorHandler::lookupError(*m_xSymbolicName);
884 return nullptr;
887 * Is it a constant?
889 if (pDecl->getNodeType() != NT_const &&
890 pDecl->getNodeType() != NT_enum_val)
892 ErrorHandler::constantExpected(pDecl, *m_xSymbolicName);
893 return nullptr;
895 if (!ErrorHandler::checkPublished(pDecl))
897 return nullptr;
900 * OK, now evaluate the constant we just got, to produce its value
902 pConst = static_cast< AstConstant* >(pDecl);
903 pConst->getConstValue()->evaluate();
904 auto const val = pConst->getConstValue()->getExprValue();
905 return val == nullptr ? nullptr : std::make_unique<AstExprValue>(*val);
908 OString AstExpression::toString()
910 OString exprStr;
911 if ( m_combOperator == ExprComb::Symbol )
912 return m_xSymbolicName ? *m_xSymbolicName : OString("<Undefined Name>");
914 if ( m_exprValue )
916 switch (m_exprValue->et)
918 case ET_short:
919 return OString::number(m_exprValue->u.sval);
920 case ET_ushort:
921 return OString::number(m_exprValue->u.usval);
922 case ET_long:
923 return OString::number(m_exprValue->u.lval);
924 case ET_ulong:
925 return OString::number(m_exprValue->u.ulval);
926 case ET_hyper:
927 return OString::number(m_exprValue->u.hval);
928 case ET_uhyper:
929 return OString::number(m_exprValue->u.uhval);
930 case ET_float:
931 return OString::number(m_exprValue->u.fval);
932 case ET_double:
933 return OString::number(m_exprValue->u.dval);
934 case ET_byte:
935 return OString::number(m_exprValue->u.byval);
936 case ET_boolean:
937 if ( m_exprValue->u.lval == 0)
938 return OString("FALSE");
939 else
940 return OString("TRUE");
941 default:
942 OSL_ASSERT(false);
943 return OString();
947 switch (m_combOperator)
949 case ExprComb::UPlus:
950 exprStr += "+";
951 break;
952 case ExprComb::UMinus:
953 exprStr += "-";
954 break;
955 default:
956 break;
958 if ( m_subExpr1 )
959 exprStr += m_subExpr1->toString();
960 switch (m_combOperator)
962 case ExprComb::Add:
963 exprStr += " + ";
964 break;
965 case ExprComb::Minus:
966 exprStr += " - ";
967 break;
968 case ExprComb::Mul:
969 exprStr += " * ";
970 break;
971 case ExprComb::Div:
972 exprStr += " / ";
973 break;
974 case ExprComb::Mod:
975 exprStr += " % ";
976 break;
977 case ExprComb::Or:
978 exprStr += " | ";
979 break;
980 case ExprComb::Xor:
981 exprStr += " ^ ";
982 break;
983 case ExprComb::And:
984 exprStr += " & ";
985 break;
986 case ExprComb::Left:
987 exprStr += " << ";
988 break;
989 case ExprComb::Right:
990 exprStr += " >> ";
991 break;
992 default:
993 break;
996 if ( m_subExpr2 )
997 exprStr += m_subExpr2->toString();
999 return exprStr;
1002 // Convert the type of an AST_Expression to a char *
1003 const sal_Char* exprTypeToString(ExprType t)
1005 switch (t)
1007 case ET_short:
1008 return "short";
1009 case ET_ushort:
1010 return "unsigned short";
1011 case ET_long:
1012 return "long";
1013 case ET_ulong:
1014 return "unsigned long";
1015 case ET_hyper:
1016 return "hyper";
1017 case ET_uhyper:
1018 return "unsigned hyper";
1019 case ET_float:
1020 return "float";
1021 case ET_double:
1022 return "double";
1023 case ET_char:
1024 return "char";
1025 case ET_byte:
1026 return "byte";
1027 case ET_boolean:
1028 return "boolean";
1029 case ET_string:
1030 return "string";
1031 case ET_any:
1032 return "any";
1033 case ET_type:
1034 return "type";
1035 case ET_void:
1036 return "void";
1037 case ET_none:
1038 return "none";
1041 return "unknown";
1044 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */