fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / idlc / source / astexpression.cxx
blobfffeadf49b3ed6fc48a31279099d0aa7778f7bd8
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 <idlc/astexpression.hxx>
22 #include <idlc/astconstant.hxx>
23 #include <idlc/astscope.hxx>
24 #include <idlc/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)
36 , m_exprValue(NULL)
37 , m_pSymbolicName(NULL)
39 fillDefinitionDetails();
43 AstExpression::AstExpression(sal_Int32 l)
44 : m_combOperator(EC_none)
45 , m_subExpr1(NULL)
46 , m_subExpr2(NULL)
47 , m_exprValue(NULL)
48 , m_pSymbolicName(NULL)
50 fillDefinitionDetails();
52 m_exprValue = new AstExprValue();
53 m_exprValue->et = ET_long;
54 m_exprValue->u.lval = l;
57 AstExpression::AstExpression(sal_Int32 l, ExprType et)
58 : m_combOperator(EC_none)
59 , m_subExpr1(NULL)
60 , m_subExpr2(NULL)
61 , m_exprValue(NULL)
62 , m_pSymbolicName(NULL)
64 fillDefinitionDetails();
66 m_exprValue = new AstExprValue();
67 m_exprValue->et = et;
68 m_exprValue->u.lval = l;
71 AstExpression::AstExpression(sal_Int64 h)
72 : m_combOperator(EC_none)
73 , m_subExpr1(NULL)
74 , m_subExpr2(NULL)
75 , m_exprValue(NULL)
76 , m_pSymbolicName(NULL)
78 fillDefinitionDetails();
80 m_exprValue = new AstExprValue();
81 m_exprValue->et = ET_hyper;
82 m_exprValue->u.hval = h;
85 AstExpression::AstExpression(sal_uInt64 uh)
86 : m_combOperator(EC_none)
87 , m_subExpr1(NULL)
88 , m_subExpr2(NULL)
89 , m_exprValue(NULL)
90 , m_pSymbolicName(NULL)
92 fillDefinitionDetails();
94 m_exprValue = new AstExprValue();
95 m_exprValue->et = ET_uhyper;
96 m_exprValue->u.uhval = uh;
99 AstExpression::AstExpression(double d)
100 : m_combOperator(EC_none)
101 , m_subExpr1(NULL)
102 , m_subExpr2(NULL)
103 , m_exprValue(NULL)
104 , m_pSymbolicName(NULL)
106 fillDefinitionDetails();
108 m_exprValue = new AstExprValue();
109 m_exprValue->et = ET_double;
110 m_exprValue->u.dval = d;
113 AstExpression::AstExpression(OString* scopedName)
114 : m_combOperator(EC_symbol)
115 , m_subExpr1(NULL)
116 , m_subExpr2(NULL)
117 , m_exprValue(NULL)
118 , m_pSymbolicName(scopedName)
120 fillDefinitionDetails();
123 AstExpression::~AstExpression()
125 if ( m_exprValue )
126 delete m_exprValue;
127 if ( m_subExpr1 )
128 delete m_subExpr1;
129 if ( m_subExpr2 )
130 delete m_subExpr2;
131 if ( m_pSymbolicName )
132 delete m_pSymbolicName;
136 * Perform the coercion from the given AstExprValue to the requested
137 * ExprType. Return an AstExprValue if successful, NULL if failed.
138 * must be done for hyper, uhyper
140 static AstExprValue *
141 coerce_value(AstExprValue *ev, ExprType t)
143 if (ev == NULL)
144 return NULL;
146 switch (t)
148 case ET_short:
149 switch (ev->et)
151 case ET_short:
152 return ev;
153 case ET_ushort:
154 if (ev->u.usval > SAL_MAX_INT16)
155 return NULL;
156 ev->u.sval = (sal_Int16)ev->u.usval;
157 ev->et = ET_short;
158 return ev;
159 case ET_long:
160 if (ev->u.lval < SAL_MIN_INT16 || ev->u.lval > SAL_MAX_INT16)
161 return NULL;
162 ev->u.sval = (sal_Int16)ev->u.lval;
163 ev->et = ET_short;
164 return ev;
165 case ET_ulong:
166 if (ev->u.ulval > SAL_MAX_INT16)
167 return NULL;
168 ev->u.sval = (sal_Int16)ev->u.ulval;
169 ev->et = ET_short;
170 return ev;
171 case ET_hyper:
172 if (ev->u.hval < SAL_MIN_INT16 || ev->u.hval > SAL_MAX_INT16)
173 return NULL;
174 ev->u.sval = (sal_Int16)ev->u.hval;
175 ev->et = ET_short;
176 return ev;
177 case ET_uhyper:
178 if (ev->u.uhval > SAL_MAX_INT16)
179 return NULL;
180 ev->u.sval = (sal_Int16)ev->u.uhval;
181 ev->et = ET_short;
182 return ev;
183 case ET_boolean:
184 ev->u.sval = (sal_Int16)ev->u.bval;
185 ev->et = ET_short;
186 return ev;
187 case ET_float:
188 if (ev->u.fval < SAL_MIN_INT16 || ev->u.fval > SAL_MAX_INT16)
189 return NULL;
190 ev->u.sval = (sal_Int16)ev->u.fval;
191 ev->et = ET_short;
192 return ev;
193 case ET_double:
194 if (ev->u.dval < SAL_MIN_INT16 || ev->u.dval > SAL_MAX_INT16)
195 return NULL;
196 ev->u.sval = (sal_Int16)ev->u.dval;
197 ev->et = ET_short;
198 return ev;
199 case ET_byte:
200 ev->u.sval = (sal_Int16)ev->u.byval;
201 ev->et = ET_short;
202 return ev;
203 default:
204 OSL_ASSERT(false);
205 return NULL;
207 case ET_ushort:
208 switch (ev->et)
210 case ET_short:
211 if (ev->u.sval < 0)
212 return NULL;
213 ev->u.usval = (sal_uInt16)ev->u.sval;
214 ev->et = ET_ushort;
215 return ev;
216 case ET_ushort:
217 return ev;
218 case ET_long:
219 if (ev->u.lval < 0 || ev->u.lval > SAL_MAX_UINT16)
220 return NULL;
221 ev->u.usval = (sal_uInt16)ev->u.lval;
222 ev->et = ET_ushort;
223 return ev;
224 case ET_ulong:
225 if (ev->u.ulval > SAL_MAX_UINT16)
226 return NULL;
227 ev->u.usval = (sal_uInt16)ev->u.ulval;
228 ev->et = ET_ushort;
229 return ev;
230 case ET_hyper:
231 if (ev->u.hval < 0 || ev->u.hval > SAL_MAX_UINT16)
232 return NULL;
233 ev->u.usval = (sal_uInt16)ev->u.hval;
234 ev->et = ET_ushort;
235 return ev;
236 case ET_uhyper:
237 if (ev->u.uhval > SAL_MAX_UINT16)
238 return NULL;
239 ev->u.usval = (sal_uInt16)ev->u.uhval;
240 ev->et = ET_ushort;
241 return ev;
242 case ET_boolean:
243 ev->u.usval = (sal_uInt16)ev->u.bval;
244 ev->et = ET_short;
245 return ev;
246 case ET_float:
247 if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT16)
248 return NULL;
249 ev->u.usval = (sal_uInt16)ev->u.fval;
250 ev->et = ET_short;
251 return ev;
252 case ET_double:
253 if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT16)
254 return NULL;
255 ev->u.usval = (sal_uInt16)ev->u.dval;
256 ev->et = ET_short;
257 return ev;
258 case ET_byte:
259 ev->u.usval = (sal_uInt16)ev->u.byval;
260 ev->et = ET_ushort;
261 return ev;
262 default:
263 OSL_ASSERT(false);
264 return NULL;
266 case ET_long:
267 switch (ev->et)
269 case ET_short:
270 ev->u.lval = (sal_Int32)ev->u.sval;
271 ev->et = ET_long;
272 return ev;
273 case ET_ushort:
274 ev->u.lval = (sal_Int32)ev->u.usval;
275 ev->et = ET_long;
276 return ev;
277 case ET_long:
278 return ev;
279 case ET_ulong:
280 if (ev->u.ulval > SAL_MAX_INT32)
281 return NULL;
282 ev->u.lval = (sal_Int32)ev->u.ulval;
283 ev->et = ET_long;
284 return ev;
285 case ET_hyper:
286 if (ev->u.hval < SAL_MIN_INT32 || ev->u.hval > SAL_MAX_INT32)
287 return NULL;
288 ev->u.lval = (sal_Int32)ev->u.hval;
289 ev->et = ET_long;
290 return ev;
291 case ET_uhyper:
292 if (ev->u.uhval > SAL_MAX_INT32)
293 return NULL;
294 ev->u.lval = (sal_Int32)ev->u.uhval;
295 ev->et = ET_long;
296 return ev;
297 case ET_boolean:
298 ev->u.lval = (sal_Int32)ev->u.bval;
299 ev->et = ET_long;
300 return ev;
301 case ET_float:
302 if (ev->u.fval < SAL_MIN_INT32 || ev->u.fval > SAL_MAX_INT32)
303 return NULL;
304 ev->u.lval = (sal_Int32)ev->u.fval;
305 ev->et = ET_long;
306 return ev;
307 case ET_double:
308 if (ev->u.dval < SAL_MIN_INT32 || ev->u.dval > SAL_MAX_INT32)
309 return NULL;
310 ev->u.lval = (sal_Int32)ev->u.dval;
311 ev->et = ET_long;
312 return ev;
313 case ET_byte:
314 ev->u.lval = (sal_Int32) ev->u.byval;
315 ev->et = ET_long;
316 return ev;
317 default:
318 OSL_ASSERT(false);
319 return NULL;
321 case ET_ulong:
322 switch (ev->et)
324 case ET_short:
325 if (ev->u.sval < 0)
326 return NULL;
327 ev->u.ulval = (sal_uInt32)ev->u.sval;
328 ev->et = ET_ulong;
329 return ev;
330 case ET_ushort:
331 ev->u.ulval = (sal_uInt32)ev->u.usval;
332 ev->et = ET_ulong;
333 return ev;
334 case ET_long:
335 if (ev->u.lval < 0)
336 return NULL;
337 ev->u.ulval = (sal_uInt32)ev->u.lval;
338 ev->et = ET_ulong;
339 return ev;
340 case ET_ulong:
341 return ev;
342 case ET_hyper:
343 if (ev->u.hval < 0 || ev->u.hval > SAL_MAX_UINT32)
344 return NULL;
345 ev->u.lval = (sal_uInt32)ev->u.hval;
346 ev->et = ET_ulong;
347 return ev;
348 case ET_uhyper:
349 if (ev->u.uhval > SAL_MAX_UINT32)
350 return NULL;
351 ev->u.ulval = (sal_uInt32)ev->u.uhval;
352 ev->et = ET_ulong;
353 return ev;
354 case ET_boolean:
355 ev->u.ulval = (sal_uInt32)ev->u.bval;
356 ev->et = ET_ulong;
357 return ev;
358 case ET_float:
359 if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT32)
360 return NULL;
361 ev->u.ulval = (sal_uInt32)ev->u.fval;
362 ev->et = ET_ulong;
363 return ev;
364 case ET_double:
365 if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT32)
366 return NULL;
367 ev->u.ulval = (sal_uInt32)ev->u.dval;
368 ev->et = ET_ulong;
369 return ev;
370 case ET_byte:
371 ev->u.ulval = (sal_uInt32)ev->u.byval;
372 ev->et = ET_ulong;
373 return ev;
374 default:
375 OSL_ASSERT(false);
376 return NULL;
378 case ET_hyper:
379 switch (ev->et)
381 case ET_short:
382 ev->u.hval = (sal_Int64)ev->u.sval;
383 ev->et = ET_hyper;
384 return ev;
385 case ET_ushort:
386 ev->u.hval = (sal_Int64)ev->u.usval;
387 ev->et = ET_hyper;
388 return ev;
389 case ET_long:
390 ev->u.hval = (sal_Int64)ev->u.lval;
391 ev->et = ET_hyper;
392 return ev;
393 case ET_ulong:
394 ev->u.hval = (sal_Int64)ev->u.ulval;
395 ev->et = ET_hyper;
396 return ev;
397 case ET_hyper:
398 return ev;
399 case ET_uhyper:
400 if (ev->u.uhval > SAL_MAX_INT64)
401 return NULL;
402 ev->u.hval = (sal_Int64)ev->u.uhval;
403 ev->et = ET_long;
404 return ev;
405 case ET_boolean:
406 ev->u.hval = (sal_Int64)ev->u.bval;
407 ev->et = ET_hyper;
408 return ev;
409 case ET_float:
410 if (ev->u.fval < SAL_MIN_INT64 || ev->u.fval > SAL_MAX_INT64)
411 return NULL;
412 ev->u.hval = (sal_Int64)ev->u.fval;
413 ev->et = ET_hyper;
414 return ev;
415 case ET_double:
416 if (ev->u.dval < SAL_MIN_INT64 || ev->u.dval > SAL_MAX_INT64)
417 return NULL;
418 ev->u.hval = (sal_Int64)ev->u.dval;
419 ev->et = ET_hyper;
420 return ev;
421 case ET_byte:
422 ev->u.hval = (sal_Int64)ev->u.byval;
423 ev->et = ET_hyper;
424 return ev;
425 default:
426 OSL_ASSERT(false);
427 return NULL;
429 case ET_uhyper:
430 switch (ev->et)
432 case ET_short:
433 if (ev->u.sval < 0)
434 return NULL;
435 ev->u.uhval = (sal_uInt64)ev->u.sval;
436 ev->et = ET_uhyper;
437 return ev;
438 case ET_ushort:
439 ev->u.uhval = (sal_uInt64)ev->u.usval;
440 ev->et = ET_uhyper;
441 return ev;
442 case ET_long:
443 if (ev->u.lval < 0)
444 return NULL;
445 ev->u.uhval = (sal_uInt64)ev->u.lval;
446 ev->et = ET_uhyper;
447 return ev;
448 case ET_ulong:
449 ev->u.uhval = (sal_uInt64)ev->u.ulval;
450 ev->et = ET_uhyper;
451 return ev;
452 case ET_hyper:
453 if (ev->u.hval < 0)
454 return NULL;
455 ev->u.uhval = (sal_uInt64)ev->u.hval;
456 ev->et = ET_uhyper;
457 return ev;
458 case ET_uhyper:
459 return ev;
460 case ET_boolean:
461 ev->u.uhval = (sal_uInt64)ev->u.bval;
462 ev->et = ET_uhyper;
463 return ev;
464 case ET_float:
465 if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT64)
466 return NULL;
467 ev->u.uhval = (sal_uInt64)ev->u.fval;
468 ev->et = ET_uhyper;
469 return ev;
470 case ET_double:
471 if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT64)
472 return NULL;
473 ev->u.uhval = (sal_uInt64)ev->u.dval;
474 ev->et = ET_uhyper;
475 return ev;
476 case ET_byte:
477 ev->u.uhval = (sal_uInt64)ev->u.byval;
478 ev->et = ET_uhyper;
479 return ev;
480 default:
481 OSL_ASSERT(false);
482 return NULL;
484 case ET_boolean:
485 switch (ev->et)
487 case ET_short:
488 ev->u.bval = ev->u.sval != 0;
489 ev->et = ET_boolean;
490 return ev;
491 case ET_ushort:
492 ev->u.bval = ev->u.usval != 0;
493 ev->et = ET_boolean;
494 return ev;
495 case ET_long:
496 ev->u.bval = ev->u.lval != 0;
497 ev->et = ET_boolean;
498 return ev;
499 case ET_ulong:
500 ev->u.bval = ev->u.ulval != 0;
501 ev->et = ET_boolean;
502 return ev;
503 case ET_hyper:
504 ev->u.bval = ev->u.hval != 0;
505 ev->et = ET_boolean;
506 return ev;
507 case ET_uhyper:
508 ev->u.bval = ev->u.uhval != 0;
509 ev->et = ET_boolean;
510 return ev;
511 case ET_boolean:
512 return ev;
513 case ET_float:
514 ev->u.bval = ev->u.fval != 0.0;
515 ev->et = ET_boolean;
516 return ev;
517 case ET_double:
518 ev->u.bval = ev->u.dval != 0.0;
519 ev->et = ET_boolean;
520 return ev;
521 case ET_byte:
522 ev->u.bval = ev->u.byval != 0;
523 ev->et = ET_boolean;
524 return ev;
525 default:
526 OSL_ASSERT(false);
527 return NULL;
529 case ET_float:
530 switch (ev->et)
532 case ET_short:
533 ev->u.fval = (float)ev->u.sval;
534 ev->et = ET_float;
535 return ev;
536 case ET_ushort:
537 ev->u.fval = (float)ev->u.usval;
538 ev->et = ET_float;
539 return ev;
540 case ET_long:
541 ev->u.fval = (float)ev->u.lval;
542 ev->et = ET_float;
543 return ev;
544 case ET_ulong:
545 ev->u.fval = (float)ev->u.ulval;
546 ev->et = ET_float;
547 return ev;
548 case ET_hyper:
549 ev->u.fval = (float)ev->u.hval;
550 ev->et = ET_float;
551 return ev;
552 case ET_uhyper:
553 if ((float)ev->u.ulval > FLT_MAX)
554 return NULL;
555 ev->u.fval = (float)ev->u.ulval;
556 ev->et = ET_float;
557 return ev;
558 case ET_boolean:
559 ev->u.fval = ev->u.bval ? 1.0f : 0.0f;
560 ev->et = ET_float;
561 return ev;
562 case ET_float:
563 return ev;
564 case ET_double:
565 if ((float)ev->u.dval > FLT_MAX || (float)ev->u.dval < -FLT_MAX)
566 return NULL;
567 ev->u.fval = (float)ev->u.dval;
568 ev->et = ET_float;
569 return ev;
570 case ET_byte:
571 ev->u.fval = (float)ev->u.byval;
572 ev->et = ET_float;
573 return ev;
574 default:
575 OSL_ASSERT(false);
576 return NULL;
578 case ET_double:
579 switch (ev->et)
581 case ET_short:
582 ev->u.dval = (double)ev->u.sval;
583 ev->et = ET_double;
584 return ev;
585 case ET_ushort:
586 ev->u.dval = (double)ev->u.usval;
587 ev->et = ET_double;
588 return ev;
589 case ET_long:
590 ev->u.dval = (double)ev->u.lval;
591 ev->et = ET_double;
592 return ev;
593 case ET_ulong:
594 ev->u.dval = (double)ev->u.ulval;
595 ev->et = ET_double;
596 return ev;
597 case ET_hyper:
598 ev->u.dval = (double)ev->u.hval;
599 ev->et = ET_double;
600 return ev;
601 case ET_uhyper:
602 if ((double)ev->u.dval > FLT_MAX || (double)ev->u.dval < -FLT_MAX)
603 return NULL;
604 ev->u.dval = (double)ev->u.ulval;
605 ev->et = ET_double;
606 return ev;
607 case ET_boolean:
608 ev->u.dval = ev->u.bval ? 1.0 : 0.0;
609 ev->et = ET_double;
610 return ev;
611 case ET_float:
612 ev->u.dval = (double)ev->u.fval;
613 ev->et = ET_double;
614 return ev;
615 case ET_double:
616 return ev;
617 case ET_byte:
618 ev->u.dval = (double)ev->u.byval;
619 ev->et = ET_double;
620 return ev;
621 default:
622 OSL_ASSERT(false);
623 return NULL;
625 case ET_byte:
626 switch (ev->et)
628 case ET_short:
629 if (ev->u.sval < SAL_MIN_INT8 || ev->u.sval > SAL_MAX_UINT8)
630 return NULL;
631 ev->u.byval = (unsigned char)ev->u.sval;
632 ev->et = ET_byte;
633 return ev;
634 case ET_ushort:
635 if (ev->u.usval > SAL_MAX_UINT8)
636 return NULL;
637 ev->u.byval = (unsigned char)ev->u.usval;
638 ev->et = ET_byte;
639 return ev;
640 case ET_long:
641 if (ev->u.lval < SAL_MIN_INT8 || ev->u.lval > SAL_MAX_UINT8)
642 return NULL;
643 ev->u.byval = (unsigned char) ev->u.lval;
644 ev->et = ET_byte;
645 return ev;
646 case ET_ulong:
647 if (ev->u.ulval > SAL_MAX_UINT8)
648 return NULL;
649 ev->u.byval = (unsigned char) ev->u.ulval;
650 ev->et = ET_byte;
651 return ev;
652 case ET_hyper:
653 if (ev->u.hval < SAL_MIN_INT8 || ev->u.hval > SAL_MAX_UINT8)
654 return NULL;
655 ev->u.byval = (unsigned char) ev->u.hval;
656 ev->et = ET_byte;
657 return ev;
658 case ET_uhyper:
659 if (ev->u.uhval > SAL_MAX_UINT8)
660 return NULL;
661 ev->u.byval = (unsigned char) ev->u.uhval;
662 ev->et = ET_byte;
663 return ev;
664 case ET_boolean:
665 ev->u.byval = ev->u.bval ? 1 : 0;
666 ev->et = ET_byte;
667 return ev;
668 case ET_float:
669 if (ev->u.fval < SAL_MIN_INT8 || ev->u.fval > SAL_MAX_UINT8)
670 return NULL;
671 ev->u.byval = (unsigned char) ev->u.fval;
672 ev->et = ET_byte;
673 return ev;
674 case ET_double:
675 if (ev->u.dval < SAL_MIN_INT8 || ev->u.dval > SAL_MAX_UINT8)
676 return NULL;
677 ev->u.byval = (unsigned char) ev->u.dval;
678 ev->et = ET_byte;
679 return ev;
680 case ET_byte:
681 return ev;
682 default:
683 OSL_ASSERT(false);
684 return NULL;
686 default:
687 OSL_ASSERT(false);
688 return NULL;
693 * Evaluate the expression with the evaluation kind requested. Supported
694 * evaluation kinds are
695 * - EK_const: The expression must evaluate to a constant
696 * - EK_positive_int: The expression must further evaluate to a
697 * positive integer
699 static AstExprValue *
700 eval_kind(AstExprValue *ev, EvalKind ek)
702 if (ek == EK_const)
703 return ev;
704 if (ek == EK_positive_int)
705 return coerce_value(ev, ET_ulong);
707 return NULL;
710 AstExprValue* AstExpression::coerce(ExprType t, bool bAssign)
712 AstExprValue *copy;
715 * Is it already of the right type?
717 if (m_exprValue != NULL && m_exprValue->et == t)
718 return m_exprValue;
720 * OK, must coerce
722 * First, evaluate it, then try to coerce result type
723 * If already evaluated, return the result
725 m_exprValue = eval_internal(EK_const);
726 if (m_exprValue == NULL)
727 return NULL;
730 * Create a copy to contain coercion result
732 copy = new AstExprValue;
734 copy->et = m_exprValue->et;
735 switch (m_exprValue->et)
737 case ET_short:
738 copy->u.sval = m_exprValue->u.sval;
739 break;
740 case ET_ushort:
741 copy->u.usval = m_exprValue->u.usval;
742 break;
743 case ET_long:
744 copy->u.lval = m_exprValue->u.lval;
745 break;
746 case ET_ulong:
747 copy->u.ulval = m_exprValue->u.ulval;
748 break;
749 case ET_hyper:
750 copy->u.hval = m_exprValue->u.hval;
751 break;
752 case ET_uhyper:
753 copy->u.uhval = m_exprValue->u.uhval;
754 break;
755 case ET_boolean:
756 copy->u.bval = m_exprValue->u.bval;
757 break;
758 case ET_float:
759 copy->u.fval = m_exprValue->u.fval;
760 break;
761 case ET_double:
762 copy->u.dval = m_exprValue->u.dval;
763 break;
764 case ET_byte:
765 copy->u.byval = m_exprValue->u.byval;
766 break;
767 default:
768 OSL_ASSERT(false);
769 break;
772 AstExprValue* const coerced(coerce_value(copy, t));
773 if (!coerced)
774 delete copy;
776 if (bAssign)
777 m_exprValue = coerced;
779 return coerced;
782 void AstExpression::evaluate(EvalKind ek)
784 m_exprValue = eval_internal(ek);
785 m_exprValue = eval_kind(m_exprValue, ek);
788 bool AstExpression::operator==(AstExpression *pExpr)
790 bool bRet = false;
791 if (m_combOperator != pExpr->getCombOperator())
792 return bRet;
793 evaluate(EK_const);
794 pExpr->evaluate(EK_const);
795 if (m_exprValue == NULL || pExpr->getExprValue() == NULL)
796 return bRet;
797 if (m_exprValue->et != pExpr->getExprValue()->et)
798 return bRet;
799 switch (m_exprValue->et)
801 case ET_short:
802 bRet = m_exprValue->u.sval == pExpr->getExprValue()->u.sval;
803 break;
804 case ET_ushort:
805 bRet = m_exprValue->u.usval == pExpr->getExprValue()->u.usval;
806 break;
807 case ET_long:
808 bRet = m_exprValue->u.lval == pExpr->getExprValue()->u.lval;
809 break;
810 case ET_ulong:
811 bRet = m_exprValue->u.ulval == pExpr->getExprValue()->u.ulval;
812 break;
813 case ET_hyper:
814 bRet = m_exprValue->u.hval == pExpr->getExprValue()->u.hval;
815 break;
816 case ET_uhyper:
817 bRet = m_exprValue->u.uhval == pExpr->getExprValue()->u.uhval;
818 break;
819 case ET_float:
820 bRet = m_exprValue->u.fval == pExpr->getExprValue()->u.fval;
821 break;
822 case ET_double:
823 bRet = m_exprValue->u.dval == pExpr->getExprValue()->u.dval;
824 break;
825 case ET_byte:
826 bRet = m_exprValue->u.byval == pExpr->getExprValue()->u.byval;
827 break;
828 case ET_boolean:
829 bRet = m_exprValue->u.lval == pExpr->getExprValue()->u.lval;
830 break;
831 default:
832 OSL_ASSERT(false);
833 bRet = false;
834 break;
837 return bRet;
840 bool AstExpression::compare(AstExpression *pExpr)
842 bool bRet = false;
843 if (m_combOperator != pExpr->getCombOperator())
844 return bRet;
845 evaluate(EK_const);
846 pExpr->evaluate(EK_const);
847 if (m_exprValue == NULL || pExpr->getExprValue() == NULL)
848 return bRet;
849 if (m_exprValue->et != pExpr->getExprValue()->et)
850 return bRet;
851 switch (m_exprValue->et)
853 case ET_short:
854 bRet = m_exprValue->u.sval == pExpr->getExprValue()->u.sval;
855 break;
856 case ET_ushort:
857 bRet = m_exprValue->u.usval == pExpr->getExprValue()->u.usval;
858 break;
859 case ET_long:
860 bRet = m_exprValue->u.lval == pExpr->getExprValue()->u.lval;
861 break;
862 case ET_ulong:
863 bRet = m_exprValue->u.ulval == pExpr->getExprValue()->u.ulval;
864 break;
865 case ET_hyper:
866 bRet = m_exprValue->u.hval == pExpr->getExprValue()->u.hval;
867 break;
868 case ET_uhyper:
869 bRet = m_exprValue->u.uhval == pExpr->getExprValue()->u.uhval;
870 break;
871 case ET_float:
872 bRet = m_exprValue->u.fval == pExpr->getExprValue()->u.fval;
873 break;
874 case ET_double:
875 bRet = m_exprValue->u.dval == pExpr->getExprValue()->u.dval;
876 break;
877 case ET_byte:
878 bRet = m_exprValue->u.byval == pExpr->getExprValue()->u.byval;
879 break;
880 case ET_boolean:
881 bRet = m_exprValue->u.lval == pExpr->getExprValue()->u.lval;
882 break;
883 default:
884 OSL_ASSERT(false);
885 bRet = false;
886 break;
888 return bRet;
891 void AstExpression::fillDefinitionDetails()
893 m_pScope = idlc()->scopes()->depth() > 0 ? idlc()->scopes()->top() : NULL;
894 m_lineNo = idlc()->getLineNumber();
895 m_fileName = idlc()->getFileName();
898 AstExprValue* AstExpression::eval_internal(EvalKind ek)
901 * Already evaluated?
903 if ( m_exprValue != NULL )
904 return eval_kind(m_exprValue, ek);
906 * OK, must evaluate operator
908 switch (m_combOperator)
910 case EC_add:
911 case EC_minus:
912 case EC_mul:
913 case EC_div:
914 case EC_mod:
915 m_exprValue = eval_bin_op(ek);
916 return eval_kind(m_exprValue, ek);
917 case EC_or:
918 case EC_xor:
919 case EC_and:
920 case EC_left:
921 case EC_right:
922 m_exprValue = eval_bit_op(ek);
923 return eval_kind(m_exprValue, ek);
924 case EC_u_plus:
925 case EC_u_minus:
926 case EC_bit_neg:
927 m_exprValue = eval_un_op(ek);
928 return eval_kind(m_exprValue, ek);
929 case EC_symbol:
930 m_exprValue = eval_symbol(ek);
931 return eval_kind(m_exprValue, ek);
932 case EC_none:
933 return NULL;
936 return NULL;
939 AstExprValue* AstExpression::eval_bin_op(EvalKind ek)
941 ExprType eType = ET_double;
943 if ( m_combOperator == EC_mod )
944 eType = ET_hyper;
946 if (ek != EK_const && ek != EK_positive_int)
947 return NULL;
948 if (m_subExpr1 == NULL || m_subExpr2 == NULL)
949 return NULL;
950 m_subExpr1->setExprValue(m_subExpr1->eval_internal(ek));
951 if (m_subExpr1->getExprValue() == NULL)
952 return NULL;
953 m_subExpr1->setExprValue(m_subExpr1->coerce(eType));
954 if (m_subExpr1->getExprValue() == NULL)
955 return NULL;
956 m_subExpr2->setExprValue(m_subExpr2->eval_internal(ek));
957 if (m_subExpr2->getExprValue() == NULL)
958 return NULL;
959 m_subExpr2->setExprValue(m_subExpr2->coerce(eType));
960 if (m_subExpr2->getExprValue() == NULL)
961 return NULL;
963 std::unique_ptr< AstExprValue > retval(new AstExprValue());
964 retval->et = eType;
966 switch (m_combOperator)
968 case EC_mod:
969 if (m_subExpr2->getExprValue()->u.hval == 0)
970 return NULL;
971 retval->u.hval = m_subExpr1->getExprValue()->u.hval % m_subExpr2->getExprValue()->u.hval;
972 break;
973 case EC_add:
974 retval->u.dval = m_subExpr1->getExprValue()->u.dval + m_subExpr2->getExprValue()->u.dval;
975 break;
976 case EC_minus:
977 retval->u.dval = m_subExpr1->getExprValue()->u.dval - m_subExpr2->getExprValue()->u.dval;
978 break;
979 case EC_mul:
980 retval->u.dval = m_subExpr1->getExprValue()->u.dval * m_subExpr2->getExprValue()->u.dval;
981 break;
982 case EC_div:
983 if (m_subExpr2->getExprValue()->u.dval == 0.0)
984 return NULL;
985 retval->u.dval = m_subExpr1->getExprValue()->u.dval / m_subExpr2->getExprValue()->u.dval;
986 break;
987 default:
988 return NULL;
991 return retval.release();
994 AstExprValue* AstExpression::eval_bit_op(EvalKind ek)
996 if (ek != EK_const && ek != EK_positive_int)
997 return NULL;
998 if (m_subExpr1 == NULL || m_subExpr2 == NULL)
999 return NULL;
1000 m_subExpr1->setExprValue(m_subExpr1->eval_internal(ek));
1001 if (m_subExpr1->getExprValue() == NULL)
1002 return NULL;
1003 m_subExpr1->setExprValue(m_subExpr1->coerce(ET_long));
1004 if (m_subExpr1->getExprValue() == NULL)
1005 return NULL;
1006 m_subExpr2->setExprValue(m_subExpr2->eval_internal(ek));
1007 if (m_subExpr2->getExprValue() == NULL)
1008 return NULL;
1009 m_subExpr2->setExprValue(m_subExpr2->coerce(ET_long));
1010 if (m_subExpr2->getExprValue() == NULL)
1011 return NULL;
1013 std::unique_ptr< AstExprValue > retval(new AstExprValue());
1014 retval->et = ET_long;
1016 switch (m_combOperator)
1018 case EC_or:
1019 retval->u.lval = m_subExpr1->getExprValue()->u.lval | m_subExpr2->getExprValue()->u.lval;
1020 break;
1021 case EC_xor:
1022 retval->u.lval = m_subExpr1->getExprValue()->u.lval ^ m_subExpr2->getExprValue()->u.lval;
1023 break;
1024 case EC_and:
1025 retval->u.lval = m_subExpr1->getExprValue()->u.lval & m_subExpr2->getExprValue()->u.lval;
1026 break;
1027 case EC_left:
1028 retval->u.lval = m_subExpr1->getExprValue()->u.lval << m_subExpr2->getExprValue()->u.lval;
1029 break;
1030 case EC_right:
1031 retval->u.lval = m_subExpr1->getExprValue()->u.lval >> m_subExpr2->getExprValue()->u.lval;
1032 break;
1033 default:
1034 return NULL;
1037 return retval.release();
1040 AstExprValue* AstExpression::eval_un_op(EvalKind ek)
1042 if (m_exprValue != NULL)
1043 return m_exprValue;
1045 if (ek != EK_const && ek != EK_positive_int)
1046 return NULL;
1047 if (m_subExpr1 == NULL)
1048 return NULL;
1049 m_subExpr1->setExprValue(m_subExpr1->eval_internal(ek));
1050 if (m_subExpr1->getExprValue() == NULL)
1051 return NULL;
1052 m_subExpr1->setExprValue(m_subExpr1->coerce(ET_double));
1053 if (m_subExpr1->getExprValue() == NULL)
1054 return NULL;
1056 std::unique_ptr< AstExprValue > retval(new AstExprValue());
1057 retval->et = ET_double;
1059 switch (m_combOperator)
1061 case EC_u_plus:
1062 retval->u.lval = m_subExpr1->getExprValue()->u.lval;
1063 break;
1064 case EC_u_minus:
1065 retval->u.lval = -(m_subExpr1->getExprValue()->u.lval);
1066 break;
1067 case EC_bit_neg:
1068 m_subExpr1->setExprValue(m_subExpr1->coerce(ET_long));
1069 if (m_subExpr1->getExprValue() == NULL)
1070 return NULL;
1071 retval->u.lval = ~m_subExpr1->getExprValue()->u.lval;
1072 break;
1073 default:
1074 return NULL;
1077 return retval.release();
1080 AstExprValue* AstExpression::eval_symbol(EvalKind ek)
1082 AstScope *pScope = 0;
1083 AstDeclaration *pDecl;
1084 AstConstant *pConst;
1087 * Is there a symbol stored?
1089 if (m_pSymbolicName == NULL)
1091 ErrorHandler::evalError(this);
1092 return NULL;
1095 * Get current scope for lookup
1097 if (idlc()->scopes()->depth() > 0)
1098 pScope = idlc()->scopes()->topNonNull();
1099 if ( !pScope )
1101 ErrorHandler::lookupError(*m_pSymbolicName);
1102 return NULL;
1105 * Do lookup
1107 pDecl = pScope->lookupByName(*m_pSymbolicName);
1108 if (pDecl == NULL)
1110 ErrorHandler::lookupError(*m_pSymbolicName);
1111 return NULL;
1114 * Is it a constant?
1116 if (pDecl->getNodeType() != NT_const &&
1117 pDecl->getNodeType() != NT_enum_val)
1119 ErrorHandler::constantExpected(pDecl, *m_pSymbolicName);
1120 return NULL;
1122 if (!ErrorHandler::checkPublished(pDecl))
1124 return NULL;
1127 * OK, now evaluate the constant we just got, to produce its value
1129 pConst = static_cast< AstConstant* >(pDecl);
1130 return pConst->getConstValue()->eval_internal(ek);
1133 OString AstExpression::toString()
1135 OString exprStr;
1136 if ( m_combOperator == EC_symbol )
1137 return m_pSymbolicName ? *m_pSymbolicName : OString("<Undefined Name>");
1139 if ( m_exprValue )
1141 switch (m_exprValue->et)
1143 case ET_short:
1144 return OString::number(m_exprValue->u.sval);
1145 case ET_ushort:
1146 return OString::number(m_exprValue->u.usval);
1147 case ET_long:
1148 return OString::number(m_exprValue->u.lval);
1149 case ET_ulong:
1150 return OString::number(m_exprValue->u.ulval);
1151 case ET_hyper:
1152 return OString::number(m_exprValue->u.hval);
1153 case ET_uhyper:
1154 return OString::number(m_exprValue->u.uhval);
1155 case ET_float:
1156 return OString::number(m_exprValue->u.fval);
1157 case ET_double:
1158 return OString::number(m_exprValue->u.dval);
1159 case ET_byte:
1160 return OString::number(m_exprValue->u.byval);
1161 case ET_boolean:
1162 if ( m_exprValue->u.lval == 0)
1163 return OString("FALSE");
1164 else
1165 return OString("TRUE");
1166 default:
1167 OSL_ASSERT(false);
1168 return OString();
1172 switch (m_combOperator)
1174 case EC_u_plus:
1175 exprStr += OString("+");
1176 break;
1177 case EC_u_minus:
1178 exprStr += OString("-");
1179 break;
1180 case EC_bit_neg:
1181 exprStr += OString("~");
1182 break;
1183 default:
1184 break;
1186 if ( m_subExpr1 )
1187 exprStr += m_subExpr1->toString();
1188 switch (m_combOperator)
1190 case EC_add:
1191 exprStr += OString(" + ");
1192 break;
1193 case EC_minus:
1194 exprStr += OString(" - ");
1195 break;
1196 case EC_mul:
1197 exprStr += OString(" * ");
1198 break;
1199 case EC_div:
1200 exprStr += OString(" / ");
1201 break;
1202 case EC_mod:
1203 exprStr += OString(" % ");
1204 break;
1205 case EC_or:
1206 exprStr += OString(" | ");
1207 break;
1208 case EC_xor:
1209 exprStr += OString(" ^ ");
1210 break;
1211 case EC_and:
1212 exprStr += OString(" & ");
1213 break;
1214 case EC_left:
1215 exprStr += OString(" << ");
1216 break;
1217 case EC_right:
1218 exprStr += OString(" >> ");
1219 break;
1220 default:
1221 break;
1224 if ( m_subExpr2 )
1225 exprStr += m_subExpr2->toString();
1227 return exprStr;
1230 // Convert the type of an AST_Expression to a char *
1231 const sal_Char* SAL_CALL exprTypeToString(ExprType t)
1233 switch (t)
1235 case ET_short:
1236 return "short";
1237 case ET_ushort:
1238 return "unsigned short";
1239 case ET_long:
1240 return "long";
1241 case ET_ulong:
1242 return "unsigned long";
1243 case ET_hyper:
1244 return "hyper";
1245 case ET_uhyper:
1246 return "unsigned hyper";
1247 case ET_float:
1248 return "float";
1249 case ET_double:
1250 return "double";
1251 case ET_char:
1252 return "char";
1253 case ET_byte:
1254 return "byte";
1255 case ET_boolean:
1256 return "boolean";
1257 case ET_string:
1258 return "string";
1259 case ET_any:
1260 return "any";
1261 case ET_type:
1262 return "type";
1263 case ET_void:
1264 return "void";
1265 case ET_none:
1266 return "none";
1269 return ("unknown");
1272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */