Unified · Split
| @@ -3057,74 +3057,72 @@ static void post_type_args(CType *type, AttributeDef *ad) · 3056 unmodified lines | |||
| 3057 | AttributeDef ad1; | 3057 | AttributeDef ad1; |
| 3058 | CType pt; | 3058 | CType pt; |
| 3059 | 3059 | ||
| 3060 | if (tok == '(') { | 3060 | /* function declaration */ |
| 3061 | /* function declaration */ | 3061 | next(); |
| 3062 | next(); | 3062 | l = 0; |
| 3063 | l = 0; | 3063 | first = NULL; |
| 3064 | first = NULL; | 3064 | plast = &first; |
| 3065 | plast = &first; | 3065 | arg_size = 0; |
| 3066 | arg_size = 0; | 3066 | if (tok != ')') { |
| 3067 | if (tok != ')') { | 3067 | for(;;) { |
| 3068 | for(;;) { | 3068 | /* read param name and compute offset */ |
| 3069 | /* read param name and compute offset */ | 3069 | if (l != FUNC_OLD) { |
| 3070 | if (l != FUNC_OLD) { | 3070 | if (!parse_btype(&pt, &ad1)) { |
| 3071 | if (!parse_btype(&pt, &ad1)) { | 3071 | if (l) { |
| 3072 | if (l) { | 3072 | error("invalid type"); |
| 3073 | error("invalid type"); | 3073 | } else { |
| 3074 | } else { | 3074 | l = FUNC_OLD; |
| 3075 | l = FUNC_OLD; | 3075 | goto old_proto; |
| 3076 | goto old_proto; | ||
| 3077 | } | ||
| 3078 | } | 3076 | } |
| 3079 | l = FUNC_NEW; | ||
| 3080 | if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')') | ||
| 3081 | break; | ||
| 3082 | type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT); | ||
| 3083 | if ((pt.t & VT_BTYPE) == VT_VOID) | ||
| 3084 | error("parameter declared as void"); | ||
| 3085 | arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE; | ||
| 3086 | } else { | ||
| 3087 | old_proto: | ||
| 3088 | n = tok; | ||
| 3089 | if (n < TOK_UIDENT) | ||
| 3090 | expect("identifier"); | ||
| 3091 | pt.t = VT_INT; | ||
| 3092 | next(); | ||
| 3093 | } | 3077 | } |
| 3094 | convert_parameter_type(&pt); | 3078 | l = FUNC_NEW; |
| 3095 | s = sym_push(n | SYM_FIELD, &pt, 0, 0); | 3079 | if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')') |
| 3096 | *plast = s; | ||
| 3097 | plast = &s->next; | ||
| 3098 | if (tok == ')') | ||
| 3099 | break; | ||
| 3100 | skip(','); | ||
| 3101 | if (l == FUNC_NEW && tok == TOK_DOTS) { | ||
| 3102 | l = FUNC_ELLIPSIS; | ||
| 3103 | next(); | ||
| 3104 | break; | 3080 | break; |
| 3105 | } | 3081 | type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT); |
| 3082 | if ((pt.t & VT_BTYPE) == VT_VOID) | ||
| 3083 | error("parameter declared as void"); | ||
| 3084 | arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE; | ||
| 3085 | } else { | ||
| 3086 | old_proto: | ||
| 3087 | n = tok; | ||
| 3088 | if (n < TOK_UIDENT) | ||
| 3089 | expect("identifier"); | ||
| 3090 | pt.t = VT_INT; | ||
| 3091 | next(); | ||
| 3092 | } | ||
| 3093 | convert_parameter_type(&pt); | ||
| 3094 | s = sym_push(n | SYM_FIELD, &pt, 0, 0); | ||
| 3095 | *plast = s; | ||
| 3096 | plast = &s->next; | ||
| 3097 | if (tok == ')') | ||
| 3098 | break; | ||
| 3099 | skip(','); | ||
| 3100 | if (l == FUNC_NEW && tok == TOK_DOTS) { | ||
| 3101 | l = FUNC_ELLIPSIS; | ||
| 3102 | next(); | ||
| 3103 | break; | ||
| 3106 | } | 3104 | } |
| 3107 | } | 3105 | } |
| 3108 | /* if no parameters, then old type prototype */ | ||
| 3109 | if (l == 0) | ||
| 3110 | l = FUNC_OLD; | ||
| 3111 | skip(')'); | ||
| 3112 | t1 = type->t & VT_STORAGE; | ||
| 3113 | /* NOTE: const is ignored in returned type as it has a special | ||
| 3114 | meaning in gcc / C++ */ | ||
| 3115 | type->t &= ~(VT_STORAGE | VT_CONSTANT); | ||
| 3116 | /* some ancient pre-K&R C allows a function to return an array | ||
| 3117 | and the array brackets to be put after the arguments, such | ||
| 3118 | that "int c()[]" means the same as "int[] c()" */ | ||
| 3119 | if (tok == '[') | ||
| 3120 | post_type_array(type, ad); | ||
| 3121 | /* we push a anonymous symbol which will contain the function prototype */ | ||
| 3122 | ad->func_args = arg_size; | ||
| 3123 | s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l); | ||
| 3124 | s->next = first; | ||
| 3125 | type->t = t1 | VT_FUNC; | ||
| 3126 | type->ref = s; | ||
| 3127 | } | 3106 | } |
| 3107 | /* if no parameters, then old type prototype */ | ||
| 3108 | if (l == 0) | ||
| 3109 | l = FUNC_OLD; | ||
| 3110 | skip(')'); | ||
| 3111 | t1 = type->t & VT_STORAGE; | ||
| 3112 | /* NOTE: const is ignored in returned type as it has a special | ||
| 3113 | meaning in gcc / C++ */ | ||
| 3114 | type->t &= ~(VT_STORAGE | VT_CONSTANT); | ||
| 3115 | /* some ancient pre-K&R C allows a function to return an array | ||
| 3116 | and the array brackets to be put after the arguments, such | ||
| 3117 | that "int c()[]" means the same as "int[] c()" */ | ||
| 3118 | if (tok == '[') | ||
| 3119 | post_type_array(type, ad); | ||
| 3120 | /* we push a anonymous symbol which will contain the function prototype */ | ||
| 3121 | ad->func_args = arg_size; | ||
| 3122 | s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l); | ||
| 3123 | s->next = first; | ||
| 3124 | type->t = t1 | VT_FUNC; | ||
| 3125 | type->ref = s; | ||
| 3128 | } | 3126 | } |
| 3129 | 3127 | ||
| 3130 | static void post_type_array(CType *type, AttributeDef *ad) | 3128 | static void post_type_array(CType *type, AttributeDef *ad) |
| @@ -3132,32 +3130,30 @@ static void post_type_array(CType *type, AttributeDef *ad) · 1 unmodified line | |||
| 3132 | int n, t1; | 3130 | int n, t1; |
| 3133 | Sym *s; | 3131 | Sym *s; |
| 3134 | 3132 | ||
| 3135 | if (tok == '[') { | 3133 | /* array definition */ |
| 3136 | /* array definition */ | 3134 | next(); |
| 3135 | if (tok == TOK_RESTRICT1) | ||
| 3137 | next(); | 3136 | next(); |
| 3138 | if (tok == TOK_RESTRICT1) | 3137 | n = -1; |
| 3139 | next(); | 3138 | if (tok != ']') { |
| 3140 | n = -1; | 3139 | n = expr_const(); |
| 3141 | if (tok != ']') { | 3140 | if (n < 0) |
| 3142 | n = expr_const(); | 3141 | error("invalid array size"); |
| 3143 | if (n < 0) | 3142 | } |
| 3144 | error("invalid array size"); | 3143 | skip(']'); |
| 3145 | } | 3144 | /* parse next post type */ |
| 3146 | skip(']'); | 3145 | t1 = type->t & VT_STORAGE; |
| 3147 | /* parse next post type */ | 3146 | type->t &= ~VT_STORAGE; |
| 3148 | t1 = type->t & VT_STORAGE; | 3147 | if (tok == '[') |
| 3149 | type->t &= ~VT_STORAGE; | 3148 | post_type_array(type, ad); |
| 3150 | if (tok == '[') | 3149 | |
| 3151 | post_type_array(type, ad); | 3150 | /* we push a anonymous symbol which will contain the array |
| 3152 | 3151 | element type */ | |
| 3153 | /* we push a anonymous symbol which will contain the array | 3152 | s = sym_push(SYM_FIELD, type, 0, n); |
| 3154 | element type */ | ||
| 3155 | s = sym_push(SYM_FIELD, type, 0, n); | ||
| 3156 | if (n < 0) | 3153 | if (n < 0) |
| 3157 | ARRAY_RESIZE(s->r) = 1; | 3154 | ARRAY_RESIZE(s->r) = 1; |
| 3158 | type->t = t1 | VT_ARRAY | VT_PTR; | 3155 | type->t = t1 | VT_ARRAY | VT_PTR; |
| 3159 | type->ref = s; | 3156 | type->ref = s; |
| 3160 | } | ||
| 3161 | } | 3157 | } |
| 3162 | 3158 | ||
| 3163 | /* Parse a type declaration (except basic type), and return the type | 3159 | /* Parse a type declaration (except basic type), and return the type |