Fix for UBSan build
[platform/upstream/doxygen.git] / src / constexp.y
1 /******************************************************************************
2  *
3  * $Id: constexp.y,v 1.6 2001/03/19 19:27:40 root Exp $
4  *
5  *
6  * Copyright (C) 1997-2012 by Dimitri van Heesch.
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation under the terms of the GNU General Public License is hereby 
10  * granted. No representations are made about the suitability of this software 
11  * for any purpose. It is provided "as is" without express or implied warranty.
12  * See the GNU General Public License for more details.
13  *
14  * Documents produced by Doxygen are derivative works derived from the
15  * input used in their production; they are not affected by this license.
16  *
17  */
18
19 %{
20
21 #include "cppvalue.h"
22 #include "constexp.h"
23 #include "message.h"
24
25 #if defined(_MSC_VER)
26 #define MSDOS
27 #endif
28
29 #define YYSTYPE CPPValue
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 int cppExpYYerror(const char *s)
35 {
36   warn(g_constExpFileName,g_constExpLineNr,
37        "warning: preprocessing issue while doing constant expression evaluation: %s",s);
38   return 0;
39 }
40
41 int cppExpYYlex();
42
43 %}
44
45 %token TOK_QUESTIONMARK
46 %token TOK_COLON
47 %token TOK_OR
48 %token TOK_AND
49 %token TOK_BITWISEOR
50 %token TOK_BITWISEXOR
51 %token TOK_AMPERSAND
52 %token TOK_NOTEQUAL
53 %token TOK_EQUAL
54 %token TOK_LESSTHAN
55 %token TOK_GREATERTHAN
56 %token TOK_LESSTHANOREQUALTO
57 %token TOK_GREATERTHANOREQUALTO
58 %token TOK_SHIFTLEFT
59 %token TOK_SHIFTRIGHT
60 %token TOK_PLUS
61 %token TOK_MINUS
62 %token TOK_STAR
63 %token TOK_DIVIDE
64 %token TOK_MOD
65 %token TOK_TILDE
66 %token TOK_NOT
67 %token TOK_LPAREN
68 %token TOK_RPAREN
69 %token TOK_OCTALINT
70 %token TOK_DECIMALINT
71 %token TOK_HEXADECIMALINT
72 %token TOK_CHARACTER
73 %token TOK_FLOAT
74
75 %%
76
77 start: constant_expression
78        { g_resultValue = $1; return 0; }
79 ;
80
81 constant_expression: logical_or_expression
82                      { $$ = $1; }
83                    | logical_or_expression 
84                      TOK_QUESTIONMARK logical_or_expression 
85                      TOK_COLON logical_or_expression
86                      {
87                        bool c = ($1.isInt() ? ((long)$1 != 0) : ((double)$1 != 0.0));
88                        $$ = c ? $3 : $5;
89                      }
90 ;
91
92 logical_or_expression: logical_and_expression
93                        { $$ = $1; }
94                      | logical_or_expression TOK_OR logical_and_expression
95                        {
96                          $$ = CPPValue( (long)((long)$1 || (long)$3) );
97                        }
98 ;
99
100 logical_and_expression: inclusive_or_expression
101                         { $$ = $1; }
102                       | logical_and_expression TOK_AND inclusive_or_expression
103                         {
104                           $$ = CPPValue( (long)((long)$1 && (long)$3) );
105                         }
106 ;
107
108 inclusive_or_expression: exclusive_or_expression
109                          { $$ = $1; }
110                        | inclusive_or_expression TOK_BITWISEOR 
111                          exclusive_or_expression
112                          { 
113                            $$ = CPPValue( (long)$1 | (long)$3 );
114                          }
115 ;
116
117 exclusive_or_expression: and_expression
118                          { $$ = $1; }
119                        | exclusive_or_expression TOK_BITWISEXOR and_expression
120                          {
121                            $$ = CPPValue( (long)$1 ^ (long)$3 );
122                          }
123 ;
124
125 and_expression: equality_expression
126                 { $$ = $1; }
127               | and_expression TOK_AMPERSAND equality_expression
128                 { 
129                   $$ = CPPValue( (long)$1 & (long)$3 );
130                 }
131 ;
132
133 equality_expression: relational_expression
134                      { $$ = $1; }
135                    | equality_expression TOK_EQUAL relational_expression
136                      { 
137                        $$ = CPPValue( (long)((double)$1 == (double)$3) );
138                      }
139                    | equality_expression TOK_NOTEQUAL relational_expression
140                      {
141                        $$ = CPPValue( (long)((double)$1 != (double)$3) );
142                      }
143 ;
144
145 relational_expression: shift_expression
146                        { $$ = $1; }
147                      | relational_expression TOK_LESSTHAN shift_expression
148                        { 
149                          $$ = CPPValue( (long)((double)$1 < (double)$3) );
150                        }
151                      | relational_expression TOK_GREATERTHAN shift_expression
152                        {
153                          $$ = CPPValue( (long)((double)$1 > (double)$3) );
154                        }
155                      | relational_expression TOK_LESSTHANOREQUALTO
156                        shift_expression
157                        {
158                          $$ = CPPValue( (long)((double)$1 <= (double)$3) );
159                        }
160                      | relational_expression TOK_GREATERTHANOREQUALTO
161                        shift_expression
162                        {
163                          $$ = CPPValue( (long)((double)$1 >= (double)$3) );
164                        }
165 ;
166
167 shift_expression: additive_expression
168                   { $$ = $1; }
169                 | shift_expression TOK_SHIFTLEFT additive_expression
170                   {
171                     $$ = CPPValue( (long)$1 << (long)$3 );      
172                   }
173                 | shift_expression TOK_SHIFTRIGHT additive_expression
174                   {
175                     $$ = CPPValue( (long)$1 >> (long)$3 );
176                   }
177 ;
178
179 additive_expression: multiplicative_expression
180                      { $$ = $1; }
181                    | additive_expression TOK_PLUS multiplicative_expression
182                      {
183                        if (!$1.isInt() || !$3.isInt())
184                        {
185                          $$ = CPPValue( (double)$1 + (double)$3 );
186                        }
187                        else     
188                        {
189                          $$ = CPPValue( (long)$1 + (long)$3 );
190                        }
191                      }
192                    | additive_expression TOK_MINUS multiplicative_expression
193                      {
194                        if (!$1.isInt() || !$3.isInt())
195                        {
196                          $$ = CPPValue( (double)$1 - (double)$3 );
197                        }
198                        else     
199                        {
200                          $$ = CPPValue( (long)$1 - (long)$3 );
201                        }
202                      }
203 ;
204
205 multiplicative_expression: unary_expression
206                            { $$ = $1; }
207                          | multiplicative_expression TOK_STAR unary_expression
208                            { 
209                              if (!$1.isInt() || !$3.isInt())
210                              {
211                                $$ = CPPValue( (double)$1 * (double)$3 );
212                              }
213                              else
214                              {
215                                $$ = CPPValue( (long)$1 * (long)$3 );
216                              }
217                            }
218                          | multiplicative_expression TOK_DIVIDE unary_expression
219                            { 
220                              if (!$1.isInt() || !$3.isInt())
221                              {
222                                $$ = CPPValue( (double)$1 / (double)$3 );
223                              }
224                              else
225                              {
226                                long value = $3;
227                                if (value==0) value=1;
228                                $$ = CPPValue( (long)$1 / value );
229                              }
230                            }
231                          | multiplicative_expression TOK_MOD unary_expression
232                            { 
233                              long value = $3;
234                              if (value==0) value=1;
235                              $$ = CPPValue( (long)$1 % value );
236                            }
237 ;
238
239 unary_expression: primary_expression
240                   { $$ = $1; }
241                 | TOK_PLUS unary_expression
242                   { $$ = $1; }
243                 | TOK_MINUS unary_expression
244                   { 
245                     if ($2.isInt()) 
246                       $$ = CPPValue(-(long)$2);
247                     else
248                       $$ = CPPValue(-(double)$2);
249                   }
250                 | TOK_TILDE unary_expression
251                   {
252                     $$ = CPPValue(~(long)$2);
253                   }
254                 | TOK_NOT unary_expression
255                   {
256                     $$ = CPPValue((long)!(long)$2);
257                   }
258 ;
259
260 primary_expression: constant
261                     { $$ = $1; }
262                   | TOK_LPAREN constant_expression TOK_RPAREN
263                     { $$ = $2; }
264 ;
265
266 constant: TOK_OCTALINT
267           { $$ = parseOctal(); }
268         | TOK_DECIMALINT
269           { $$ = parseDecimal(); }
270         | TOK_HEXADECIMALINT
271           { $$ = parseHexadecimal(); }
272         | TOK_CHARACTER
273           { $$ = parseCharacter(); }
274         | TOK_FLOAT
275           { $$ = parseFloat(); }
276 ;
277
278 %%