Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / idlc / source / astexpression.cxx
blobb28cfed3619cb20cc71784ed2fe57a9670c279f2
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 <limits.h>
27 #include <float.h>
28 #include <memory> // auto_ptr<>
30 #undef MAXCHAR
31 #define MAXCHAR 127
32 #undef MINCHAR
33 #define MINCHAR -128
35 using namespace ::rtl;
37 AstExpression::AstExpression(ExprComb c, AstExpression *pExpr1, AstExpression *pExpr2)
38 : m_combOperator(c)
39 , m_subExpr1(pExpr1)
40 , m_subExpr2(pExpr2)
41 , m_exprValue(NULL)
42 , m_pSymbolicName(NULL)
44 fillDefinitionDetails();
48 AstExpression::AstExpression(sal_Int32 l)
49 : m_combOperator(EC_none)
50 , m_subExpr1(NULL)
51 , m_subExpr2(NULL)
52 , m_exprValue(NULL)
53 , m_pSymbolicName(NULL)
55 fillDefinitionDetails();
57 m_exprValue = new AstExprValue();
58 m_exprValue->et = ET_long;
59 m_exprValue->u.lval = l;
62 AstExpression::AstExpression(sal_Int32 l, ExprType et)
63 : m_combOperator(EC_none)
64 , m_subExpr1(NULL)
65 , m_subExpr2(NULL)
66 , m_exprValue(NULL)
67 , m_pSymbolicName(NULL)
69 fillDefinitionDetails();
71 m_exprValue = new AstExprValue();
72 m_exprValue->et = et;
73 m_exprValue->u.lval = l;
76 AstExpression::AstExpression(sal_Int64 h)
77 : m_combOperator(EC_none)
78 , m_subExpr1(NULL)
79 , m_subExpr2(NULL)
80 , m_exprValue(NULL)
81 , m_pSymbolicName(NULL)
83 fillDefinitionDetails();
85 m_exprValue = new AstExprValue();
86 m_exprValue->et = ET_hyper;
87 m_exprValue->u.hval = h;
90 AstExpression::AstExpression(sal_uInt64 uh)
91 : m_combOperator(EC_none)
92 , m_subExpr1(NULL)
93 , m_subExpr2(NULL)
94 , m_exprValue(NULL)
95 , m_pSymbolicName(NULL)
97 fillDefinitionDetails();
99 m_exprValue = new AstExprValue();
100 m_exprValue->et = ET_uhyper;
101 m_exprValue->u.uhval = uh;
104 AstExpression::AstExpression(double d)
105 : m_combOperator(EC_none)
106 , m_subExpr1(NULL)
107 , m_subExpr2(NULL)
108 , m_exprValue(NULL)
109 , m_pSymbolicName(NULL)
111 fillDefinitionDetails();
113 m_exprValue = new AstExprValue();
114 m_exprValue->et = ET_double;
115 m_exprValue->u.dval = d;
118 AstExpression::AstExpression(::rtl::OString* scopedName)
119 : m_combOperator(EC_symbol)
120 , m_subExpr1(NULL)
121 , m_subExpr2(NULL)
122 , m_exprValue(NULL)
123 , m_pSymbolicName(scopedName)
125 fillDefinitionDetails();
128 AstExpression::~AstExpression()
130 if ( m_exprValue )
131 delete m_exprValue;
132 if ( m_subExpr1 )
133 delete m_subExpr1;
134 if ( m_subExpr2 )
135 delete m_subExpr2;
136 if ( m_pSymbolicName )
137 delete m_pSymbolicName;
141 * Perform the coercion from the given AstExprValue to the requested
142 * ExprType. Return an AstExprValue if successful, NULL if failed.
143 * must be done for hyper, uhyper
145 static AstExprValue *
146 coerce_value(AstExprValue *ev, ExprType t)
148 if (ev == NULL)
149 return NULL;
151 switch (t)
153 case ET_short:
154 switch (ev->et)
156 case ET_short:
157 return ev;
158 case ET_ushort:
159 if (ev->u.usval > SAL_MAX_INT16)
160 return NULL;
161 ev->u.sval = (sal_Int16)ev->u.usval;
162 ev->et = ET_short;
163 return ev;
164 case ET_long:
165 if (ev->u.lval < SAL_MIN_INT16 || ev->u.lval > SAL_MAX_INT16)
166 return NULL;
167 ev->u.sval = (sal_Int16)ev->u.lval;
168 ev->et = ET_short;
169 return ev;
170 case ET_ulong:
171 if (ev->u.ulval > SAL_MAX_INT16)
172 return NULL;
173 ev->u.sval = (sal_Int16)ev->u.ulval;
174 ev->et = ET_short;
175 return ev;
176 case ET_hyper:
177 if (ev->u.hval < SAL_MIN_INT16 || ev->u.hval > SAL_MAX_INT16)
178 return NULL;
179 ev->u.sval = (sal_Int16)ev->u.hval;
180 ev->et = ET_short;
181 return ev;
182 case ET_uhyper:
183 if (ev->u.uhval > SAL_MAX_INT16)
184 return NULL;
185 ev->u.sval = (sal_Int16)ev->u.uhval;
186 ev->et = ET_short;
187 return ev;
188 case ET_boolean:
189 ev->u.sval = (sal_Int16)ev->u.bval;
190 ev->et = ET_short;
191 return ev;
192 case ET_float:
193 if (ev->u.fval < SAL_MIN_INT16 || ev->u.fval > SAL_MAX_INT16)
194 return NULL;
195 ev->u.sval = (sal_Int16)ev->u.fval;
196 ev->et = ET_short;
197 return ev;
198 case ET_double:
199 if (ev->u.dval < SAL_MIN_INT16 || ev->u.dval > SAL_MAX_INT16)
200 return NULL;
201 ev->u.sval = (sal_Int16)ev->u.dval;
202 ev->et = ET_short;
203 return ev;
204 case ET_byte:
205 ev->u.sval = (sal_Int16)ev->u.byval;
206 ev->et = ET_short;
207 return ev;
208 default:
209 OSL_ASSERT(false);
210 return NULL;
212 case ET_ushort:
213 switch (ev->et)
215 case ET_short:
216 if (ev->u.sval < 0)
217 return NULL;
218 ev->u.usval = (sal_uInt16)ev->u.sval;
219 ev->et = ET_ushort;
220 return ev;
221 case ET_ushort:
222 return ev;
223 case ET_long:
224 if (ev->u.lval < 0 || ev->u.lval > SAL_MAX_UINT16)
225 return NULL;
226 ev->u.usval = (sal_uInt16)ev->u.lval;
227 ev->et = ET_ushort;
228 return ev;
229 case ET_ulong:
230 if (ev->u.ulval > SAL_MAX_UINT16)
231 return NULL;
232 ev->u.usval = (sal_uInt16)ev->u.ulval;
233 ev->et = ET_ushort;
234 return ev;
235 case ET_hyper:
236 if (ev->u.hval < 0 || ev->u.hval > SAL_MAX_UINT16)
237 return NULL;
238 ev->u.usval = (sal_uInt16)ev->u.hval;
239 ev->et = ET_ushort;
240 return ev;
241 case ET_uhyper:
242 if (ev->u.uhval > SAL_MAX_UINT16)
243 return NULL;
244 ev->u.usval = (sal_uInt16)ev->u.uhval;
245 ev->et = ET_ushort;
246 return ev;
247 case ET_boolean:
248 ev->u.usval = (sal_uInt16)ev->u.bval;
249 ev->et = ET_short;
250 return ev;
251 case ET_float:
252 if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT16)
253 return NULL;
254 ev->u.usval = (sal_uInt16)ev->u.fval;
255 ev->et = ET_short;
256 return ev;
257 case ET_double:
258 if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT16)
259 return NULL;
260 ev->u.usval = (sal_uInt16)ev->u.dval;
261 ev->et = ET_short;
262 return ev;
263 case ET_byte:
264 ev->u.usval = (sal_uInt16)ev->u.byval;
265 ev->et = ET_ushort;
266 return ev;
267 default:
268 OSL_ASSERT(false);
269 return NULL;
271 case ET_long:
272 switch (ev->et)
274 case ET_short:
275 ev->u.lval = (sal_Int32)ev->u.sval;
276 ev->et = ET_long;
277 return ev;
278 case ET_ushort:
279 ev->u.lval = (sal_Int32)ev->u.usval;
280 ev->et = ET_long;
281 return ev;
282 case ET_long:
283 return ev;
284 case ET_ulong:
285 if (ev->u.ulval > SAL_MAX_INT32)
286 return NULL;
287 ev->u.lval = (sal_Int32)ev->u.ulval;
288 ev->et = ET_long;
289 return ev;
290 case ET_hyper:
291 if (ev->u.hval < SAL_MIN_INT32 || ev->u.hval > SAL_MAX_INT32)
292 return NULL;
293 ev->u.lval = (sal_Int32)ev->u.hval;
294 ev->et = ET_long;
295 return ev;
296 case ET_uhyper:
297 if (ev->u.uhval > SAL_MAX_INT32)
298 return NULL;
299 ev->u.lval = (sal_Int32)ev->u.uhval;
300 ev->et = ET_long;
301 return ev;
302 case ET_boolean:
303 ev->u.lval = (sal_Int32)ev->u.bval;
304 ev->et = ET_long;
305 return ev;
306 case ET_float:
307 if (ev->u.fval < SAL_MIN_INT32 || ev->u.fval > SAL_MAX_INT32)
308 return NULL;
309 ev->u.lval = (sal_Int32)ev->u.fval;
310 ev->et = ET_long;
311 return ev;
312 case ET_double:
313 if (ev->u.dval < SAL_MIN_INT32 || ev->u.dval > SAL_MAX_INT32)
314 return NULL;
315 ev->u.lval = (sal_Int32)ev->u.dval;
316 ev->et = ET_long;
317 return ev;
318 case ET_byte:
319 ev->u.lval = (sal_Int32) ev->u.byval;
320 ev->et = ET_long;
321 return ev;
322 default:
323 OSL_ASSERT(false);
324 return NULL;
326 case ET_ulong:
327 switch (ev->et)
329 case ET_short:
330 if (ev->u.sval < 0)
331 return NULL;
332 ev->u.ulval = (sal_uInt32)ev->u.sval;
333 ev->et = ET_ulong;
334 return ev;
335 case ET_ushort:
336 ev->u.ulval = (sal_uInt32)ev->u.usval;
337 ev->et = ET_ulong;
338 return ev;
339 case ET_long:
340 if (ev->u.lval < 0)
341 return NULL;
342 ev->u.ulval = (sal_uInt32)ev->u.lval;
343 ev->et = ET_ulong;
344 return ev;
345 case ET_ulong:
346 return ev;
347 case ET_hyper:
348 if (ev->u.hval < 0 || ev->u.hval > SAL_MAX_UINT32)
349 return NULL;
350 ev->u.lval = (sal_uInt32)ev->u.hval;
351 ev->et = ET_ulong;
352 return ev;
353 case ET_uhyper:
354 if (ev->u.uhval > SAL_MAX_UINT32)
355 return NULL;
356 ev->u.ulval = (sal_uInt32)ev->u.uhval;
357 ev->et = ET_ulong;
358 return ev;
359 case ET_boolean:
360 ev->u.ulval = (sal_uInt32)ev->u.bval;
361 ev->et = ET_ulong;
362 return ev;
363 case ET_float:
364 if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT32)
365 return NULL;
366 ev->u.ulval = (sal_uInt32)ev->u.fval;
367 ev->et = ET_ulong;
368 return ev;
369 case ET_double:
370 if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT32)
371 return NULL;
372 ev->u.ulval = (sal_uInt32)ev->u.dval;
373 ev->et = ET_ulong;
374 return ev;
375 case ET_byte:
376 ev->u.ulval = (sal_uInt32)ev->u.byval;
377 ev->et = ET_ulong;
378 return ev;
379 default:
380 OSL_ASSERT(false);
381 return NULL;
383 case ET_hyper:
384 switch (ev->et)
386 case ET_short:
387 ev->u.hval = (sal_Int64)ev->u.sval;
388 ev->et = ET_hyper;
389 return ev;
390 case ET_ushort:
391 ev->u.hval = (sal_Int64)ev->u.usval;
392 ev->et = ET_hyper;
393 return ev;
394 case ET_long:
395 ev->u.hval = (sal_Int64)ev->u.lval;
396 ev->et = ET_hyper;
397 return ev;
398 case ET_ulong:
399 ev->u.hval = (sal_Int64)ev->u.ulval;
400 ev->et = ET_hyper;
401 return ev;
402 case ET_hyper:
403 return ev;
404 case ET_uhyper:
405 if (ev->u.uhval > SAL_MAX_INT64)
406 return NULL;
407 ev->u.hval = (sal_Int64)ev->u.uhval;
408 ev->et = ET_long;
409 return ev;
410 case ET_boolean:
411 ev->u.hval = (sal_Int64)ev->u.bval;
412 ev->et = ET_hyper;
413 return ev;
414 case ET_float:
415 if (ev->u.fval < SAL_MIN_INT64 || ev->u.fval > SAL_MAX_INT64)
416 return NULL;
417 ev->u.hval = (sal_Int64)ev->u.fval;
418 ev->et = ET_hyper;
419 return ev;
420 case ET_double:
421 if (ev->u.dval < SAL_MIN_INT64 || ev->u.dval > SAL_MAX_INT64)
422 return NULL;
423 ev->u.hval = (sal_Int64)ev->u.dval;
424 ev->et = ET_hyper;
425 return ev;
426 case ET_byte:
427 ev->u.hval = (sal_Int64)ev->u.byval;
428 ev->et = ET_hyper;
429 return ev;
430 default:
431 OSL_ASSERT(false);
432 return NULL;
434 case ET_uhyper:
435 switch (ev->et)
437 case ET_short:
438 if (ev->u.sval < 0)
439 return NULL;
440 ev->u.uhval = (sal_uInt64)ev->u.sval;
441 ev->et = ET_uhyper;
442 return ev;
443 case ET_ushort:
444 ev->u.uhval = (sal_uInt64)ev->u.usval;
445 ev->et = ET_uhyper;
446 return ev;
447 case ET_long:
448 if (ev->u.lval < 0)
449 return NULL;
450 ev->u.uhval = (sal_uInt64)ev->u.lval;
451 ev->et = ET_uhyper;
452 return ev;
453 case ET_ulong:
454 ev->u.uhval = (sal_uInt64)ev->u.ulval;
455 ev->et = ET_uhyper;
456 return ev;
457 case ET_hyper:
458 if (ev->u.hval < 0)
459 return NULL;
460 ev->u.uhval = (sal_uInt64)ev->u.hval;
461 ev->et = ET_uhyper;
462 return ev;
463 case ET_uhyper:
464 return ev;
465 case ET_boolean:
466 ev->u.uhval = (sal_uInt64)ev->u.bval;
467 ev->et = ET_uhyper;
468 return ev;
469 case ET_float:
470 if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT64)
471 return NULL;
472 ev->u.uhval = (sal_uInt64)ev->u.fval;
473 ev->et = ET_uhyper;
474 return ev;
475 case ET_double:
476 if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT64)
477 return NULL;
478 ev->u.uhval = (sal_uInt64)ev->u.dval;
479 ev->et = ET_uhyper;
480 return ev;
481 case ET_byte:
482 ev->u.uhval = (sal_uInt64)ev->u.byval;
483 ev->et = ET_uhyper;
484 return ev;
485 default:
486 OSL_ASSERT(false);
487 return NULL;
489 case ET_boolean:
490 switch (ev->et)
492 case ET_short:
493 ev->u.bval = ev->u.sval != 0;
494 ev->et = ET_boolean;
495 return ev;
496 case ET_ushort:
497 ev->u.bval = ev->u.usval != 0;
498 ev->et = ET_boolean;
499 return ev;
500 case ET_long:
501 ev->u.bval = ev->u.lval != 0;
502 ev->et = ET_boolean;
503 return ev;
504 case ET_ulong:
505 ev->u.bval = ev->u.ulval != 0;
506 ev->et = ET_boolean;
507 return ev;
508 case ET_hyper:
509 ev->u.bval = ev->u.hval != 0;
510 ev->et = ET_boolean;
511 return ev;
512 case ET_uhyper:
513 ev->u.bval = ev->u.uhval != 0;
514 ev->et = ET_boolean;
515 return ev;
516 case ET_boolean:
517 return ev;
518 case ET_float:
519 ev->u.bval = ev->u.fval != 0.0;
520 ev->et = ET_boolean;
521 return ev;
522 case ET_double:
523 ev->u.bval = ev->u.dval != 0.0;
524 ev->et = ET_boolean;
525 return ev;
526 case ET_byte:
527 ev->u.bval = ev->u.byval != 0;
528 ev->et = ET_boolean;
529 return ev;
530 default:
531 OSL_ASSERT(false);
532 return NULL;
534 case ET_float:
535 switch (ev->et)
537 case ET_short:
538 ev->u.fval = (float)ev->u.sval;
539 ev->et = ET_float;
540 return ev;
541 case ET_ushort:
542 ev->u.fval = (float)ev->u.usval;
543 ev->et = ET_float;
544 return ev;
545 case ET_long:
546 ev->u.fval = (float)ev->u.lval;
547 ev->et = ET_float;
548 return ev;
549 case ET_ulong:
550 ev->u.fval = (float)ev->u.ulval;
551 ev->et = ET_float;
552 return ev;
553 case ET_hyper:
554 ev->u.fval = (float)ev->u.hval;
555 ev->et = ET_float;
556 return ev;
557 case ET_uhyper:
558 if ((float)ev->u.ulval > FLT_MAX)
559 return NULL;
560 ev->u.fval = (float)ev->u.ulval;
561 ev->et = ET_float;
562 return ev;
563 case ET_boolean:
564 ev->u.fval = ev->u.bval ? 1.0f : 0.0f;
565 ev->et = ET_float;
566 return ev;
567 case ET_float:
568 return ev;
569 case ET_double:
570 if ((float)ev->u.dval > FLT_MAX || (float)ev->u.dval < -FLT_MAX)
571 return NULL;
572 ev->u.fval = (float)ev->u.dval;
573 ev->et = ET_float;
574 return ev;
575 case ET_byte:
576 ev->u.fval = (float)ev->u.byval;
577 ev->et = ET_float;
578 return ev;
579 default:
580 OSL_ASSERT(false);
581 return NULL;
583 case ET_double:
584 switch (ev->et)
586 case ET_short:
587 ev->u.dval = (double)ev->u.sval;
588 ev->et = ET_double;
589 return ev;
590 case ET_ushort:
591 ev->u.dval = (double)ev->u.usval;
592 ev->et = ET_double;
593 return ev;
594 case ET_long:
595 ev->u.dval = (double)ev->u.lval;
596 ev->et = ET_double;
597 return ev;
598 case ET_ulong:
599 ev->u.dval = (double)ev->u.ulval;
600 ev->et = ET_double;
601 return ev;
602 case ET_hyper:
603 ev->u.dval = (double)ev->u.hval;
604 ev->et = ET_double;
605 return ev;
606 case ET_uhyper:
607 if ((double)ev->u.dval > FLT_MAX || (double)ev->u.dval < -FLT_MAX)
608 return NULL;
609 ev->u.dval = (double)ev->u.ulval;
610 ev->et = ET_double;
611 return ev;
612 case ET_boolean:
613 ev->u.dval = ev->u.bval ? 1.0 : 0.0;
614 ev->et = ET_double;
615 return ev;
616 case ET_float:
617 ev->u.dval = (double)ev->u.fval;
618 ev->et = ET_double;
619 return ev;
620 case ET_double:
621 return ev;
622 case ET_byte:
623 ev->u.dval = (double)ev->u.byval;
624 ev->et = ET_double;
625 return ev;
626 default:
627 OSL_ASSERT(false);
628 return NULL;
630 case ET_byte:
631 switch (ev->et)
633 case ET_short:
634 if (ev->u.sval < SAL_MIN_INT8 || ev->u.sval > SAL_MAX_UINT8)
635 return NULL;
636 ev->u.byval = (sal_uChar)ev->u.sval;
637 ev->et = ET_byte;
638 return ev;
639 case ET_ushort:
640 if (ev->u.usval > SAL_MAX_UINT8)
641 return NULL;
642 ev->u.byval = (sal_uChar)ev->u.usval;
643 ev->et = ET_byte;
644 return ev;
645 case ET_long:
646 if (ev->u.lval < SAL_MIN_INT8 || ev->u.lval > SAL_MAX_UINT8)
647 return NULL;
648 ev->u.byval = (sal_uChar) ev->u.lval;
649 ev->et = ET_byte;
650 return ev;
651 case ET_ulong:
652 if (ev->u.ulval > SAL_MAX_UINT8)
653 return NULL;
654 ev->u.byval = (sal_uChar) ev->u.ulval;
655 ev->et = ET_byte;
656 return ev;
657 case ET_hyper:
658 if (ev->u.hval < SAL_MIN_INT8 || ev->u.hval > SAL_MAX_UINT8)
659 return NULL;
660 ev->u.byval = (sal_uChar) ev->u.hval;
661 ev->et = ET_byte;
662 return ev;
663 case ET_uhyper:
664 if (ev->u.uhval > SAL_MAX_UINT8)
665 return NULL;
666 ev->u.byval = (sal_uChar) ev->u.uhval;
667 ev->et = ET_byte;
668 return ev;
669 case ET_boolean:
670 ev->u.byval = ev->u.bval ? 1 : 0;
671 ev->et = ET_byte;
672 return ev;
673 case ET_float:
674 if (ev->u.fval < SAL_MIN_INT8 || ev->u.fval > SAL_MAX_UINT8)
675 return NULL;
676 ev->u.byval = (sal_uChar) ev->u.fval;
677 ev->et = ET_byte;
678 return ev;
679 case ET_double:
680 if (ev->u.dval < SAL_MIN_INT8 || ev->u.dval > SAL_MAX_UINT8)
681 return NULL;
682 ev->u.byval = (sal_uChar) ev->u.dval;
683 ev->et = ET_byte;
684 return ev;
685 case ET_byte:
686 return ev;
687 default:
688 OSL_ASSERT(false);
689 return NULL;
691 default:
692 OSL_ASSERT(false);
693 return NULL;
698 * Evaluate the expression with the evaluation kind requested. Supported
699 * evaluation kinds are
700 * - EK_const: The expression must evaluate to a constant
701 * - EK_positive_int: The expression must further evaluate to a
702 * positive integer
704 static AstExprValue *
705 eval_kind(AstExprValue *ev, EvalKind ek)
707 if (ek == EK_const)
708 return ev;
709 if (ek == EK_positive_int)
710 return coerce_value(ev, ET_ulong);
712 return NULL;
715 AstExprValue* AstExpression::coerce(ExprType t, sal_Bool bAssign)
717 AstExprValue *copy;
720 * Is it already of the right type?
722 if (m_exprValue != NULL && m_exprValue->et == t)
723 return m_exprValue;
725 * OK, must coerce
727 * First, evaluate it, then try to coerce result type
728 * If already evaluated, return the result
730 m_exprValue = eval_internal(EK_const);
731 if (m_exprValue == NULL)
732 return NULL;
735 * Create a copy to contain coercion result
737 copy = new AstExprValue;
739 copy->et = m_exprValue->et;
740 switch (m_exprValue->et)
742 case ET_short:
743 copy->u.sval = m_exprValue->u.sval;
744 break;
745 case ET_ushort:
746 copy->u.usval = m_exprValue->u.usval;
747 break;
748 case ET_long:
749 copy->u.lval = m_exprValue->u.lval;
750 break;
751 case ET_ulong:
752 copy->u.ulval = m_exprValue->u.ulval;
753 break;
754 case ET_hyper:
755 copy->u.hval = m_exprValue->u.hval;
756 break;
757 case ET_uhyper:
758 copy->u.uhval = m_exprValue->u.uhval;
759 break;
760 case ET_boolean:
761 copy->u.bval = m_exprValue->u.bval;
762 break;
763 case ET_float:
764 copy->u.fval = m_exprValue->u.fval;
765 break;
766 case ET_double:
767 copy->u.dval = m_exprValue->u.dval;
768 break;
769 case ET_byte:
770 copy->u.byval = m_exprValue->u.byval;
771 break;
772 default:
773 OSL_ASSERT(false);
774 break;
777 AstExprValue* const coerced(coerce_value(copy, t));
778 if (!coerced)
779 delete copy;
781 if (bAssign)
782 m_exprValue = coerced;
784 return coerced;
787 void AstExpression::evaluate(EvalKind ek)
789 m_exprValue = eval_internal(ek);
790 m_exprValue = eval_kind(m_exprValue, ek);
793 sal_Bool AstExpression::operator==(AstExpression *pExpr)
795 sal_Bool bRet = sal_False;
796 if (m_combOperator != pExpr->getCombOperator())
797 return bRet;
798 evaluate(EK_const);
799 pExpr->evaluate(EK_const);
800 if (m_exprValue == NULL || pExpr->getExprValue() == NULL)
801 return bRet;
802 if (m_exprValue->et != pExpr->getExprValue()->et)
803 return bRet;
804 switch (m_exprValue->et)
806 case ET_short:
807 bRet = (m_exprValue->u.sval == pExpr->getExprValue()->u.sval) ? sal_True : sal_False;
808 break;
809 case ET_ushort:
810 bRet = (m_exprValue->u.usval == pExpr->getExprValue()->u.usval) ? sal_True : sal_False;
811 break;
812 case ET_long:
813 bRet = (m_exprValue->u.lval == pExpr->getExprValue()->u.lval) ? sal_True : sal_False;
814 break;
815 case ET_ulong:
816 bRet = (m_exprValue->u.ulval == pExpr->getExprValue()->u.ulval) ? sal_True : sal_False;
817 break;
818 case ET_hyper:
819 bRet = (m_exprValue->u.hval == pExpr->getExprValue()->u.hval) ? sal_True : sal_False;
820 break;
821 case ET_uhyper:
822 bRet = (m_exprValue->u.uhval == pExpr->getExprValue()->u.uhval) ? sal_True : sal_False;
823 break;
824 case ET_float:
825 bRet = (m_exprValue->u.fval == pExpr->getExprValue()->u.fval) ? sal_True : sal_False;
826 break;
827 case ET_double:
828 bRet = (m_exprValue->u.dval == pExpr->getExprValue()->u.dval) ? sal_True : sal_False;
829 break;
830 case ET_byte:
831 bRet = (m_exprValue->u.byval == pExpr->getExprValue()->u.byval) ? sal_True : sal_False;
832 break;
833 case ET_boolean:
834 bRet = (m_exprValue->u.lval == pExpr->getExprValue()->u.lval) ? sal_True : sal_False;
835 break;
836 default:
837 OSL_ASSERT(false);
838 bRet = sal_False;
839 break;
842 return bRet;
845 sal_Bool AstExpression::compare(AstExpression *pExpr)
847 bool bRet = sal_False;
848 if (m_combOperator != pExpr->getCombOperator())
849 return bRet;
850 evaluate(EK_const);
851 pExpr->evaluate(EK_const);
852 if (m_exprValue == NULL || pExpr->getExprValue() == NULL)
853 return bRet;
854 if (m_exprValue->et != pExpr->getExprValue()->et)
855 return bRet;
856 switch (m_exprValue->et)
858 case ET_short:
859 bRet = (m_exprValue->u.sval == pExpr->getExprValue()->u.sval) ? sal_True : sal_False;
860 break;
861 case ET_ushort:
862 bRet = (m_exprValue->u.usval == pExpr->getExprValue()->u.usval) ? sal_True : sal_False;
863 break;
864 case ET_long:
865 bRet = (m_exprValue->u.lval == pExpr->getExprValue()->u.lval) ? sal_True : sal_False;
866 break;
867 case ET_ulong:
868 bRet = (m_exprValue->u.ulval == pExpr->getExprValue()->u.ulval) ? sal_True : sal_False;
869 break;
870 case ET_hyper:
871 bRet = (m_exprValue->u.hval == pExpr->getExprValue()->u.hval) ? sal_True : sal_False;
872 break;
873 case ET_uhyper:
874 bRet = (m_exprValue->u.uhval == pExpr->getExprValue()->u.uhval) ? sal_True : sal_False;
875 break;
876 case ET_float:
877 bRet = (m_exprValue->u.fval == pExpr->getExprValue()->u.fval) ? sal_True : sal_False;
878 break;
879 case ET_double:
880 bRet = (m_exprValue->u.dval == pExpr->getExprValue()->u.dval) ? sal_True : sal_False;
881 break;
882 case ET_byte:
883 bRet = (m_exprValue->u.byval == pExpr->getExprValue()->u.byval) ? sal_True : sal_False;
884 break;
885 case ET_boolean:
886 bRet = (m_exprValue->u.lval == pExpr->getExprValue()->u.lval) ? sal_True : sal_False;
887 break;
888 default:
889 OSL_ASSERT(false);
890 bRet = sal_False;
891 break;
893 return bRet;
896 void AstExpression::fillDefinitionDetails()
898 m_pScope = idlc()->scopes()->depth() > 0 ? idlc()->scopes()->top() : NULL;
899 m_lineNo = idlc()->getLineNumber();
900 m_fileName = idlc()->getFileName();
903 AstExprValue* AstExpression::eval_internal(EvalKind ek)
906 * Already evaluated?
908 if ( m_exprValue != NULL )
909 return eval_kind(m_exprValue, ek);
911 * OK, must evaluate operator
913 switch (m_combOperator)
915 case EC_add:
916 case EC_minus:
917 case EC_mul:
918 case EC_div:
919 case EC_mod:
920 m_exprValue = eval_bin_op(ek);
921 return eval_kind(m_exprValue, ek);
922 case EC_or:
923 case EC_xor:
924 case EC_and:
925 case EC_left:
926 case EC_right:
927 m_exprValue = eval_bit_op(ek);
928 return eval_kind(m_exprValue, ek);
929 case EC_u_plus:
930 case EC_u_minus:
931 case EC_bit_neg:
932 m_exprValue = eval_un_op(ek);
933 return eval_kind(m_exprValue, ek);
934 case EC_symbol:
935 m_exprValue = eval_symbol(ek);
936 return eval_kind(m_exprValue, ek);
937 case EC_none:
938 return NULL;
941 return NULL;
944 AstExprValue* AstExpression::eval_bin_op(EvalKind ek)
946 ExprType eType = ET_double;
948 if ( m_combOperator == EC_mod )
949 eType = ET_hyper;
951 if (ek != EK_const && ek != EK_positive_int)
952 return NULL;
953 if (m_subExpr1 == NULL || m_subExpr2 == NULL)
954 return NULL;
955 m_subExpr1->setExprValue(m_subExpr1->eval_internal(ek));
956 if (m_subExpr1->getExprValue() == NULL)
957 return NULL;
958 m_subExpr1->setExprValue(m_subExpr1->coerce(eType));
959 if (m_subExpr1->getExprValue() == NULL)
960 return NULL;
961 m_subExpr2->setExprValue(m_subExpr2->eval_internal(ek));
962 if (m_subExpr2->getExprValue() == NULL)
963 return NULL;
964 m_subExpr2->setExprValue(m_subExpr2->coerce(eType));
965 if (m_subExpr2->getExprValue() == NULL)
966 return NULL;
968 std::auto_ptr< AstExprValue > retval(new AstExprValue());
969 retval->et = eType;
971 switch (m_combOperator)
973 case EC_mod:
974 if (m_subExpr2->getExprValue()->u.hval == 0)
975 return NULL;
976 retval->u.hval = m_subExpr1->getExprValue()->u.hval % m_subExpr2->getExprValue()->u.hval;
977 break;
978 case EC_add:
979 retval->u.dval = m_subExpr1->getExprValue()->u.dval + m_subExpr2->getExprValue()->u.dval;
980 break;
981 case EC_minus:
982 retval->u.dval = m_subExpr1->getExprValue()->u.dval - m_subExpr2->getExprValue()->u.dval;
983 break;
984 case EC_mul:
985 retval->u.dval = m_subExpr1->getExprValue()->u.dval * m_subExpr2->getExprValue()->u.dval;
986 break;
987 case EC_div:
988 if (m_subExpr2->getExprValue()->u.dval == 0.0)
989 return NULL;
990 retval->u.dval = m_subExpr1->getExprValue()->u.dval / m_subExpr2->getExprValue()->u.dval;
991 break;
992 default:
993 return NULL;
996 return retval.release();
999 AstExprValue* AstExpression::eval_bit_op(EvalKind ek)
1001 if (ek != EK_const && ek != EK_positive_int)
1002 return NULL;
1003 if (m_subExpr1 == NULL || m_subExpr2 == NULL)
1004 return NULL;
1005 m_subExpr1->setExprValue(m_subExpr1->eval_internal(ek));
1006 if (m_subExpr1->getExprValue() == NULL)
1007 return NULL;
1008 m_subExpr1->setExprValue(m_subExpr1->coerce(ET_long));
1009 if (m_subExpr1->getExprValue() == NULL)
1010 return NULL;
1011 m_subExpr2->setExprValue(m_subExpr2->eval_internal(ek));
1012 if (m_subExpr2->getExprValue() == NULL)
1013 return NULL;
1014 m_subExpr2->setExprValue(m_subExpr2->coerce(ET_long));
1015 if (m_subExpr2->getExprValue() == NULL)
1016 return NULL;
1018 SAL_WNODEPRECATED_DECLARATIONS_PUSH
1019 std::auto_ptr< AstExprValue > retval(new AstExprValue());
1020 SAL_WNODEPRECATED_DECLARATIONS_POP
1021 retval->et = ET_long;
1023 switch (m_combOperator)
1025 case EC_or:
1026 retval->u.lval = m_subExpr1->getExprValue()->u.lval | m_subExpr2->getExprValue()->u.lval;
1027 break;
1028 case EC_xor:
1029 retval->u.lval = m_subExpr1->getExprValue()->u.lval ^ m_subExpr2->getExprValue()->u.lval;
1030 break;
1031 case EC_and:
1032 retval->u.lval = m_subExpr1->getExprValue()->u.lval & m_subExpr2->getExprValue()->u.lval;
1033 break;
1034 case EC_left:
1035 retval->u.lval = m_subExpr1->getExprValue()->u.lval << m_subExpr2->getExprValue()->u.lval;
1036 break;
1037 case EC_right:
1038 retval->u.lval = m_subExpr1->getExprValue()->u.lval >> m_subExpr2->getExprValue()->u.lval;
1039 break;
1040 default:
1041 return NULL;
1044 return retval.release();
1047 AstExprValue* AstExpression::eval_un_op(EvalKind ek)
1049 if (m_exprValue != NULL)
1050 return m_exprValue;
1052 if (ek != EK_const && ek != EK_positive_int)
1053 return NULL;
1054 if (m_subExpr1 == NULL)
1055 return NULL;
1056 m_subExpr1->setExprValue(m_subExpr1->eval_internal(ek));
1057 if (m_subExpr1->getExprValue() == NULL)
1058 return NULL;
1059 m_subExpr1->setExprValue(m_subExpr1->coerce(ET_double));
1060 if (m_subExpr1->getExprValue() == NULL)
1061 return NULL;
1063 SAL_WNODEPRECATED_DECLARATIONS_PUSH
1064 std::auto_ptr< AstExprValue > retval(new AstExprValue());
1065 SAL_WNODEPRECATED_DECLARATIONS_POP
1066 retval->et = ET_double;
1068 switch (m_combOperator)
1070 case EC_u_plus:
1071 retval->u.lval = m_subExpr1->getExprValue()->u.lval;
1072 break;
1073 case EC_u_minus:
1074 retval->u.lval = -(m_subExpr1->getExprValue()->u.lval);
1075 break;
1076 case EC_bit_neg:
1077 m_subExpr1->setExprValue(m_subExpr1->coerce(ET_long));
1078 if (m_subExpr1->getExprValue() == NULL)
1079 return NULL;
1080 retval->u.lval = ~m_subExpr1->getExprValue()->u.lval;
1081 break;
1082 default:
1083 return NULL;
1086 return retval.release();
1089 AstExprValue* AstExpression::eval_symbol(EvalKind ek)
1091 AstScope *pScope = 0;
1092 AstDeclaration *pDecl;
1093 AstConstant *pConst;
1096 * Is there a symbol stored?
1098 if (m_pSymbolicName == NULL)
1100 idlc()->error()->evalError(this);
1101 return NULL;
1104 * Get current scope for lookup
1106 if (idlc()->scopes()->depth() > 0)
1107 pScope = idlc()->scopes()->topNonNull();
1108 if ( !pScope )
1110 idlc()->error()->lookupError(*m_pSymbolicName);
1111 return NULL;
1114 * Do lookup
1116 pDecl = pScope->lookupByName(*m_pSymbolicName);
1117 if (pDecl == NULL)
1119 idlc()->error()->lookupError(*m_pSymbolicName);
1120 return NULL;
1123 * Is it a constant?
1125 if (pDecl->getNodeType() != NT_const &&
1126 pDecl->getNodeType() != NT_enum_val)
1128 idlc()->error()->constantExpected(pDecl, *m_pSymbolicName);
1129 return NULL;
1131 if (!idlc()->error()->checkPublished(pDecl))
1133 return NULL;
1136 * OK, now evaluate the constant we just got, to produce its value
1138 pConst = static_cast< AstConstant* >(pDecl);
1139 if (pConst == NULL)
1140 return NULL;
1141 return pConst->getConstValue()->eval_internal(ek);
1144 OString AstExpression::toString()
1146 OString exprStr;
1147 if ( m_combOperator == EC_symbol )
1148 return *m_pSymbolicName;
1150 if ( m_exprValue )
1152 switch (m_exprValue->et)
1154 case ET_short:
1155 return OString::valueOf((sal_Int32)m_exprValue->u.sval);
1156 case ET_ushort:
1157 return OString::valueOf((sal_Int32)m_exprValue->u.usval);
1158 case ET_long:
1159 return OString::valueOf(m_exprValue->u.lval);
1160 case ET_ulong:
1161 return OString::valueOf((sal_Int32)m_exprValue->u.ulval);
1162 case ET_hyper:
1163 return OString::valueOf(m_exprValue->u.hval);
1164 case ET_uhyper:
1165 return OString::valueOf((sal_Int64)m_exprValue->u.uhval);
1166 case ET_float:
1167 return OString::valueOf(m_exprValue->u.fval);
1168 case ET_double:
1169 return OString::valueOf(m_exprValue->u.dval);
1170 case ET_byte:
1171 return OString::valueOf((sal_Int32)m_exprValue->u.byval);
1172 case ET_boolean:
1173 if ( m_exprValue->u.lval == 0)
1174 return OString("FALSE");
1175 else
1176 return OString("TRUE");
1177 default:
1178 OSL_ASSERT(false);
1179 return OString();
1183 switch (m_combOperator)
1185 case EC_u_plus:
1186 exprStr += OString("+");
1187 break;
1188 case EC_u_minus:
1189 exprStr += OString("-");
1190 break;
1191 case EC_bit_neg:
1192 exprStr += OString("~");
1193 break;
1194 default:
1195 break;
1197 if ( m_subExpr1 )
1198 exprStr += m_subExpr1->toString();
1199 switch (m_combOperator)
1201 case EC_add:
1202 exprStr += OString(" + ");
1203 break;
1204 case EC_minus:
1205 exprStr += OString(" - ");
1206 break;
1207 case EC_mul:
1208 exprStr += OString(" * ");
1209 break;
1210 case EC_div:
1211 exprStr += OString(" / ");
1212 break;
1213 case EC_mod:
1214 exprStr += OString(" % ");
1215 break;
1216 case EC_or:
1217 exprStr += OString(" | ");
1218 break;
1219 case EC_xor:
1220 exprStr += OString(" ^ ");
1221 break;
1222 case EC_and:
1223 exprStr += OString(" & ");
1224 break;
1225 case EC_left:
1226 exprStr += OString(" << ");
1227 break;
1228 case EC_right:
1229 exprStr += OString(" >> ");
1230 break;
1231 default:
1232 break;
1235 if ( m_subExpr2 )
1236 exprStr += m_subExpr2->toString();
1238 return exprStr;
1241 // Convert the type of an AST_Expression to a char *
1242 const sal_Char* SAL_CALL exprTypeToString(ExprType t)
1244 switch (t)
1246 case ET_short:
1247 return "short";
1248 case ET_ushort:
1249 return "unsigned short";
1250 case ET_long:
1251 return "long";
1252 case ET_ulong:
1253 return "unsigned long";
1254 case ET_hyper:
1255 return "hyper";
1256 case ET_uhyper:
1257 return "unsigned hyper";
1258 case ET_float:
1259 return "float";
1260 case ET_double:
1261 return "double";
1262 case ET_char:
1263 return "char";
1264 case ET_byte:
1265 return "byte";
1266 case ET_boolean:
1267 return "boolean";
1268 case ET_string:
1269 return "string";
1270 case ET_any:
1271 return "any";
1272 case ET_type:
1273 return "type";
1274 case ET_void:
1275 return "void";
1276 case ET_none:
1277 return "none";
1280 return ("unkown");
1283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */