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/env.h"
00037 #include "ctrump/common/varray.h"
00038 #include <stdlib.h>
00039
00043 static struct ctrump_texpr *
00044 handle_type_spec(struct ctrump_typespec *spec)
00045 {
00046 struct ctrump_texpr *t;
00047
00048 switch (spec->code) {
00049 case CTRUMP_TYPESPEC_BUILTIN:
00050 t = spec->u.builtin.texpr;
00051 break;
00052
00053 case CTRUMP_TYPESPEC_UNTYPED_SIGNED_INT:
00054 t = &ctrump_builtin_sint_type;
00055 break;
00056
00057 case CTRUMP_TYPESPEC_TYPEDEF_NAME:
00058 t = spec->u.typedef_name.texpr;
00059 break;
00060
00061 case CTRUMP_TYPESPEC_STRUCT_NAME:
00062 case CTRUMP_TYPESPEC_UNION_NAME:
00063 t = spec->u.struct_name.texpr;
00064 break;
00065
00066 case CTRUMP_TYPESPEC_ANON_STRUCT_DEFINITION:
00067 case CTRUMP_TYPESPEC_ANON_UNION_DEFINITION:
00068 t = spec->u.anon_struct_definition.def.record_type;
00069 break;
00070
00071 case CTRUMP_TYPESPEC_STRUCT_DEFINITION:
00072 case CTRUMP_TYPESPEC_UNION_DEFINITION:
00073 t = spec->u.struct_definition.def.record_type;
00074 break;
00075
00076
00077 default:
00078 abort();
00079 }
00080
00081 return t;
00082 }
00083
00084 static void
00085 ctrump_env_handle_decl(const struct ctrump_symbol **ret_name,
00086 struct ctrump_texpr **ret_type,
00087 struct ctrump_type_env *type_env,
00088 const struct ctrump_declarator *decl,
00089 struct ctrump_decl_specifier *declspec);
00090
00091
00095 static void
00096 ctrump_env_handle_decl_node(const struct ctrump_symbol **ret_name,
00097 struct ctrump_texpr **ret_type,
00098 struct ctrump_type_env *type_env,
00099 const struct ctrump_declarator_node *cur_node,
00100 struct ctrump_decl_specifier *declspec)
00101 {
00102 struct ctrump_texpr *cur_t, *t;
00103 const struct ctrump_symbol *var_name = NULL;
00104 struct ctrump_texpr *spec_t;
00105 int size_val;
00106 int is_const;
00107
00108 spec_t = handle_type_spec(&declspec->type_spec);
00109 spec_t = ctrump_get_qualified_type(type_env, spec_t, declspec->qual_flags);
00110 cur_t = ctrump_get_qualified_type(type_env, spec_t, declspec->qual_flags);
00111
00112
00113 while (1) {
00114 switch (cur_node->code) {
00115 case CTRUMP_DECLARATOR_IDENTIFIER:
00116 var_name = cur_node->u.identifier.identifier;
00117 goto next;
00118
00119 case CTRUMP_DECLARATOR_POINTER:
00120 t = ctrump_get_pointer_type(type_env, cur_t);
00121 cur_t = ctrump_get_qualified_attr_type(type_env, t, cur_node->u.pointer.qual_flags, &cur_node->u.pointer.attr);
00122 cur_node = cur_node->u.pointer.decl;
00123 break;
00124
00125 case CTRUMP_DECLARATOR_ARRAY:
00126 size_val = ctrump_ast_fold_const_sint(&cur_node->u.array.size, &is_const);
00127 if (!is_const) {
00128
00129 abort();
00130 }
00131 cur_t = ctrump_get_array_type(type_env, cur_t, size_val);
00132 cur_node = cur_node->u.array.decl;
00133 break;
00134
00135 case CTRUMP_DECLARATOR_PARAM_TYPELIST: {
00136 struct ctrump_texpr *ret_type = cur_t;
00137 struct ctrump_get_functype_iterator iter;
00138 int num_args = cur_node->u.param_typelist.num_args;
00139 int i;
00140 struct ctrump_texpr *arg_type, *fun_type;
00141 const struct ctrump_symbol *arg_name;
00142 struct ctrump_declarator_param *args = cur_node->u.param_typelist.args;
00143
00144 ctrump_get_functype_begin(&iter, type_env, ret_type, num_args);
00145 for (i=0; i<num_args; i++) {
00146 struct ctrump_decl_specifier *spec = &args[i].decl_spec;
00147 struct ctrump_declarator *decl = args[i].decl;
00148
00149 ctrump_env_handle_decl(&arg_name, &arg_type,
00150 type_env, decl, spec);
00151 ctrump_get_functype_next(type_env, &iter, arg_type);
00152 }
00153
00154 fun_type = ctrump_get_functype_end(type_env, &iter, cur_node->u.param_typelist.has_unspecified_arg);
00155 cur_t = fun_type;
00156 cur_node = cur_node->u.param_typelist.decl;
00157 }
00158 break;
00159
00160 case CTRUMP_DECLARATOR_PAREN:
00161 cur_node = cur_node->u.paren.decl;
00162 break;
00163
00164 case CTRUMP_DECLARATOR_EMPTY:
00165 case CTRUMP_DECLARATOR_VARLEN_ARRAY:
00166 case CTRUMP_DECLARATOR_INCOMPLETE_ARRAY:
00167 case CTRUMP_DECLARATOR_PARAM_IDENTIFIER_LIST:
00168
00169 abort();
00170 }
00171 }
00172
00173 next:
00174 *ret_name = var_name;
00175 *ret_type = cur_t;
00176 }
00177
00181 static void
00182 ctrump_env_handle_decl(const struct ctrump_symbol **ret_name,
00183 struct ctrump_texpr **ret_type,
00184 struct ctrump_type_env *type_env,
00185 const struct ctrump_declarator *decl,
00186 struct ctrump_decl_specifier *declspec)
00187 {
00188 struct ctrump_texpr *decl_type;
00189 ctrump_env_handle_decl_node(ret_name, &decl_type, type_env, &decl->nodes, declspec);
00190 *ret_type = ctrump_get_qualified_attr_type(type_env, decl_type, 0, &decl->attr);
00191 }
00192
00193 void
00194 ctrump_env_handle_record_decls(struct ctrump_record_type *record_type,
00195 struct ctrump_type_env *type_env,
00196 struct ctrump_struct_decl *decls,
00197 int num_decl,
00198 int num_field,
00199 struct ctrump_gccext_attribute *attr,
00200 struct ctrump_location *loc,
00201 struct ctrump_mempool *tree_pool)
00202 {
00203 int decl_idx, field_idx=0;
00204 struct ctrump_record_type_field *fields;
00205
00206 fields = ctrump_mempool_alloc(tree_pool, sizeof(*fields)*num_field);
00207 for (decl_idx=0; decl_idx<num_decl; decl_idx++) {
00208 int j;
00209 struct ctrump_struct_decl *decl = &decls[decl_idx];
00210 if (decl->code == CTRUMP_STRUCT_DECL_FIELDS) {
00211 struct ctrump_decl_specifier *declspec = &decl->u.decl.spec;
00212 struct ctrump_struct_declarator *declarators = decl->u.decl.decls;
00213 int num_declarator = decl->u.decl.num_decls;
00214 for (j=0; j<num_declarator; j++) {
00215 const struct ctrump_symbol *name;
00216 struct ctrump_texpr *texpr;
00217
00218 ctrump_env_handle_decl(&name, &texpr,
00219 type_env, declarators[j].decl, declspec);
00220
00221 fields[field_idx].name = name;
00222 fields[field_idx].type = texpr;
00223 fields[field_idx].bitfield_width = CTRUMP_BITFIELD_UNSPECIFIED;
00224 fields[field_idx].attr.flags = 0;
00225 fields[field_idx].attr.aligned = CTRUMP_ALIGN_UNSPECIFIED;
00226
00227 field_idx++;
00228 }
00229 }
00230 }
00231
00232 ctrump_complete_struct_type(record_type, num_field, fields, attr);
00233 }
00234