00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00036 #include "ctrump/ast/ast.h"
00037 #include "ctrump/common/abort.h"
00038
00039 enum {
00040 PREC_ASSIGN,
00041 PREC_CONDITIONAL,
00042 PREC_LOR,
00043 PREC_LAND,
00044 PREC_BOR,
00045 PREC_BXOR,
00046 PREC_BAND,
00047 PREC_EQ,
00048 PREC_REL,
00049 PREC_SHIFT,
00050 PREC_ADDITIVE,
00051 PREC_MULTIPLICATIVE,
00052 PREC_CAST,
00053 PREC_UNARY,
00054 PREC_POSTFIX,
00055 PREC_PRIMARY,
00056 PREC_PAREN,
00057 PREC_MAX
00058 };
00059
00060 int ctrump_expr_prec_table[] = {
00061 [CTRUMP_EXPR_PAREN] = PREC_PAREN,
00062
00063 [CTRUMP_EXPR_CONDITIONAL] = PREC_CONDITIONAL,
00064 [CTRUMP_EXPR_MEMBER_REF] = PREC_POSTFIX,
00065 [CTRUMP_EXPR_PTR_MEMBER_REF] = PREC_POSTFIX,
00066 [CTRUMP_EXPR_CALL] = PREC_POSTFIX,
00067
00068 [CTRUMP_EXPR_STR_LITERAL] = PREC_MAX,
00069 [CTRUMP_EXPR_FLOAT_LITERAL] = PREC_MAX,
00070 [CTRUMP_EXPR_DOUBLE_LITERAL] = PREC_MAX,
00071 [CTRUMP_EXPR_SINT_LITERAL] = PREC_MAX,
00072 [CTRUMP_EXPR_SLONG_LITERAL] = PREC_MAX,
00073 [CTRUMP_EXPR_SLLONG_LITERAL] = PREC_MAX,
00074 [CTRUMP_EXPR_UINT_LITERAL] = PREC_MAX,
00075 [CTRUMP_EXPR_ULONG_LITERAL] = PREC_MAX,
00076 [CTRUMP_EXPR_ULLONG_LITERAL] = PREC_MAX,
00077 [CTRUMP_EXPR_CAST] = PREC_CAST,
00078 [CTRUMP_EXPR_IMPLICIT_CAST] = PREC_MAX,
00079 [CTRUMP_EXPR_VARREF] = PREC_MAX,
00080
00081 [CTRUMP_EXPR_UNA_PTRREF] = PREC_UNARY,
00082 [CTRUMP_EXPR_UNA_POS] = PREC_UNARY,
00083 [CTRUMP_EXPR_UNA_NEG] = PREC_UNARY,
00084 [CTRUMP_EXPR_UNA_PRE_INC] = PREC_UNARY,
00085 [CTRUMP_EXPR_UNA_PRE_DEC] = PREC_UNARY,
00086 [CTRUMP_EXPR_UNA_ADDR] = PREC_UNARY,
00087 [CTRUMP_EXPR_UNA_SIZEOF] = PREC_UNARY,
00088
00089 [CTRUMP_EXPR_UNA_POST_INC] = PREC_POSTFIX,
00090 [CTRUMP_EXPR_UNA_POST_DEC] = PREC_POSTFIX,
00091
00092 [CTRUMP_EXPR_BIN_ADD] = PREC_ADDITIVE,
00093 [CTRUMP_EXPR_BIN_SUB] = PREC_ADDITIVE,
00094
00095 [CTRUMP_EXPR_BIN_MUL] = PREC_MULTIPLICATIVE,
00096 [CTRUMP_EXPR_BIN_DIV] = PREC_MULTIPLICATIVE,
00097 [CTRUMP_EXPR_BIN_MOD] = PREC_MULTIPLICATIVE,
00098
00099 [CTRUMP_EXPR_BIN_LAND] = PREC_LAND,
00100 [CTRUMP_EXPR_BIN_LOR] = PREC_LOR,
00101 [CTRUMP_EXPR_BIN_BAND] = PREC_BAND,
00102 [CTRUMP_EXPR_BIN_BOR] = PREC_BOR,
00103 [CTRUMP_EXPR_BIN_BXOR] = PREC_BXOR,
00104 [CTRUMP_EXPR_BIN_LSHIFT] = PREC_SHIFT,
00105 [CTRUMP_EXPR_BIN_RSHIFT] = PREC_SHIFT,
00106 [CTRUMP_EXPR_BIN_GT] = PREC_REL,
00107 [CTRUMP_EXPR_BIN_GE] = PREC_REL,
00108 [CTRUMP_EXPR_BIN_LT] = PREC_REL,
00109 [CTRUMP_EXPR_BIN_LE] = PREC_REL,
00110 [CTRUMP_EXPR_BIN_NE] = PREC_EQ,
00111 [CTRUMP_EXPR_BIN_EQ] = PREC_EQ,
00112
00113 [CTRUMP_EXPR_BIN_ASSIGN] = PREC_ASSIGN,
00114 [CTRUMP_EXPR_BIN_MUL_ASSIGN] = PREC_ASSIGN,
00115 [CTRUMP_EXPR_BIN_DIV_ASSIGN] = PREC_ASSIGN,
00116 [CTRUMP_EXPR_BIN_MOD_ASSIGN] = PREC_ASSIGN,
00117 [CTRUMP_EXPR_BIN_ADD_ASSIGN] = PREC_ASSIGN,
00118 [CTRUMP_EXPR_BIN_SUB_ASSIGN] = PREC_ASSIGN,
00119 [CTRUMP_EXPR_BIN_LSHIFT_ASSIGN] = PREC_ASSIGN,
00120 [CTRUMP_EXPR_BIN_RSHIFT_ASSIGN] = PREC_ASSIGN,
00121 [CTRUMP_EXPR_BIN_BAND_ASSIGN] = PREC_ASSIGN,
00122 [CTRUMP_EXPR_BIN_BOR_ASSIGN] = PREC_ASSIGN,
00123 [CTRUMP_EXPR_BIN_BXOR_ASSIGN] = PREC_ASSIGN,
00124
00125 [CTRUMP_EXPR_SIZEOF_TYPE] = PREC_UNARY,
00126
00127 [CTRUMP_EXPR_ARRREF] = PREC_POSTFIX,
00128 [CTRUMP_EXPR_MACRO_EXPAND] = PREC_MAX,
00129 [CTRUMP_EXPR_EMPTY] = PREC_MAX
00130 };
00131
00132 struct ctrump_location
00133 ctrump_get_stmt_loc(const struct ctrump_stmt *s)
00134 {
00135 struct ctrump_location ret;
00136
00137 switch (s->code) {
00138 case CTRUMP_STMT_LABELED:
00139 return s->u.labeled.label_loc;
00140 case CTRUMP_STMT_CASE:
00141 return s->u.case_.kwd_loc;
00142 case CTRUMP_STMT_DEFAULT:
00143 return s->u.default_.kwd_loc;
00144 case CTRUMP_STMT_COMPOUND:
00145 return s->u.compound.lbr_loc;
00146 case CTRUMP_STMT_IF:
00147 return s->u.if_.kwd_loc;
00148 case CTRUMP_STMT_IF_ELSE:
00149 return s->u.if_else.kwd_loc;
00150 case CTRUMP_STMT_EXPR:
00151
00152 return s->u.expr.semi_loc;
00153 case CTRUMP_STMT_DECL:
00154 return s->u.decl.spec.begin;
00155 case CTRUMP_STMT_SWITCH:
00156 return s->u.switch_.kwd_loc;
00157 case CTRUMP_STMT_FOR:
00158 return s->u.for_.kwd_loc;
00159 case CTRUMP_STMT_FOR_DECL:
00160 return s->u.for_decl.kwd_loc;
00161 case CTRUMP_STMT_WHILE:
00162 return s->u.while_.kwd_loc;
00163 case CTRUMP_STMT_DO_WHILE:
00164 return s->u.do_while.do_loc;
00165 case CTRUMP_STMT_GOTO:
00166 return s->u.goto_.kwd_loc;
00167 case CTRUMP_STMT_CONTINUE:
00168 return s->u.continue_.kwd_loc;
00169 case CTRUMP_STMT_BREAK:
00170 return s->u.break_.kwd_loc;
00171 case CTRUMP_STMT_RETURN_EXPR:
00172 return s->u.ret_expr.kwd_loc;
00173 case CTRUMP_STMT_RETURN:
00174 return s->u.return_.kwd_loc;
00175 case CTRUMP_STMT_EMPTY:
00176 return s->u.empty.semi_loc;
00177
00178 case CTRUMP_STMT_PRAGMA:
00179 case CTRUMP_STMT_SLASLA_COMMENT:
00180 case CTRUMP_STMT_SLAAST_COMMENT:
00181 case CTRUMP_STMT_IFDEF:
00182 case CTRUMP_STMT_ASM:
00183 case CTRUMP_STMT_DEFINE:
00184 case CTRUMP_STMT_UNDEF:
00185 case CTRUMP_STMT_NEWLINE:
00186 case CTRUMP_STMT_LIST:
00187 ctrump_fixme("get_stmt_location");
00188 break;
00189 }
00190
00191 LOCATION_GENERATED(ret);
00192
00193 return ret;
00194 }
00195
00196 int
00197 ctrump_expr_occur_var(const struct ctrump_expr *e, const struct ctrump_var *v)
00198 {
00199 int i, n;
00200 tailcall:
00201 switch (e->code) {
00202 case CTRUMP_CASE_ALL_BIN_EXPR:
00203 if (ctrump_expr_occur_var(e->u.binary.lhs, v))
00204 return 1;
00205 e = e->u.binary.rhs;
00206 goto tailcall;
00207
00208 case CTRUMP_EXPR_ARRREF:
00209 if (ctrump_expr_occur_var(e->u.arr_ref.array, v))
00210 return 1;
00211 e = e->u.arr_ref.subscript;
00212 goto tailcall;
00213
00214 case CTRUMP_EXPR_CONDITIONAL:
00215 if (ctrump_expr_occur_var(e->u.cond.cond, v)) return 1;
00216 if (ctrump_expr_occur_var(e->u.cond.then_, v)) return 1;
00217 e = e->u.cond.else_;
00218 goto tailcall;
00219
00220 case CTRUMP_EXPR_MEMBER_REF:
00221 case CTRUMP_EXPR_PTR_MEMBER_REF:
00222 e = e->u.member_ref.obj_expr;
00223 goto tailcall;
00224
00225 case CTRUMP_EXPR_CALL:
00226 n = e->u.call.num_args;
00227 for (i=0; i<n; i++) {
00228 if (ctrump_expr_occur_var(&e->u.call.args[i], v)) return 1;
00229 }
00230 e = e->u.call.func_expr;
00231 goto tailcall;
00232
00233 case CTRUMP_EXPR_CAST:
00234 case CTRUMP_EXPR_IMPLICIT_CAST:
00235 e = e->u.cast.expr;
00236 goto tailcall;
00237
00238 case CTRUMP_EXPR_PAREN:
00239 e = e->u.paren.expr;
00240 goto tailcall;
00241
00242 case CTRUMP_EXPR_VARREF:
00243 return (e->u.varref.var == v);
00244
00245 case CTRUMP_CASE_ALL_UNARY_EXPR:
00246 e = e->u.unary.expr;
00247 goto tailcall;
00248
00249 case CTRUMP_EXPR_INITIALIZER:
00250 ctrump_fixme("check occur for compound literal");
00251 return 0;
00252
00253 case CTRUMP_EXPR_MACRO_EXPAND:
00254 e = e->u.macro_expand.expanded;
00255 goto tailcall;
00256
00257 case CTRUMP_EXPR_IVTMP:
00258 e = e->u.ivtmp.original_expr;
00259 goto tailcall;
00260
00261 case CTRUMP_EXPR_EMPTY:
00262 return 0;
00263
00264 case CTRUMP_CASE_CONSTANT_TERM:
00265 return 0;
00266
00267 case CTRUMP_EXPR_TEXT:
00268 ctrump_unreachable("invalid expr code");
00269 }
00270 return 0;
00271 }
00272