1 #ifndef EXPRESSION_H
   2 #define EXPRESSION_H
   3 /*
   4  * sparse/expression.h
   5  *
   6  * Copyright (C) 2003 Transmeta Corp.
   7  *               2003 Linus Torvalds
   8  *
   9  * Permission is hereby granted, free of charge, to any person obtaining a copy
  10  * of this software and associated documentation files (the "Software"), to deal
  11  * in the Software without restriction, including without limitation the rights
  12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13  * copies of the Software, and to permit persons to whom the Software is
  14  * furnished to do so, subject to the following conditions:
  15  *
  16  * The above copyright notice and this permission notice shall be included in
  17  * all copies or substantial portions of the Software.
  18  *
  19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25  * THE SOFTWARE.
  26  *
  27  * Declarations and helper functions for expression parsing.
  28  */
  29 
  30 #include "allocate.h"
  31 #include "lib.h"
  32 #include "symbol.h"
  33 
  34 struct expression_list;
  35 
  36 enum expression_type {
  37         EXPR_VALUE = 1,
  38         EXPR_STRING,
  39         EXPR_SYMBOL,
  40         EXPR_TYPE,
  41         EXPR_BINOP,
  42         EXPR_ASSIGNMENT,
  43         EXPR_LOGICAL,
  44         EXPR_DEREF,
  45         EXPR_PREOP,
  46         EXPR_POSTOP,
  47         EXPR_CAST,
  48         EXPR_FORCE_CAST,
  49         EXPR_IMPLIED_CAST,
  50         EXPR_SIZEOF,
  51         EXPR_ALIGNOF,
  52         EXPR_PTRSIZEOF,
  53         EXPR_CONDITIONAL,
  54         EXPR_SELECT,            // a "safe" conditional expression
  55         EXPR_STATEMENT,
  56         EXPR_CALL,
  57         EXPR_COMMA,
  58         EXPR_COMPARE,
  59         EXPR_LABEL,
  60         EXPR_INITIALIZER,       // initializer list
  61         EXPR_IDENTIFIER,        // identifier in initializer
  62         EXPR_INDEX,             // index in initializer
  63         EXPR_POS,               // position in initializer
  64         EXPR_FVALUE,
  65         EXPR_SLICE,
  66         EXPR_OFFSETOF,
  67 };
  68 
  69 
  70 /*
  71  * Flags for tracking the promotion of constness related attributes
  72  * from subexpressions to their parents.
  73  *
  74  * The flags are not independent as one might imply another.
  75  * The implications are as follows:
  76  * - CEF_INT, CEF_ENUM and
  77  *   CEF_CHAR imply CEF_ICE.
  78  *
  79  * Use the CEF_*_SET_MASK and CEF_*_CLEAR_MASK
  80  * helper macros defined below to set or clear one of these flags.
  81  */
  82 enum constexpr_flag {
  83         CEF_NONE = 0,
  84         /*
  85          * A constant in the sense of [6.4.4]:
  86          * - Integer constant [6.4.4.1]
  87          * - Floating point constant [6.4.4.2]
  88          * - Enumeration constant [6.4.4.3]
  89          * - Character constant [6.4.4.4]
  90          */
  91         CEF_INT = (1 << 0),
  92         CEF_FLOAT = (1 << 1),
  93         CEF_ENUM = (1 << 2),
  94         CEF_CHAR = (1 << 3),
  95 
  96         /*
  97          * A constant expression in the sense of [6.6]:
  98          * - integer constant expression [6.6(6)]
  99          * - arithmetic constant expression [6.6(8)]
 100          * - address constant [6.6(9)]
 101          */
 102         CEF_ICE = (1 << 4),
 103         CEF_ACE = (1 << 5),
 104         CEF_ADDR = (1 << 6),
 105 
 106         /* integer constant expression => arithmetic constant expression */
 107         CEF_SET_ICE = (CEF_ICE | CEF_ACE),
 108 
 109         /* integer constant => integer constant expression */
 110         CEF_SET_INT = (CEF_INT | CEF_SET_ICE),
 111 
 112         /* floating point constant => arithmetic constant expression */
 113         CEF_SET_FLOAT = (CEF_FLOAT | CEF_ACE),
 114 
 115         /* enumeration constant => integer constant expression */
 116         CEF_SET_ENUM = (CEF_ENUM | CEF_SET_ICE),
 117 
 118         /* character constant => integer constant expression */
 119         CEF_SET_CHAR = (CEF_CHAR | CEF_SET_ICE),
 120 
 121         /*
 122          * Remove any "Constant" [6.4.4] flag, but retain the "constant
 123          * expression" [6.6] flags.
 124          */
 125         CEF_CONST_MASK = (CEF_INT | CEF_FLOAT | CEF_CHAR),
 126 
 127         /*
 128          * not an integer constant expression => neither of integer,
 129          * enumeration and character constant
 130          */
 131         CEF_CLR_ICE = (CEF_ICE | CEF_INT | CEF_ENUM | CEF_CHAR),
 132 };
 133 
 134 enum {
 135         Handled = 1 << 0,
 136         Fake    = 1 << 1,
 137 }; /* for expr->flags */
 138 
 139 enum {
 140         Taint_comma = 1,
 141 }; /* for expr->taint */
 142 
 143 struct expression {
 144         enum expression_type type:8;
 145         unsigned flags:8;
 146         unsigned smatch_flags:16;
 147         int op;
 148         struct position pos;
 149         struct symbol *ctype;
 150         unsigned long parent;
 151         union {
 152                 // EXPR_VALUE
 153                 struct {
 154                         unsigned long long value;
 155                         unsigned taint;
 156                 };
 157 
 158                 // EXPR_FVALUE
 159                 long double fvalue;
 160 
 161                 // EXPR_STRING
 162                 struct {
 163                         int wide;
 164                         struct string *string;
 165                 };
 166 
 167                 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
 168                 struct /* unop */ {
 169                         struct expression *unop;
 170                         unsigned long op_value;
 171                 };
 172 
 173                 // EXPR_SYMBOL, EXPR_TYPE
 174                 struct /* symbol_arg */ {
 175                         struct symbol *symbol;
 176                         struct ident *symbol_name;
 177                 };
 178 
 179                 // EXPR_STATEMENT
 180                 struct statement *statement;
 181 
 182                 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
 183                 struct /* binop_arg */ {
 184                         struct expression *left, *right;
 185                 };
 186                 // EXPR_DEREF
 187                 struct /* deref_arg */ {
 188                         struct expression *deref;
 189                         struct ident *member;
 190                         int member_offset;
 191                 };
 192                 // EXPR_SLICE
 193                 struct /* slice */ {
 194                         struct expression *base;
 195                         unsigned r_bitpos, r_nrbits;
 196                 };
 197                 // EXPR_CAST and EXPR_SIZEOF
 198                 struct /* cast_arg */ {
 199                         struct symbol *cast_type;
 200                         struct expression *cast_expression;
 201                 };
 202                 // EXPR_CONDITIONAL
 203                 // EXPR_SELECT
 204                 struct /* conditional_expr */ {
 205                         struct expression *conditional, *cond_true, *cond_false;
 206                 };
 207                 // EXPR_CALL
 208                 struct /* call_expr */ {
 209                         struct expression *fn;
 210                         struct expression_list *args;
 211                 };
 212                 // EXPR_LABEL
 213                 struct /* label_expr */ {
 214                         struct symbol *label_symbol;
 215                 };
 216                 // EXPR_INITIALIZER
 217                 struct expression_list *expr_list;
 218                 // EXPR_IDENTIFIER
 219                 struct /* ident_expr */ {
 220                         int offset;
 221                         struct ident *expr_ident;
 222                         struct symbol *field;
 223                         struct expression *ident_expression;
 224                 };
 225                 // EXPR_INDEX
 226                 struct /* index_expr */ {
 227                         unsigned int idx_from, idx_to;
 228                         struct expression *idx_expression;
 229                 };
 230                 // EXPR_POS
 231                 struct /* initpos_expr */ {
 232                         unsigned int init_offset, init_nr;
 233                         struct expression *init_expr;
 234                 };
 235                 // EXPR_OFFSETOF
 236                 struct {
 237                         struct symbol *in;
 238                         struct expression *down;
 239                         union {
 240                                 struct ident *ident;
 241                                 struct expression *index;
 242                         };
 243                 };
 244         };
 245 };
 246 
 247 /* Constant expression values */
 248 int is_zero_constant(struct expression *);
 249 int expr_truth_value(struct expression *expr);
 250 long long get_expression_value(struct expression *);
 251 long long const_expression_value(struct expression *);
 252 long long get_expression_value_silent(struct expression *expr);
 253 
 254 /* Expression parsing */
 255 struct token *parse_expression(struct token *token, struct expression **tree);
 256 struct token *conditional_expression(struct token *token, struct expression **tree);
 257 struct token *primary_expression(struct token *token, struct expression **tree);
 258 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
 259 struct token *assignment_expression(struct token *token, struct expression **tree);
 260 
 261 extern void evaluate_symbol_list(struct symbol_list *list);
 262 extern struct symbol *evaluate_statement(struct statement *stmt);
 263 extern struct symbol *evaluate_expression(struct expression *);
 264 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset);
 265 
 266 extern int expand_symbol(struct symbol *);
 267 
 268 static inline struct expression *alloc_expression(struct position pos, int type)
 269 {
 270         struct expression *expr = __alloc_expression(0);
 271         expr->type = type;
 272         expr->pos = pos;
 273         expr->flags = CEF_NONE;
 274         return expr;
 275 }
 276 
 277 static inline struct expression *alloc_const_expression(struct position pos, int value)
 278 {
 279         struct expression *expr = __alloc_expression(0);
 280         expr->type = EXPR_VALUE;
 281         expr->pos = pos;
 282         expr->value = value;
 283         expr->ctype = &int_ctype;
 284         expr->flags = CEF_SET_INT;
 285         return expr;
 286 }
 287 
 288 /* Type name parsing */
 289 struct token *typename(struct token *, struct symbol **, int *);
 290 
 291 static inline int lookup_type(struct token *token)
 292 {
 293         if (token->pos.type == TOKEN_IDENT) {
 294                 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
 295                 return sym && (sym->namespace & NS_TYPEDEF);
 296         }
 297         return 0;
 298 }
 299 
 300 /* Statement parsing */
 301 struct statement *alloc_statement(struct position pos, int type);
 302 struct token *initializer(struct expression **tree, struct token *token);
 303 struct token *compound_statement(struct token *, struct statement *);
 304 
 305 /* The preprocessor calls this 'constant_expression()' */
 306 #define constant_expression(token,tree) conditional_expression(token, tree)
 307 
 308 /* Cast folding of constant values.. */
 309 void cast_value(struct expression *expr, struct symbol *newtype,
 310         struct expression *old, struct symbol *oldtype);
 311 
 312 #endif