1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
32 AstExpression::AstExpression(ExprComb c
, AstExpression
*pExpr1
, AstExpression
*pExpr2
)
37 , m_pSymbolicName(NULL
)
39 fillDefinitionDetails();
43 AstExpression::AstExpression(sal_Int32 l
)
44 : m_combOperator(EC_none
)
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
)
62 , m_pSymbolicName(NULL
)
64 fillDefinitionDetails();
66 m_exprValue
= new AstExprValue();
68 m_exprValue
->u
.lval
= l
;
71 AstExpression::AstExpression(sal_Int64 h
)
72 : m_combOperator(EC_none
)
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
)
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
)
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
)
118 , m_pSymbolicName(scopedName
)
120 fillDefinitionDetails();
123 AstExpression::~AstExpression()
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
)
154 if (ev
->u
.usval
> SAL_MAX_INT16
)
156 ev
->u
.sval
= (sal_Int16
)ev
->u
.usval
;
160 if (ev
->u
.lval
< SAL_MIN_INT16
|| ev
->u
.lval
> SAL_MAX_INT16
)
162 ev
->u
.sval
= (sal_Int16
)ev
->u
.lval
;
166 if (ev
->u
.ulval
> SAL_MAX_INT16
)
168 ev
->u
.sval
= (sal_Int16
)ev
->u
.ulval
;
172 if (ev
->u
.hval
< SAL_MIN_INT16
|| ev
->u
.hval
> SAL_MAX_INT16
)
174 ev
->u
.sval
= (sal_Int16
)ev
->u
.hval
;
178 if (ev
->u
.uhval
> SAL_MAX_INT16
)
180 ev
->u
.sval
= (sal_Int16
)ev
->u
.uhval
;
184 ev
->u
.sval
= (sal_Int16
)ev
->u
.bval
;
188 if (ev
->u
.fval
< SAL_MIN_INT16
|| ev
->u
.fval
> SAL_MAX_INT16
)
190 ev
->u
.sval
= (sal_Int16
)ev
->u
.fval
;
194 if (ev
->u
.dval
< SAL_MIN_INT16
|| ev
->u
.dval
> SAL_MAX_INT16
)
196 ev
->u
.sval
= (sal_Int16
)ev
->u
.dval
;
200 ev
->u
.sval
= (sal_Int16
)ev
->u
.byval
;
213 ev
->u
.usval
= (sal_uInt16
)ev
->u
.sval
;
219 if (ev
->u
.lval
< 0 || ev
->u
.lval
> SAL_MAX_UINT16
)
221 ev
->u
.usval
= (sal_uInt16
)ev
->u
.lval
;
225 if (ev
->u
.ulval
> SAL_MAX_UINT16
)
227 ev
->u
.usval
= (sal_uInt16
)ev
->u
.ulval
;
231 if (ev
->u
.hval
< 0 || ev
->u
.hval
> SAL_MAX_UINT16
)
233 ev
->u
.usval
= (sal_uInt16
)ev
->u
.hval
;
237 if (ev
->u
.uhval
> SAL_MAX_UINT16
)
239 ev
->u
.usval
= (sal_uInt16
)ev
->u
.uhval
;
243 ev
->u
.usval
= (sal_uInt16
)ev
->u
.bval
;
247 if (ev
->u
.fval
< 0.0 || ev
->u
.fval
> SAL_MAX_UINT16
)
249 ev
->u
.usval
= (sal_uInt16
)ev
->u
.fval
;
253 if (ev
->u
.dval
< 0.0 || ev
->u
.dval
> SAL_MAX_UINT16
)
255 ev
->u
.usval
= (sal_uInt16
)ev
->u
.dval
;
259 ev
->u
.usval
= (sal_uInt16
)ev
->u
.byval
;
270 ev
->u
.lval
= (sal_Int32
)ev
->u
.sval
;
274 ev
->u
.lval
= (sal_Int32
)ev
->u
.usval
;
280 if (ev
->u
.ulval
> SAL_MAX_INT32
)
282 ev
->u
.lval
= (sal_Int32
)ev
->u
.ulval
;
286 if (ev
->u
.hval
< SAL_MIN_INT32
|| ev
->u
.hval
> SAL_MAX_INT32
)
288 ev
->u
.lval
= (sal_Int32
)ev
->u
.hval
;
292 if (ev
->u
.uhval
> SAL_MAX_INT32
)
294 ev
->u
.lval
= (sal_Int32
)ev
->u
.uhval
;
298 ev
->u
.lval
= (sal_Int32
)ev
->u
.bval
;
302 if (ev
->u
.fval
< SAL_MIN_INT32
|| ev
->u
.fval
> SAL_MAX_INT32
)
304 ev
->u
.lval
= (sal_Int32
)ev
->u
.fval
;
308 if (ev
->u
.dval
< SAL_MIN_INT32
|| ev
->u
.dval
> SAL_MAX_INT32
)
310 ev
->u
.lval
= (sal_Int32
)ev
->u
.dval
;
314 ev
->u
.lval
= (sal_Int32
) ev
->u
.byval
;
327 ev
->u
.ulval
= (sal_uInt32
)ev
->u
.sval
;
331 ev
->u
.ulval
= (sal_uInt32
)ev
->u
.usval
;
337 ev
->u
.ulval
= (sal_uInt32
)ev
->u
.lval
;
343 if (ev
->u
.hval
< 0 || ev
->u
.hval
> SAL_MAX_UINT32
)
345 ev
->u
.lval
= (sal_uInt32
)ev
->u
.hval
;
349 if (ev
->u
.uhval
> SAL_MAX_UINT32
)
351 ev
->u
.ulval
= (sal_uInt32
)ev
->u
.uhval
;
355 ev
->u
.ulval
= (sal_uInt32
)ev
->u
.bval
;
359 if (ev
->u
.fval
< 0.0 || ev
->u
.fval
> SAL_MAX_UINT32
)
361 ev
->u
.ulval
= (sal_uInt32
)ev
->u
.fval
;
365 if (ev
->u
.dval
< 0.0 || ev
->u
.dval
> SAL_MAX_UINT32
)
367 ev
->u
.ulval
= (sal_uInt32
)ev
->u
.dval
;
371 ev
->u
.ulval
= (sal_uInt32
)ev
->u
.byval
;
382 ev
->u
.hval
= (sal_Int64
)ev
->u
.sval
;
386 ev
->u
.hval
= (sal_Int64
)ev
->u
.usval
;
390 ev
->u
.hval
= (sal_Int64
)ev
->u
.lval
;
394 ev
->u
.hval
= (sal_Int64
)ev
->u
.ulval
;
400 if (ev
->u
.uhval
> SAL_MAX_INT64
)
402 ev
->u
.hval
= (sal_Int64
)ev
->u
.uhval
;
406 ev
->u
.hval
= (sal_Int64
)ev
->u
.bval
;
410 if (ev
->u
.fval
< SAL_MIN_INT64
|| ev
->u
.fval
> SAL_MAX_INT64
)
412 ev
->u
.hval
= (sal_Int64
)ev
->u
.fval
;
416 if (ev
->u
.dval
< SAL_MIN_INT64
|| ev
->u
.dval
> SAL_MAX_INT64
)
418 ev
->u
.hval
= (sal_Int64
)ev
->u
.dval
;
422 ev
->u
.hval
= (sal_Int64
)ev
->u
.byval
;
435 ev
->u
.uhval
= (sal_uInt64
)ev
->u
.sval
;
439 ev
->u
.uhval
= (sal_uInt64
)ev
->u
.usval
;
445 ev
->u
.uhval
= (sal_uInt64
)ev
->u
.lval
;
449 ev
->u
.uhval
= (sal_uInt64
)ev
->u
.ulval
;
455 ev
->u
.uhval
= (sal_uInt64
)ev
->u
.hval
;
461 ev
->u
.uhval
= (sal_uInt64
)ev
->u
.bval
;
465 if (ev
->u
.fval
< 0.0 || ev
->u
.fval
> SAL_MAX_UINT64
)
467 ev
->u
.uhval
= (sal_uInt64
)ev
->u
.fval
;
471 if (ev
->u
.dval
< 0.0 || ev
->u
.dval
> SAL_MAX_UINT64
)
473 ev
->u
.uhval
= (sal_uInt64
)ev
->u
.dval
;
477 ev
->u
.uhval
= (sal_uInt64
)ev
->u
.byval
;
488 ev
->u
.bval
= ev
->u
.sval
!= 0;
492 ev
->u
.bval
= ev
->u
.usval
!= 0;
496 ev
->u
.bval
= ev
->u
.lval
!= 0;
500 ev
->u
.bval
= ev
->u
.ulval
!= 0;
504 ev
->u
.bval
= ev
->u
.hval
!= 0;
508 ev
->u
.bval
= ev
->u
.uhval
!= 0;
514 ev
->u
.bval
= ev
->u
.fval
!= 0.0;
518 ev
->u
.bval
= ev
->u
.dval
!= 0.0;
522 ev
->u
.bval
= ev
->u
.byval
!= 0;
533 ev
->u
.fval
= (float)ev
->u
.sval
;
537 ev
->u
.fval
= (float)ev
->u
.usval
;
541 ev
->u
.fval
= (float)ev
->u
.lval
;
545 ev
->u
.fval
= (float)ev
->u
.ulval
;
549 ev
->u
.fval
= (float)ev
->u
.hval
;
553 if ((float)ev
->u
.ulval
> FLT_MAX
)
555 ev
->u
.fval
= (float)ev
->u
.ulval
;
559 ev
->u
.fval
= ev
->u
.bval
? 1.0f
: 0.0f
;
565 if ((float)ev
->u
.dval
> FLT_MAX
|| (float)ev
->u
.dval
< -FLT_MAX
)
567 ev
->u
.fval
= (float)ev
->u
.dval
;
571 ev
->u
.fval
= (float)ev
->u
.byval
;
582 ev
->u
.dval
= (double)ev
->u
.sval
;
586 ev
->u
.dval
= (double)ev
->u
.usval
;
590 ev
->u
.dval
= (double)ev
->u
.lval
;
594 ev
->u
.dval
= (double)ev
->u
.ulval
;
598 ev
->u
.dval
= (double)ev
->u
.hval
;
602 if ((double)ev
->u
.dval
> FLT_MAX
|| (double)ev
->u
.dval
< -FLT_MAX
)
604 ev
->u
.dval
= (double)ev
->u
.ulval
;
608 ev
->u
.dval
= ev
->u
.bval
? 1.0 : 0.0;
612 ev
->u
.dval
= (double)ev
->u
.fval
;
618 ev
->u
.dval
= (double)ev
->u
.byval
;
629 if (ev
->u
.sval
< SAL_MIN_INT8
|| ev
->u
.sval
> SAL_MAX_UINT8
)
631 ev
->u
.byval
= (unsigned char)ev
->u
.sval
;
635 if (ev
->u
.usval
> SAL_MAX_UINT8
)
637 ev
->u
.byval
= (unsigned char)ev
->u
.usval
;
641 if (ev
->u
.lval
< SAL_MIN_INT8
|| ev
->u
.lval
> SAL_MAX_UINT8
)
643 ev
->u
.byval
= (unsigned char) ev
->u
.lval
;
647 if (ev
->u
.ulval
> SAL_MAX_UINT8
)
649 ev
->u
.byval
= (unsigned char) ev
->u
.ulval
;
653 if (ev
->u
.hval
< SAL_MIN_INT8
|| ev
->u
.hval
> SAL_MAX_UINT8
)
655 ev
->u
.byval
= (unsigned char) ev
->u
.hval
;
659 if (ev
->u
.uhval
> SAL_MAX_UINT8
)
661 ev
->u
.byval
= (unsigned char) ev
->u
.uhval
;
665 ev
->u
.byval
= ev
->u
.bval
? 1 : 0;
669 if (ev
->u
.fval
< SAL_MIN_INT8
|| ev
->u
.fval
> SAL_MAX_UINT8
)
671 ev
->u
.byval
= (unsigned char) ev
->u
.fval
;
675 if (ev
->u
.dval
< SAL_MIN_INT8
|| ev
->u
.dval
> SAL_MAX_UINT8
)
677 ev
->u
.byval
= (unsigned char) ev
->u
.dval
;
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
699 static AstExprValue
*
700 eval_kind(AstExprValue
*ev
, EvalKind ek
)
704 if (ek
== EK_positive_int
)
705 return coerce_value(ev
, ET_ulong
);
710 AstExprValue
* AstExpression::coerce(ExprType t
, bool bAssign
)
715 * Is it already of the right type?
717 if (m_exprValue
!= NULL
&& m_exprValue
->et
== t
)
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
)
730 * Create a copy to contain coercion result
732 copy
= new AstExprValue
;
734 copy
->et
= m_exprValue
->et
;
735 switch (m_exprValue
->et
)
738 copy
->u
.sval
= m_exprValue
->u
.sval
;
741 copy
->u
.usval
= m_exprValue
->u
.usval
;
744 copy
->u
.lval
= m_exprValue
->u
.lval
;
747 copy
->u
.ulval
= m_exprValue
->u
.ulval
;
750 copy
->u
.hval
= m_exprValue
->u
.hval
;
753 copy
->u
.uhval
= m_exprValue
->u
.uhval
;
756 copy
->u
.bval
= m_exprValue
->u
.bval
;
759 copy
->u
.fval
= m_exprValue
->u
.fval
;
762 copy
->u
.dval
= m_exprValue
->u
.dval
;
765 copy
->u
.byval
= m_exprValue
->u
.byval
;
772 AstExprValue
* const coerced(coerce_value(copy
, t
));
777 m_exprValue
= 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
)
791 if (m_combOperator
!= pExpr
->getCombOperator())
794 pExpr
->evaluate(EK_const
);
795 if (m_exprValue
== NULL
|| pExpr
->getExprValue() == NULL
)
797 if (m_exprValue
->et
!= pExpr
->getExprValue()->et
)
799 switch (m_exprValue
->et
)
802 bRet
= m_exprValue
->u
.sval
== pExpr
->getExprValue()->u
.sval
;
805 bRet
= m_exprValue
->u
.usval
== pExpr
->getExprValue()->u
.usval
;
808 bRet
= m_exprValue
->u
.lval
== pExpr
->getExprValue()->u
.lval
;
811 bRet
= m_exprValue
->u
.ulval
== pExpr
->getExprValue()->u
.ulval
;
814 bRet
= m_exprValue
->u
.hval
== pExpr
->getExprValue()->u
.hval
;
817 bRet
= m_exprValue
->u
.uhval
== pExpr
->getExprValue()->u
.uhval
;
820 bRet
= m_exprValue
->u
.fval
== pExpr
->getExprValue()->u
.fval
;
823 bRet
= m_exprValue
->u
.dval
== pExpr
->getExprValue()->u
.dval
;
826 bRet
= m_exprValue
->u
.byval
== pExpr
->getExprValue()->u
.byval
;
829 bRet
= m_exprValue
->u
.lval
== pExpr
->getExprValue()->u
.lval
;
840 bool AstExpression::compare(AstExpression
*pExpr
)
843 if (m_combOperator
!= pExpr
->getCombOperator())
846 pExpr
->evaluate(EK_const
);
847 if (m_exprValue
== NULL
|| pExpr
->getExprValue() == NULL
)
849 if (m_exprValue
->et
!= pExpr
->getExprValue()->et
)
851 switch (m_exprValue
->et
)
854 bRet
= m_exprValue
->u
.sval
== pExpr
->getExprValue()->u
.sval
;
857 bRet
= m_exprValue
->u
.usval
== pExpr
->getExprValue()->u
.usval
;
860 bRet
= m_exprValue
->u
.lval
== pExpr
->getExprValue()->u
.lval
;
863 bRet
= m_exprValue
->u
.ulval
== pExpr
->getExprValue()->u
.ulval
;
866 bRet
= m_exprValue
->u
.hval
== pExpr
->getExprValue()->u
.hval
;
869 bRet
= m_exprValue
->u
.uhval
== pExpr
->getExprValue()->u
.uhval
;
872 bRet
= m_exprValue
->u
.fval
== pExpr
->getExprValue()->u
.fval
;
875 bRet
= m_exprValue
->u
.dval
== pExpr
->getExprValue()->u
.dval
;
878 bRet
= m_exprValue
->u
.byval
== pExpr
->getExprValue()->u
.byval
;
881 bRet
= m_exprValue
->u
.lval
== pExpr
->getExprValue()->u
.lval
;
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
)
903 if ( m_exprValue
!= NULL
)
904 return eval_kind(m_exprValue
, ek
);
906 * OK, must evaluate operator
908 switch (m_combOperator
)
915 m_exprValue
= eval_bin_op(ek
);
916 return eval_kind(m_exprValue
, ek
);
922 m_exprValue
= eval_bit_op(ek
);
923 return eval_kind(m_exprValue
, ek
);
927 m_exprValue
= eval_un_op(ek
);
928 return eval_kind(m_exprValue
, ek
);
930 m_exprValue
= eval_symbol(ek
);
931 return eval_kind(m_exprValue
, ek
);
939 AstExprValue
* AstExpression::eval_bin_op(EvalKind ek
)
941 ExprType eType
= ET_double
;
943 if ( m_combOperator
== EC_mod
)
946 if (ek
!= EK_const
&& ek
!= EK_positive_int
)
948 if (m_subExpr1
== NULL
|| m_subExpr2
== NULL
)
950 m_subExpr1
->setExprValue(m_subExpr1
->eval_internal(ek
));
951 if (m_subExpr1
->getExprValue() == NULL
)
953 m_subExpr1
->setExprValue(m_subExpr1
->coerce(eType
));
954 if (m_subExpr1
->getExprValue() == NULL
)
956 m_subExpr2
->setExprValue(m_subExpr2
->eval_internal(ek
));
957 if (m_subExpr2
->getExprValue() == NULL
)
959 m_subExpr2
->setExprValue(m_subExpr2
->coerce(eType
));
960 if (m_subExpr2
->getExprValue() == NULL
)
963 std::unique_ptr
< AstExprValue
> retval(new AstExprValue());
966 switch (m_combOperator
)
969 if (m_subExpr2
->getExprValue()->u
.hval
== 0)
971 retval
->u
.hval
= m_subExpr1
->getExprValue()->u
.hval
% m_subExpr2
->getExprValue()->u
.hval
;
974 retval
->u
.dval
= m_subExpr1
->getExprValue()->u
.dval
+ m_subExpr2
->getExprValue()->u
.dval
;
977 retval
->u
.dval
= m_subExpr1
->getExprValue()->u
.dval
- m_subExpr2
->getExprValue()->u
.dval
;
980 retval
->u
.dval
= m_subExpr1
->getExprValue()->u
.dval
* m_subExpr2
->getExprValue()->u
.dval
;
983 if (m_subExpr2
->getExprValue()->u
.dval
== 0.0)
985 retval
->u
.dval
= m_subExpr1
->getExprValue()->u
.dval
/ m_subExpr2
->getExprValue()->u
.dval
;
991 return retval
.release();
994 AstExprValue
* AstExpression::eval_bit_op(EvalKind ek
)
996 if (ek
!= EK_const
&& ek
!= EK_positive_int
)
998 if (m_subExpr1
== NULL
|| m_subExpr2
== NULL
)
1000 m_subExpr1
->setExprValue(m_subExpr1
->eval_internal(ek
));
1001 if (m_subExpr1
->getExprValue() == NULL
)
1003 m_subExpr1
->setExprValue(m_subExpr1
->coerce(ET_long
));
1004 if (m_subExpr1
->getExprValue() == NULL
)
1006 m_subExpr2
->setExprValue(m_subExpr2
->eval_internal(ek
));
1007 if (m_subExpr2
->getExprValue() == NULL
)
1009 m_subExpr2
->setExprValue(m_subExpr2
->coerce(ET_long
));
1010 if (m_subExpr2
->getExprValue() == NULL
)
1013 std::unique_ptr
< AstExprValue
> retval(new AstExprValue());
1014 retval
->et
= ET_long
;
1016 switch (m_combOperator
)
1019 retval
->u
.lval
= m_subExpr1
->getExprValue()->u
.lval
| m_subExpr2
->getExprValue()->u
.lval
;
1022 retval
->u
.lval
= m_subExpr1
->getExprValue()->u
.lval
^ m_subExpr2
->getExprValue()->u
.lval
;
1025 retval
->u
.lval
= m_subExpr1
->getExprValue()->u
.lval
& m_subExpr2
->getExprValue()->u
.lval
;
1028 retval
->u
.lval
= m_subExpr1
->getExprValue()->u
.lval
<< m_subExpr2
->getExprValue()->u
.lval
;
1031 retval
->u
.lval
= m_subExpr1
->getExprValue()->u
.lval
>> m_subExpr2
->getExprValue()->u
.lval
;
1037 return retval
.release();
1040 AstExprValue
* AstExpression::eval_un_op(EvalKind ek
)
1042 if (m_exprValue
!= NULL
)
1045 if (ek
!= EK_const
&& ek
!= EK_positive_int
)
1047 if (m_subExpr1
== NULL
)
1049 m_subExpr1
->setExprValue(m_subExpr1
->eval_internal(ek
));
1050 if (m_subExpr1
->getExprValue() == NULL
)
1052 m_subExpr1
->setExprValue(m_subExpr1
->coerce(ET_double
));
1053 if (m_subExpr1
->getExprValue() == NULL
)
1056 std::unique_ptr
< AstExprValue
> retval(new AstExprValue());
1057 retval
->et
= ET_double
;
1059 switch (m_combOperator
)
1062 retval
->u
.lval
= m_subExpr1
->getExprValue()->u
.lval
;
1065 retval
->u
.lval
= -(m_subExpr1
->getExprValue()->u
.lval
);
1068 m_subExpr1
->setExprValue(m_subExpr1
->coerce(ET_long
));
1069 if (m_subExpr1
->getExprValue() == NULL
)
1071 retval
->u
.lval
= ~m_subExpr1
->getExprValue()->u
.lval
;
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);
1095 * Get current scope for lookup
1097 if (idlc()->scopes()->depth() > 0)
1098 pScope
= idlc()->scopes()->topNonNull();
1101 ErrorHandler::lookupError(*m_pSymbolicName
);
1107 pDecl
= pScope
->lookupByName(*m_pSymbolicName
);
1110 ErrorHandler::lookupError(*m_pSymbolicName
);
1116 if (pDecl
->getNodeType() != NT_const
&&
1117 pDecl
->getNodeType() != NT_enum_val
)
1119 ErrorHandler::constantExpected(pDecl
, *m_pSymbolicName
);
1122 if (!ErrorHandler::checkPublished(pDecl
))
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()
1136 if ( m_combOperator
== EC_symbol
)
1137 return m_pSymbolicName
? *m_pSymbolicName
: OString("<Undefined Name>");
1141 switch (m_exprValue
->et
)
1144 return OString::number(m_exprValue
->u
.sval
);
1146 return OString::number(m_exprValue
->u
.usval
);
1148 return OString::number(m_exprValue
->u
.lval
);
1150 return OString::number(m_exprValue
->u
.ulval
);
1152 return OString::number(m_exprValue
->u
.hval
);
1154 return OString::number(m_exprValue
->u
.uhval
);
1156 return OString::number(m_exprValue
->u
.fval
);
1158 return OString::number(m_exprValue
->u
.dval
);
1160 return OString::number(m_exprValue
->u
.byval
);
1162 if ( m_exprValue
->u
.lval
== 0)
1163 return OString("FALSE");
1165 return OString("TRUE");
1172 switch (m_combOperator
)
1175 exprStr
+= OString("+");
1178 exprStr
+= OString("-");
1181 exprStr
+= OString("~");
1187 exprStr
+= m_subExpr1
->toString();
1188 switch (m_combOperator
)
1191 exprStr
+= OString(" + ");
1194 exprStr
+= OString(" - ");
1197 exprStr
+= OString(" * ");
1200 exprStr
+= OString(" / ");
1203 exprStr
+= OString(" % ");
1206 exprStr
+= OString(" | ");
1209 exprStr
+= OString(" ^ ");
1212 exprStr
+= OString(" & ");
1215 exprStr
+= OString(" << ");
1218 exprStr
+= OString(" >> ");
1225 exprStr
+= m_subExpr2
->toString();
1230 // Convert the type of an AST_Expression to a char *
1231 const sal_Char
* SAL_CALL
exprTypeToString(ExprType t
)
1238 return "unsigned short";
1242 return "unsigned long";
1246 return "unsigned hyper";
1272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */