merge the formfield patch from ooo-build
[ooovba.git] / idlc / source / astexpression.cxx
blobe124e091cf0147730bf1451c2d1b6962cc968d34
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: astexpression.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_idlc.hxx"
33 #include <idlc/astexpression.hxx>
34 #include <idlc/astconstant.hxx>
35 #include <idlc/astscope.hxx>
36 #include <idlc/errorhandler.hxx>
38 #include <limits.h>
39 #include <float.h>
41 #undef MAXCHAR
42 #define MAXCHAR 127
43 #undef MINCHAR
44 #define MINCHAR -128
46 using namespace ::rtl;
48 AstExpression::AstExpression(ExprComb c, AstExpression *pExpr1, AstExpression *pExpr2)
49 : m_combOperator(c)
50 , m_subExpr1(pExpr1)
51 , m_subExpr2(pExpr2)
52 , m_exprValue(NULL)
53 , m_pSymbolicName(NULL)
55 fillDefinitionDetails();
59 AstExpression::AstExpression(sal_Int32 l)
60 : m_combOperator(EC_none)
61 , m_subExpr1(NULL)
62 , m_subExpr2(NULL)
63 , m_exprValue(NULL)
64 , m_pSymbolicName(NULL)
66 fillDefinitionDetails();
68 m_exprValue = new AstExprValue();
69 m_exprValue->et = ET_long;
70 m_exprValue->u.lval = l;
73 AstExpression::AstExpression(sal_Int32 l, ExprType et)
74 : m_combOperator(EC_none)
75 , m_subExpr1(NULL)
76 , m_subExpr2(NULL)
77 , m_exprValue(NULL)
78 , m_pSymbolicName(NULL)
80 fillDefinitionDetails();
82 m_exprValue = new AstExprValue();
83 m_exprValue->et = et;
84 m_exprValue->u.lval = l;
87 AstExpression::AstExpression(sal_Int64 h)
88 : m_combOperator(EC_none)
89 , m_subExpr1(NULL)
90 , m_subExpr2(NULL)
91 , m_exprValue(NULL)
92 , m_pSymbolicName(NULL)
94 fillDefinitionDetails();
96 m_exprValue = new AstExprValue();
97 m_exprValue->et = ET_hyper;
98 m_exprValue->u.hval = h;
101 AstExpression::AstExpression(sal_uInt64 uh)
102 : m_combOperator(EC_none)
103 , m_subExpr1(NULL)
104 , m_subExpr2(NULL)
105 , m_exprValue(NULL)
106 , m_pSymbolicName(NULL)
108 fillDefinitionDetails();
110 m_exprValue = new AstExprValue();
111 m_exprValue->et = ET_uhyper;
112 m_exprValue->u.uhval = uh;
115 AstExpression::AstExpression(double d)
116 : m_combOperator(EC_none)
117 , m_subExpr1(NULL)
118 , m_subExpr2(NULL)
119 , m_exprValue(NULL)
120 , m_pSymbolicName(NULL)
122 fillDefinitionDetails();
124 m_exprValue = new AstExprValue();
125 m_exprValue->et = ET_double;
126 m_exprValue->u.dval = d;
129 AstExpression::AstExpression(::rtl::OString* scopedName)
130 : m_combOperator(EC_symbol)
131 , m_subExpr1(NULL)
132 , m_subExpr2(NULL)
133 , m_exprValue(NULL)
134 , m_pSymbolicName(scopedName)
136 fillDefinitionDetails();
139 AstExpression::~AstExpression()
141 if ( m_exprValue )
142 delete m_exprValue;
143 if ( m_subExpr1 )
144 delete m_subExpr1;
145 if ( m_subExpr2 )
146 delete m_subExpr2;
147 if ( m_pSymbolicName )
148 delete m_pSymbolicName;
152 * Perform the coercion from the given AstExprValue to the requested
153 * ExprType. Return an AstExprValue if successful, NULL if failed.
154 * must be done for hyper, uhyper
156 static AstExprValue *
157 coerce_value(AstExprValue *ev, ExprType t)
159 if (ev == NULL)
160 return NULL;
162 switch (t)
164 case ET_short:
165 switch (ev->et)
167 case ET_short:
168 return ev;
169 case ET_ushort:
170 if (ev->u.usval > SAL_MAX_INT16)
171 return NULL;
172 ev->u.sval = (sal_Int16)ev->u.usval;
173 ev->et = ET_short;
174 return ev;
175 case ET_long:
176 if (ev->u.lval < SAL_MIN_INT16 || ev->u.lval > SAL_MAX_INT16)
177 return NULL;
178 ev->u.sval = (sal_Int16)ev->u.lval;
179 ev->et = ET_short;
180 return ev;
181 case ET_ulong:
182 if (ev->u.ulval > SAL_MAX_INT16)
183 return NULL;
184 ev->u.sval = (sal_Int16)ev->u.ulval;
185 ev->et = ET_short;
186 return ev;
187 case ET_hyper:
188 if (ev->u.hval < SAL_MIN_INT16 || ev->u.hval > SAL_MAX_INT16)
189 return NULL;
190 ev->u.sval = (sal_Int16)ev->u.hval;
191 ev->et = ET_short;
192 return ev;
193 case ET_uhyper:
194 if (ev->u.uhval > SAL_MAX_INT16)
195 return NULL;
196 ev->u.sval = (sal_Int16)ev->u.uhval;
197 ev->et = ET_short;
198 return ev;
199 case ET_boolean:
200 ev->u.sval = (sal_Int16)ev->u.bval;
201 ev->et = ET_short;
202 return ev;
203 case ET_float:
204 if (ev->u.fval < SAL_MIN_INT16 || ev->u.fval > SAL_MAX_INT16)
205 return NULL;
206 ev->u.sval = (sal_Int16)ev->u.fval;
207 ev->et = ET_short;
208 return ev;
209 case ET_double:
210 if (ev->u.dval < SAL_MIN_INT16 || ev->u.dval > SAL_MAX_INT16)
211 return NULL;
212 ev->u.sval = (sal_Int16)ev->u.dval;
213 ev->et = ET_short;
214 return ev;
215 case ET_byte:
216 ev->u.sval = (sal_Int16)ev->u.byval;
217 ev->et = ET_short;
218 return ev;
219 default:
220 OSL_ASSERT(false);
221 return NULL;
223 case ET_ushort:
224 switch (ev->et)
226 case ET_short:
227 if (ev->u.sval < 0)
228 return NULL;
229 ev->u.usval = (sal_uInt16)ev->u.sval;
230 ev->et = ET_ushort;
231 return ev;
232 case ET_ushort:
233 return ev;
234 case ET_long:
235 if (ev->u.lval < 0 || ev->u.lval > SAL_MAX_UINT16)
236 return NULL;
237 ev->u.usval = (sal_uInt16)ev->u.lval;
238 ev->et = ET_ushort;
239 return ev;
240 case ET_ulong:
241 if (ev->u.ulval > SAL_MAX_UINT16)
242 return NULL;
243 ev->u.usval = (sal_uInt16)ev->u.ulval;
244 ev->et = ET_ushort;
245 return ev;
246 case ET_hyper:
247 if (ev->u.hval < 0 || ev->u.hval > SAL_MAX_UINT16)
248 return NULL;
249 ev->u.usval = (sal_uInt16)ev->u.hval;
250 ev->et = ET_ushort;
251 return ev;
252 case ET_uhyper:
253 if (ev->u.uhval > SAL_MAX_UINT16)
254 return NULL;
255 ev->u.usval = (sal_uInt16)ev->u.uhval;
256 ev->et = ET_ushort;
257 return ev;
258 case ET_boolean:
259 ev->u.usval = (sal_uInt16)ev->u.bval;
260 ev->et = ET_short;
261 return ev;
262 case ET_float:
263 if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT16)
264 return NULL;
265 ev->u.usval = (sal_uInt16)ev->u.fval;
266 ev->et = ET_short;
267 return ev;
268 case ET_double:
269 if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT16)
270 return NULL;
271 ev->u.usval = (sal_uInt16)ev->u.dval;
272 ev->et = ET_short;
273 return ev;
274 case ET_byte:
275 ev->u.usval = (sal_uInt16)ev->u.byval;
276 ev->et = ET_ushort;
277 return ev;
278 default:
279 OSL_ASSERT(false);
280 return NULL;
282 case ET_long:
283 switch (ev->et)
285 case ET_short:
286 ev->u.lval = (sal_Int32)ev->u.sval;
287 ev->et = ET_long;
288 return ev;
289 case ET_ushort:
290 ev->u.lval = (sal_Int32)ev->u.usval;
291 ev->et = ET_long;
292 return ev;
293 case ET_long:
294 return ev;
295 case ET_ulong:
296 if (ev->u.ulval > SAL_MAX_INT32)
297 return NULL;
298 ev->u.lval = (sal_Int32)ev->u.ulval;
299 ev->et = ET_long;
300 return ev;
301 case ET_hyper:
302 if (ev->u.hval < SAL_MIN_INT32 || ev->u.hval > SAL_MAX_INT32)
303 return NULL;
304 ev->u.lval = (sal_Int32)ev->u.hval;
305 ev->et = ET_long;
306 return ev;
307 case ET_uhyper:
308 if (ev->u.uhval > SAL_MAX_INT32)
309 return NULL;
310 ev->u.lval = (sal_Int32)ev->u.uhval;
311 ev->et = ET_long;
312 return ev;
313 case ET_boolean:
314 ev->u.lval = (sal_Int32)ev->u.bval;
315 ev->et = ET_long;
316 return ev;
317 case ET_float:
318 if (ev->u.fval < SAL_MIN_INT32 || ev->u.fval > SAL_MAX_INT32)
319 return NULL;
320 ev->u.lval = (sal_Int32)ev->u.fval;
321 ev->et = ET_long;
322 return ev;
323 case ET_double:
324 if (ev->u.dval < SAL_MIN_INT32 || ev->u.dval > SAL_MAX_INT32)
325 return NULL;
326 ev->u.lval = (sal_Int32)ev->u.dval;
327 ev->et = ET_long;
328 return ev;
329 case ET_byte:
330 ev->u.lval = (sal_Int32) ev->u.byval;
331 ev->et = ET_long;
332 return ev;
333 default:
334 OSL_ASSERT(false);
335 return NULL;
337 case ET_ulong:
338 switch (ev->et)
340 case ET_short:
341 if (ev->u.sval < 0)
342 return NULL;
343 ev->u.ulval = (sal_uInt32)ev->u.sval;
344 ev->et = ET_ulong;
345 return ev;
346 case ET_ushort:
347 ev->u.ulval = (sal_uInt32)ev->u.usval;
348 ev->et = ET_ulong;
349 return ev;
350 case ET_long:
351 if (ev->u.lval < 0)
352 return NULL;
353 ev->u.ulval = (sal_uInt32)ev->u.lval;
354 ev->et = ET_ulong;
355 return ev;
356 case ET_ulong:
357 return ev;
358 case ET_hyper:
359 if (ev->u.hval < 0 || ev->u.hval > SAL_MAX_UINT32)
360 return NULL;
361 ev->u.lval = (sal_uInt32)ev->u.hval;
362 ev->et = ET_ulong;
363 return ev;
364 case ET_uhyper:
365 if (ev->u.uhval > SAL_MAX_UINT32)
366 return NULL;
367 ev->u.ulval = (sal_uInt32)ev->u.uhval;
368 ev->et = ET_ulong;
369 return ev;
370 case ET_boolean:
371 ev->u.ulval = (sal_uInt32)ev->u.bval;
372 ev->et = ET_ulong;
373 return ev;
374 case ET_float:
375 if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT32)
376 return NULL;
377 ev->u.ulval = (sal_uInt32)ev->u.fval;
378 ev->et = ET_ulong;
379 return ev;
380 case ET_double:
381 if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT32)
382 return NULL;
383 ev->u.ulval = (sal_uInt32)ev->u.dval;
384 ev->et = ET_ulong;
385 return ev;
386 case ET_byte:
387 ev->u.ulval = (sal_uInt32)ev->u.byval;
388 ev->et = ET_ulong;
389 return ev;
390 default:
391 OSL_ASSERT(false);
392 return NULL;
394 case ET_hyper:
395 switch (ev->et)
397 case ET_short:
398 ev->u.hval = (sal_Int64)ev->u.sval;
399 ev->et = ET_hyper;
400 return ev;
401 case ET_ushort:
402 ev->u.hval = (sal_Int64)ev->u.usval;
403 ev->et = ET_hyper;
404 return ev;
405 case ET_long:
406 ev->u.hval = (sal_Int64)ev->u.lval;
407 ev->et = ET_hyper;
408 return ev;
409 case ET_ulong:
410 ev->u.hval = (sal_Int64)ev->u.ulval;
411 ev->et = ET_hyper;
412 return ev;
413 case ET_hyper:
414 return ev;
415 case ET_uhyper:
416 if (ev->u.uhval > SAL_MAX_INT64)
417 return NULL;
418 ev->u.hval = (sal_Int64)ev->u.uhval;
419 ev->et = ET_long;
420 return ev;
421 case ET_boolean:
422 ev->u.hval = (sal_Int64)ev->u.bval;
423 ev->et = ET_hyper;
424 return ev;
425 case ET_float:
426 if (ev->u.fval < SAL_MIN_INT64 || ev->u.fval > SAL_MAX_INT64)
427 return NULL;
428 ev->u.hval = (sal_Int64)ev->u.fval;
429 ev->et = ET_hyper;
430 return ev;
431 case ET_double:
432 if (ev->u.dval < SAL_MIN_INT64 || ev->u.dval > SAL_MAX_INT64)
433 return NULL;
434 ev->u.hval = (sal_Int64)ev->u.dval;
435 ev->et = ET_hyper;
436 return ev;
437 case ET_byte:
438 ev->u.hval = (sal_Int64)ev->u.byval;
439 ev->et = ET_hyper;
440 return ev;
441 default:
442 OSL_ASSERT(false);
443 return NULL;
445 case ET_uhyper:
446 switch (ev->et)
448 case ET_short:
449 if (ev->u.sval < 0)
450 return NULL;
451 ev->u.uhval = (sal_uInt64)ev->u.sval;
452 ev->et = ET_uhyper;
453 return ev;
454 case ET_ushort:
455 ev->u.uhval = (sal_uInt64)ev->u.usval;
456 ev->et = ET_uhyper;
457 return ev;
458 case ET_long:
459 if (ev->u.lval < 0)
460 return NULL;
461 ev->u.uhval = (sal_uInt64)ev->u.lval;
462 ev->et = ET_uhyper;
463 return ev;
464 case ET_ulong:
465 ev->u.uhval = (sal_uInt64)ev->u.ulval;
466 ev->et = ET_uhyper;
467 return ev;
468 case ET_hyper:
469 if (ev->u.hval < 0)
470 return NULL;
471 ev->u.uhval = (sal_uInt64)ev->u.hval;
472 ev->et = ET_uhyper;
473 return ev;
474 case ET_uhyper:
475 return ev;
476 case ET_boolean:
477 ev->u.uhval = (sal_uInt64)ev->u.bval;
478 ev->et = ET_uhyper;
479 return ev;
480 case ET_float:
481 if (ev->u.fval < 0.0 || ev->u.fval > SAL_MAX_UINT64)
482 return NULL;
483 ev->u.uhval = (sal_uInt64)ev->u.fval;
484 ev->et = ET_uhyper;
485 return ev;
486 case ET_double:
487 if (ev->u.dval < 0.0 || ev->u.dval > SAL_MAX_UINT64)
488 return NULL;
489 ev->u.uhval = (sal_uInt64)ev->u.dval;
490 ev->et = ET_uhyper;
491 return ev;
492 case ET_byte:
493 ev->u.uhval = (sal_uInt64)ev->u.byval;
494 ev->et = ET_uhyper;
495 return ev;
496 default:
497 OSL_ASSERT(false);
498 return NULL;
500 case ET_boolean:
501 switch (ev->et)
503 case ET_short:
504 ev->u.bval = (ev->u.sval == 0) ? sal_False : sal_True;
505 ev->et = ET_boolean;
506 return ev;
507 case ET_ushort:
508 ev->u.bval = (ev->u.usval == 0) ? sal_False : sal_True;
509 ev->et = ET_boolean;
510 return ev;
511 case ET_long:
512 ev->u.bval = (ev->u.lval == 0) ? sal_False : sal_True;
513 ev->et = ET_boolean;
514 return ev;
515 case ET_ulong:
516 ev->u.bval = (ev->u.ulval == 0) ? sal_False : sal_True;
517 ev->et = ET_boolean;
518 return ev;
519 case ET_hyper:
520 ev->u.bval = (ev->u.hval == 0) ? sal_False : sal_True;
521 ev->et = ET_boolean;
522 return ev;
523 case ET_uhyper:
524 ev->u.bval = (ev->u.uhval == 0) ? sal_False : sal_True;
525 ev->et = ET_boolean;
526 return ev;
527 case ET_boolean:
528 return ev;
529 case ET_float:
530 ev->u.bval = (ev->u.fval == 0.0) ? sal_False : sal_True;
531 ev->et = ET_boolean;
532 return ev;
533 case ET_double:
534 ev->u.bval = (ev->u.dval == 0.0) ? sal_False : sal_True;
535 ev->et = ET_boolean;
536 return ev;
537 case ET_byte:
538 ev->u.bval = (ev->u.byval == 0) ? sal_False : sal_True;
539 ev->et = ET_boolean;
540 return ev;
541 default:
542 OSL_ASSERT(false);
543 return NULL;
545 case ET_float:
546 switch (ev->et)
548 case ET_short:
549 ev->u.fval = (float)ev->u.sval;
550 ev->et = ET_float;
551 return ev;
552 case ET_ushort:
553 ev->u.fval = (float)ev->u.usval;
554 ev->et = ET_float;
555 return ev;
556 case ET_long:
557 ev->u.fval = (float)ev->u.lval;
558 ev->et = ET_float;
559 return ev;
560 case ET_ulong:
561 ev->u.fval = (float)ev->u.ulval;
562 ev->et = ET_float;
563 return ev;
564 case ET_hyper:
565 ev->u.fval = (float)ev->u.hval;
566 ev->et = ET_float;
567 return ev;
568 case ET_uhyper:
569 if ((float)ev->u.ulval > FLT_MAX)
570 return NULL;
571 ev->u.fval = (float)ev->u.ulval;
572 ev->et = ET_float;
573 return ev;
574 case ET_boolean:
575 ev->u.fval = (ev->u.bval == sal_True) ? 1.0f : 0.0f;
576 ev->et = ET_float;
577 return ev;
578 case ET_float:
579 return ev;
580 case ET_double:
581 if ((float)ev->u.dval > FLT_MAX || (float)ev->u.dval < -FLT_MAX)
582 return NULL;
583 ev->u.fval = (float)ev->u.dval;
584 ev->et = ET_float;
585 return ev;
586 case ET_byte:
587 ev->u.fval = (float)ev->u.byval;
588 ev->et = ET_float;
589 return ev;
590 default:
591 OSL_ASSERT(false);
592 return NULL;
594 case ET_double:
595 switch (ev->et)
597 case ET_short:
598 ev->u.dval = (double)ev->u.sval;
599 ev->et = ET_double;
600 return ev;
601 case ET_ushort:
602 ev->u.dval = (double)ev->u.usval;
603 ev->et = ET_double;
604 return ev;
605 case ET_long:
606 ev->u.dval = (double)ev->u.lval;
607 ev->et = ET_double;
608 return ev;
609 case ET_ulong:
610 ev->u.dval = (double)ev->u.ulval;
611 ev->et = ET_double;
612 return ev;
613 case ET_hyper:
614 ev->u.dval = (double)ev->u.hval;
615 ev->et = ET_double;
616 return ev;
617 case ET_uhyper:
618 if ((double)ev->u.dval > FLT_MAX || (double)ev->u.dval < -FLT_MAX)
619 return NULL;
620 ev->u.dval = (double)ev->u.ulval;
621 ev->et = ET_double;
622 return ev;
623 case ET_boolean:
624 ev->u.dval = (ev->u.bval == sal_True) ? 1.0 : 0.0;
625 ev->et = ET_double;
626 return ev;
627 case ET_float:
628 ev->u.dval = (double)ev->u.fval;
629 ev->et = ET_double;
630 return ev;
631 case ET_double:
632 return ev;
633 case ET_byte:
634 ev->u.dval = (double)ev->u.byval;
635 ev->et = ET_double;
636 return ev;
637 default:
638 OSL_ASSERT(false);
639 return NULL;
641 case ET_byte:
642 switch (ev->et)
644 case ET_short:
645 if (ev->u.sval < SAL_MIN_INT8 || ev->u.sval > SAL_MAX_UINT8)
646 return NULL;
647 ev->u.byval = (sal_uChar)ev->u.sval;
648 ev->et = ET_byte;
649 return ev;
650 case ET_ushort:
651 if (ev->u.usval > SAL_MAX_UINT8)
652 return NULL;
653 ev->u.byval = (sal_uChar)ev->u.usval;
654 ev->et = ET_byte;
655 return ev;
656 case ET_long:
657 if (ev->u.lval < SAL_MIN_INT8 || ev->u.lval > SAL_MAX_UINT8)
658 return NULL;
659 ev->u.byval = (sal_uChar) ev->u.lval;
660 ev->et = ET_byte;
661 return ev;
662 case ET_ulong:
663 if (ev->u.ulval > SAL_MAX_UINT8)
664 return NULL;
665 ev->u.byval = (sal_uChar) ev->u.ulval;
666 ev->et = ET_byte;
667 return ev;
668 case ET_hyper:
669 if (ev->u.hval < SAL_MIN_INT8 || ev->u.hval > SAL_MAX_UINT8)
670 return NULL;
671 ev->u.byval = (sal_uChar) ev->u.hval;
672 ev->et = ET_byte;
673 return ev;
674 case ET_uhyper:
675 if (ev->u.uhval > SAL_MAX_UINT8)
676 return NULL;
677 ev->u.byval = (sal_uChar) ev->u.uhval;
678 ev->et = ET_byte;
679 return ev;
680 case ET_boolean:
681 ev->u.byval = (ev->u.bval == sal_False) ? 1 : 0;
682 ev->et = ET_byte;
683 return ev;
684 case ET_float:
685 if (ev->u.fval < SAL_MIN_INT8 || ev->u.fval > SAL_MAX_UINT8)
686 return NULL;
687 ev->u.byval = (sal_uChar) ev->u.fval;
688 ev->et = ET_byte;
689 return ev;
690 case ET_double:
691 if (ev->u.dval < SAL_MIN_INT8 || ev->u.dval > SAL_MAX_UINT8)
692 return NULL;
693 ev->u.byval = (sal_uChar) ev->u.dval;
694 ev->et = ET_byte;
695 return ev;
696 case ET_byte:
697 return ev;
698 default:
699 OSL_ASSERT(false);
700 return NULL;
702 default:
703 OSL_ASSERT(false);
704 return NULL;
709 * Evaluate the expression with the evaluation kind requested. Supported
710 * evaluation kinds are
711 * - EK_const: The expression must evaluate to a constant
712 * - EK_positive_int: The expression must further evaluate to a
713 * positive integer
715 static AstExprValue *
716 eval_kind(AstExprValue *ev, EvalKind ek)
718 if (ek == EK_const)
719 return ev;
720 if (ek == EK_positive_int)
721 return coerce_value(ev, ET_ulong);
723 return NULL;
726 AstExprValue* AstExpression::coerce(ExprType t, sal_Bool bAssign)
728 AstExprValue *copy;
731 * Is it already of the right type?
733 if (m_exprValue != NULL && m_exprValue->et == t)
734 return m_exprValue;
736 * OK, must coerce
738 * First, evaluate it, then try to coerce result type
739 * If already evaluated, return the result
741 m_exprValue = eval_internal(EK_const);
742 if (m_exprValue == NULL)
743 return NULL;
746 * Create a copy to contain coercion result
748 copy = new AstExprValue;
750 copy->et = m_exprValue->et;
751 switch (m_exprValue->et)
753 case ET_short:
754 copy->u.sval = m_exprValue->u.sval;
755 break;
756 case ET_ushort:
757 copy->u.usval = m_exprValue->u.usval;
758 break;
759 case ET_long:
760 copy->u.lval = m_exprValue->u.lval;
761 break;
762 case ET_ulong:
763 copy->u.ulval = m_exprValue->u.ulval;
764 break;
765 case ET_hyper:
766 copy->u.hval = m_exprValue->u.hval;
767 break;
768 case ET_uhyper:
769 copy->u.uhval = m_exprValue->u.uhval;
770 break;
771 case ET_boolean:
772 copy->u.bval = m_exprValue->u.bval;
773 break;
774 case ET_float:
775 copy->u.fval = m_exprValue->u.fval;
776 break;
777 case ET_double:
778 copy->u.dval = m_exprValue->u.dval;
779 break;
780 case ET_byte:
781 copy->u.byval = m_exprValue->u.byval;
782 break;
783 default:
784 OSL_ASSERT(false);
785 break;
788 if (bAssign)
790 m_exprValue = coerce_value(copy, t);
791 return m_exprValue;
794 return coerce_value(copy, t);
797 void AstExpression::evaluate(EvalKind ek)
799 m_exprValue = eval_internal(ek);
800 m_exprValue = eval_kind(m_exprValue, ek);
803 sal_Bool AstExpression::operator==(AstExpression *pExpr)
805 if (m_combOperator != pExpr->getCombOperator())
806 return sal_False;
807 evaluate(EK_const);
808 pExpr->evaluate(EK_const);
809 if (m_exprValue == NULL || pExpr->getExprValue() == NULL)
810 return sal_False;
811 if (m_exprValue->et != pExpr->getExprValue()->et)
812 return sal_False;
813 switch (m_exprValue->et)
815 case ET_short:
816 return (m_exprValue->u.sval == pExpr->getExprValue()->u.sval) ? sal_True : sal_False;
817 case ET_ushort:
818 return (m_exprValue->u.usval == pExpr->getExprValue()->u.usval) ? sal_True : sal_False;
819 case ET_long:
820 return (m_exprValue->u.lval == pExpr->getExprValue()->u.lval) ? sal_True : sal_False;
821 case ET_ulong:
822 return (m_exprValue->u.ulval == pExpr->getExprValue()->u.ulval) ? sal_True : sal_False;
823 case ET_hyper:
824 return (m_exprValue->u.hval == pExpr->getExprValue()->u.hval) ? sal_True : sal_False;
825 case ET_uhyper:
826 return (m_exprValue->u.uhval == pExpr->getExprValue()->u.uhval) ? sal_True : sal_False;
827 case ET_float:
828 return (m_exprValue->u.fval == pExpr->getExprValue()->u.fval) ? sal_True : sal_False;
829 case ET_double:
830 return (m_exprValue->u.dval == pExpr->getExprValue()->u.dval) ? sal_True : sal_False;
831 case ET_byte:
832 return (m_exprValue->u.byval == pExpr->getExprValue()->u.byval) ? sal_True : sal_False;
833 case ET_boolean:
834 return (m_exprValue->u.lval == pExpr->getExprValue()->u.lval) ? sal_True : sal_False;
835 default:
836 OSL_ASSERT(false);
837 return sal_False;
840 return sal_False;
843 sal_Bool AstExpression::compare(AstExpression *pExpr)
845 if (m_combOperator != pExpr->getCombOperator())
846 return sal_False;
847 evaluate(EK_const);
848 pExpr->evaluate(EK_const);
849 if (m_exprValue == NULL || pExpr->getExprValue() == NULL)
850 return sal_False;
851 if (m_exprValue->et != pExpr->getExprValue()->et)
852 return sal_False;
853 switch (m_exprValue->et)
855 case ET_short:
856 return (m_exprValue->u.sval == pExpr->getExprValue()->u.sval) ? sal_True : sal_False;
857 case ET_ushort:
858 return (m_exprValue->u.usval == pExpr->getExprValue()->u.usval) ? sal_True : sal_False;
859 case ET_long:
860 return (m_exprValue->u.lval == pExpr->getExprValue()->u.lval) ? sal_True : sal_False;
861 case ET_ulong:
862 return (m_exprValue->u.ulval == pExpr->getExprValue()->u.ulval) ? sal_True : sal_False;
863 case ET_hyper:
864 return (m_exprValue->u.hval == pExpr->getExprValue()->u.hval) ? sal_True : sal_False;
865 case ET_uhyper:
866 return (m_exprValue->u.uhval == pExpr->getExprValue()->u.uhval) ? sal_True : sal_False;
867 case ET_float:
868 return (m_exprValue->u.fval == pExpr->getExprValue()->u.fval) ? sal_True : sal_False;
869 case ET_double:
870 return (m_exprValue->u.dval == pExpr->getExprValue()->u.dval) ? sal_True : sal_False;
871 case ET_byte:
872 return (m_exprValue->u.byval == pExpr->getExprValue()->u.byval) ? sal_True : sal_False;
873 case ET_boolean:
874 return (m_exprValue->u.lval == pExpr->getExprValue()->u.lval) ? sal_True : sal_False;
875 default:
876 OSL_ASSERT(false);
877 return sal_False;
880 return sal_False;
883 void AstExpression::fillDefinitionDetails()
885 m_pScope = idlc()->scopes()->depth() > 0 ? idlc()->scopes()->top() : NULL;
886 m_lineNo = idlc()->getLineNumber();
887 m_fileName = idlc()->getFileName();
890 AstExprValue* AstExpression::eval_internal(EvalKind ek)
893 * Already evaluated?
895 if ( m_exprValue != NULL )
896 return eval_kind(m_exprValue, ek);
898 * OK, must evaluate operator
900 switch (m_combOperator)
902 case EC_add:
903 case EC_minus:
904 case EC_mul:
905 case EC_div:
906 case EC_mod:
907 m_exprValue = eval_bin_op(ek);
908 return eval_kind(m_exprValue, ek);
909 case EC_or:
910 case EC_xor:
911 case EC_and:
912 case EC_left:
913 case EC_right:
914 m_exprValue = eval_bit_op(ek);
915 return eval_kind(m_exprValue, ek);
916 case EC_u_plus:
917 case EC_u_minus:
918 case EC_bit_neg:
919 m_exprValue = eval_un_op(ek);
920 return eval_kind(m_exprValue, ek);
921 case EC_symbol:
922 m_exprValue = eval_symbol(ek);
923 return eval_kind(m_exprValue, ek);
924 case EC_none:
925 return NULL;
928 return NULL;
931 AstExprValue* AstExpression::eval_bin_op(EvalKind ek)
933 AstExprValue *retval = NULL;
934 ExprType eType = ET_double;
936 if ( m_combOperator == EC_mod )
937 eType = ET_hyper;
939 if (ek != EK_const && ek != EK_positive_int)
940 return NULL;
941 if (m_subExpr1 == NULL || m_subExpr2 == NULL)
942 return NULL;
943 m_subExpr1->setExprValue(m_subExpr1->eval_internal(ek));
944 if (m_subExpr1->getExprValue() == NULL)
945 return NULL;
946 m_subExpr1->setExprValue(m_subExpr1->coerce(eType));
947 if (m_subExpr1->getExprValue() == NULL)
948 return NULL;
949 m_subExpr2->setExprValue(m_subExpr2->eval_internal(ek));
950 if (m_subExpr2->getExprValue() == NULL)
951 return NULL;
952 m_subExpr2->setExprValue(m_subExpr2->coerce(eType));
953 if (m_subExpr2->getExprValue() == NULL)
954 return NULL;
956 retval = new AstExprValue();
957 retval->et = eType;
959 switch (m_combOperator)
961 case EC_mod:
962 if (m_subExpr2->getExprValue()->u.hval == 0)
963 return NULL;
964 retval->u.hval = m_subExpr1->getExprValue()->u.hval % m_subExpr2->getExprValue()->u.hval;
965 break;
966 case EC_add:
967 retval->u.dval = m_subExpr1->getExprValue()->u.dval + m_subExpr2->getExprValue()->u.dval;
968 break;
969 case EC_minus:
970 retval->u.dval = m_subExpr1->getExprValue()->u.dval - m_subExpr2->getExprValue()->u.dval;
971 break;
972 case EC_mul:
973 retval->u.dval = m_subExpr1->getExprValue()->u.dval * m_subExpr2->getExprValue()->u.dval;
974 break;
975 case EC_div:
976 if (m_subExpr2->getExprValue()->u.dval == 0.0)
977 return NULL;
978 retval->u.dval = m_subExpr1->getExprValue()->u.dval / m_subExpr2->getExprValue()->u.dval;
979 break;
980 default:
981 return NULL;
984 return retval;
987 AstExprValue* AstExpression::eval_bit_op(EvalKind ek)
989 AstExprValue *retval = NULL;
991 if (ek != EK_const && ek != EK_positive_int)
992 return NULL;
993 if (m_subExpr1 == NULL || m_subExpr2 == NULL)
994 return NULL;
995 m_subExpr1->setExprValue(m_subExpr1->eval_internal(ek));
996 if (m_subExpr1->getExprValue() == NULL)
997 return NULL;
998 m_subExpr1->setExprValue(m_subExpr1->coerce(ET_long));
999 if (m_subExpr1->getExprValue() == NULL)
1000 return NULL;
1001 m_subExpr2->setExprValue(m_subExpr2->eval_internal(ek));
1002 if (m_subExpr2->getExprValue() == NULL)
1003 return NULL;
1004 m_subExpr2->setExprValue(m_subExpr2->coerce(ET_long));
1005 if (m_subExpr2->getExprValue() == NULL)
1006 return NULL;
1008 retval = new AstExprValue;
1009 retval->et = ET_long;
1011 switch (m_combOperator)
1013 case EC_or:
1014 retval->u.lval = m_subExpr1->getExprValue()->u.lval | m_subExpr2->getExprValue()->u.lval;
1015 break;
1016 case EC_xor:
1017 retval->u.lval = m_subExpr1->getExprValue()->u.lval ^ m_subExpr2->getExprValue()->u.lval;
1018 break;
1019 case EC_and:
1020 retval->u.lval = m_subExpr1->getExprValue()->u.lval & m_subExpr2->getExprValue()->u.lval;
1021 break;
1022 case EC_left:
1023 retval->u.lval = m_subExpr1->getExprValue()->u.lval << m_subExpr2->getExprValue()->u.lval;
1024 break;
1025 case EC_right:
1026 retval->u.lval = m_subExpr1->getExprValue()->u.lval >> m_subExpr2->getExprValue()->u.lval;
1027 break;
1028 default:
1029 return NULL;
1032 return retval;
1035 AstExprValue* AstExpression::eval_un_op(EvalKind ek)
1037 AstExprValue *retval = NULL;
1039 if (m_exprValue != NULL)
1040 return m_exprValue;
1042 if (ek != EK_const && ek != EK_positive_int)
1043 return NULL;
1044 if (m_subExpr1 == NULL)
1045 return NULL;
1046 m_subExpr1->setExprValue(m_subExpr1->eval_internal(ek));
1047 if (m_subExpr1->getExprValue() == NULL)
1048 return NULL;
1049 m_subExpr1->setExprValue(m_subExpr1->coerce(ET_double));
1050 if (m_subExpr1->getExprValue() == NULL)
1051 return NULL;
1053 retval = new AstExprValue();
1054 retval->et = ET_double;
1056 switch (m_combOperator)
1058 case EC_u_plus:
1059 retval->u.lval = m_subExpr1->getExprValue()->u.lval;
1060 break;
1061 case EC_u_minus:
1062 retval->u.lval = -(m_subExpr1->getExprValue()->u.lval);
1063 break;
1064 case EC_bit_neg:
1065 m_subExpr1->setExprValue(m_subExpr1->coerce(ET_long));
1066 if (m_subExpr1->getExprValue() == NULL)
1067 return NULL;
1068 retval->u.lval = ~m_subExpr1->getExprValue()->u.lval;
1069 break;
1070 default:
1071 return NULL;
1074 return retval;
1077 AstExprValue* AstExpression::eval_symbol(EvalKind ek)
1079 AstScope *pScope = 0;
1080 AstDeclaration *pDecl;
1081 AstConstant *pConst;
1084 * Is there a symbol stored?
1086 if (m_pSymbolicName == NULL)
1088 idlc()->error()->evalError(this);
1089 return NULL;
1092 * Get current scope for lookup
1094 if (idlc()->scopes()->depth() > 0)
1095 pScope = idlc()->scopes()->topNonNull();
1096 if ( !pScope )
1098 idlc()->error()->lookupError(*m_pSymbolicName);
1099 return NULL;
1102 * Do lookup
1104 pDecl = pScope->lookupByName(*m_pSymbolicName);
1105 if (pDecl == NULL)
1107 idlc()->error()->lookupError(*m_pSymbolicName);
1108 return NULL;
1111 * Is it a constant?
1113 if (pDecl->getNodeType() != NT_const &&
1114 pDecl->getNodeType() != NT_enum_val)
1116 idlc()->error()->constantExpected(pDecl, *m_pSymbolicName);
1117 return NULL;
1119 if (!idlc()->error()->checkPublished(pDecl))
1121 return NULL;
1124 * OK, now evaluate the constant we just got, to produce its value
1126 pConst = static_cast< AstConstant* >(pDecl);
1127 if (pConst == NULL)
1128 return NULL;
1129 return pConst->getConstValue()->eval_internal(ek);
1132 OString AstExpression::toString()
1134 OString exprStr;
1135 if ( m_combOperator == EC_symbol )
1136 return *m_pSymbolicName;
1138 if ( m_exprValue )
1140 switch (m_exprValue->et)
1142 case ET_short:
1143 return OString::valueOf((sal_Int32)m_exprValue->u.sval);
1144 case ET_ushort:
1145 return OString::valueOf((sal_Int32)m_exprValue->u.usval);
1146 case ET_long:
1147 return OString::valueOf(m_exprValue->u.lval);
1148 case ET_ulong:
1149 return OString::valueOf((sal_Int32)m_exprValue->u.ulval);
1150 case ET_hyper:
1151 return OString::valueOf(m_exprValue->u.hval);
1152 case ET_uhyper:
1153 return OString::valueOf((sal_Int64)m_exprValue->u.uhval);
1154 case ET_float:
1155 return OString::valueOf(m_exprValue->u.fval);
1156 case ET_double:
1157 return OString::valueOf(m_exprValue->u.dval);
1158 case ET_byte:
1159 return OString::valueOf((sal_Int32)m_exprValue->u.byval);
1160 case ET_boolean:
1161 if ( m_exprValue->u.lval == 0)
1162 return OString("FALSE");
1163 else
1164 return OString("TRUE");
1165 default:
1166 OSL_ASSERT(false);
1167 return OString();
1171 switch (m_combOperator)
1173 case EC_u_plus:
1174 exprStr += OString("+");
1175 break;
1176 case EC_u_minus:
1177 exprStr += OString("-");
1178 break;
1179 case EC_bit_neg:
1180 exprStr += OString("~");
1181 break;
1182 default:
1183 break;
1185 if ( m_subExpr1 )
1186 exprStr += m_subExpr1->toString();
1187 switch (m_combOperator)
1189 case EC_add:
1190 exprStr += OString(" + ");
1191 break;
1192 case EC_minus:
1193 exprStr += OString(" - ");
1194 break;
1195 case EC_mul:
1196 exprStr += OString(" * ");
1197 break;
1198 case EC_div:
1199 exprStr += OString(" / ");
1200 break;
1201 case EC_mod:
1202 exprStr += OString(" % ");
1203 break;
1204 case EC_or:
1205 exprStr += OString(" | ");
1206 break;
1207 case EC_xor:
1208 exprStr += OString(" ^ ");
1209 break;
1210 case EC_and:
1211 exprStr += OString(" & ");
1212 break;
1213 case EC_left:
1214 exprStr += OString(" << ");
1215 break;
1216 case EC_right:
1217 exprStr += OString(" >> ");
1218 break;
1219 default:
1220 break;
1223 if ( m_subExpr2 )
1224 exprStr += m_subExpr2->toString();
1226 return exprStr;
1229 // Convert the type of an AST_Expression to a char *
1230 const sal_Char* SAL_CALL exprTypeToString(ExprType t)
1232 switch (t)
1234 case ET_short:
1235 return "short";
1236 case ET_ushort:
1237 return "unsigned short";
1238 case ET_long:
1239 return "long";
1240 case ET_ulong:
1241 return "unsigned long";
1242 case ET_hyper:
1243 return "hyper";
1244 case ET_uhyper:
1245 return "unsigned hyper";
1246 case ET_float:
1247 return "float";
1248 case ET_double:
1249 return "double";
1250 case ET_char:
1251 return "char";
1252 case ET_byte:
1253 return "byte";
1254 case ET_boolean:
1255 return "boolean";
1256 case ET_string:
1257 return "string";
1258 case ET_any:
1259 return "any";
1260 case ET_type:
1261 return "type";
1262 case ET_void:
1263 return "void";
1264 case ET_none:
1265 return "none";
1268 return ("unkown");