1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * Stupid C parser, version 1e-6. 3f08c3bdfSopenharmony_ci * 4f08c3bdfSopenharmony_ci * Let's see how hard this is to do. 5f08c3bdfSopenharmony_ci * 6f08c3bdfSopenharmony_ci * Copyright (C) 2003 Transmeta Corp. 7f08c3bdfSopenharmony_ci * 2003-2004 Linus Torvalds 8f08c3bdfSopenharmony_ci * Copyright (C) 2004 Christopher Li 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 11f08c3bdfSopenharmony_ci * of this software and associated documentation files (the "Software"), to deal 12f08c3bdfSopenharmony_ci * in the Software without restriction, including without limitation the rights 13f08c3bdfSopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14f08c3bdfSopenharmony_ci * copies of the Software, and to permit persons to whom the Software is 15f08c3bdfSopenharmony_ci * furnished to do so, subject to the following conditions: 16f08c3bdfSopenharmony_ci * 17f08c3bdfSopenharmony_ci * The above copyright notice and this permission notice shall be included in 18f08c3bdfSopenharmony_ci * all copies or substantial portions of the Software. 19f08c3bdfSopenharmony_ci * 20f08c3bdfSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21f08c3bdfSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22f08c3bdfSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23f08c3bdfSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24f08c3bdfSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25f08c3bdfSopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26f08c3bdfSopenharmony_ci * THE SOFTWARE. 27f08c3bdfSopenharmony_ci */ 28f08c3bdfSopenharmony_ci 29f08c3bdfSopenharmony_ci#include <stdarg.h> 30f08c3bdfSopenharmony_ci#include <stdlib.h> 31f08c3bdfSopenharmony_ci#include <stdio.h> 32f08c3bdfSopenharmony_ci#include <string.h> 33f08c3bdfSopenharmony_ci#include <ctype.h> 34f08c3bdfSopenharmony_ci#include <unistd.h> 35f08c3bdfSopenharmony_ci#include <fcntl.h> 36f08c3bdfSopenharmony_ci#include <limits.h> 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_ci#include "lib.h" 39f08c3bdfSopenharmony_ci#include "allocate.h" 40f08c3bdfSopenharmony_ci#include "token.h" 41f08c3bdfSopenharmony_ci#include "parse.h" 42f08c3bdfSopenharmony_ci#include "symbol.h" 43f08c3bdfSopenharmony_ci#include "scope.h" 44f08c3bdfSopenharmony_ci#include "expression.h" 45f08c3bdfSopenharmony_ci#include "target.h" 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_cistatic struct symbol_list **function_symbol_list; 48f08c3bdfSopenharmony_cistruct symbol_list *function_computed_target_list; 49f08c3bdfSopenharmony_cistruct statement_list *function_computed_goto_list; 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_cistatic struct token *statement(struct token *token, struct statement **tree); 52f08c3bdfSopenharmony_cistatic struct token *handle_attributes(struct token *token, struct decl_state *ctx); 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_citypedef struct token *declarator_t(struct token *, struct symbol *, struct decl_state *); 55f08c3bdfSopenharmony_cistatic declarator_t 56f08c3bdfSopenharmony_ci struct_specifier, union_specifier, enum_specifier, 57f08c3bdfSopenharmony_ci attribute_specifier, typeof_specifier, 58f08c3bdfSopenharmony_ci storage_specifier, thread_specifier; 59f08c3bdfSopenharmony_cistatic declarator_t generic_qualifier; 60f08c3bdfSopenharmony_cistatic declarator_t autotype_specifier; 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_cistatic struct token *parse_if_statement(struct token *token, struct statement *stmt); 63f08c3bdfSopenharmony_cistatic struct token *parse_return_statement(struct token *token, struct statement *stmt); 64f08c3bdfSopenharmony_cistatic struct token *parse_loop_iterator(struct token *token, struct statement *stmt); 65f08c3bdfSopenharmony_cistatic struct token *parse_default_statement(struct token *token, struct statement *stmt); 66f08c3bdfSopenharmony_cistatic struct token *parse_case_statement(struct token *token, struct statement *stmt); 67f08c3bdfSopenharmony_cistatic struct token *parse_switch_statement(struct token *token, struct statement *stmt); 68f08c3bdfSopenharmony_cistatic struct token *parse_for_statement(struct token *token, struct statement *stmt); 69f08c3bdfSopenharmony_cistatic struct token *parse_while_statement(struct token *token, struct statement *stmt); 70f08c3bdfSopenharmony_cistatic struct token *parse_do_statement(struct token *token, struct statement *stmt); 71f08c3bdfSopenharmony_cistatic struct token *parse_goto_statement(struct token *token, struct statement *stmt); 72f08c3bdfSopenharmony_cistatic struct token *parse_context_statement(struct token *token, struct statement *stmt); 73f08c3bdfSopenharmony_cistatic struct token *parse_range_statement(struct token *token, struct statement *stmt); 74f08c3bdfSopenharmony_cistatic struct token *parse_asm_statement(struct token *token, struct statement *stmt); 75f08c3bdfSopenharmony_cistatic struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list); 76f08c3bdfSopenharmony_cistatic struct token *parse_static_assert(struct token *token, struct symbol_list **unused); 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_citypedef struct token *attr_t(struct token *, struct symbol *, 79f08c3bdfSopenharmony_ci struct decl_state *); 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_cistatic attr_t 82f08c3bdfSopenharmony_ci attribute_packed, attribute_aligned, attribute_modifier, 83f08c3bdfSopenharmony_ci attribute_function, 84f08c3bdfSopenharmony_ci attribute_bitwise, 85f08c3bdfSopenharmony_ci attribute_address_space, attribute_context, 86f08c3bdfSopenharmony_ci attribute_designated_init, 87f08c3bdfSopenharmony_ci attribute_transparent_union, ignore_attribute, 88f08c3bdfSopenharmony_ci attribute_mode, attribute_force; 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_citypedef struct symbol *to_mode_t(struct symbol *); 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_cistatic to_mode_t 93f08c3bdfSopenharmony_ci to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode; 94f08c3bdfSopenharmony_cistatic to_mode_t to_pointer_mode, to_word_mode; 95f08c3bdfSopenharmony_ci 96f08c3bdfSopenharmony_cienum { 97f08c3bdfSopenharmony_ci Set_T = 1, 98f08c3bdfSopenharmony_ci Set_S = 2, 99f08c3bdfSopenharmony_ci Set_Char = 4, 100f08c3bdfSopenharmony_ci Set_Int = 8, 101f08c3bdfSopenharmony_ci Set_Double = 16, 102f08c3bdfSopenharmony_ci Set_Float = 32, 103f08c3bdfSopenharmony_ci Set_Signed = 64, 104f08c3bdfSopenharmony_ci Set_Unsigned = 128, 105f08c3bdfSopenharmony_ci Set_Short = 256, 106f08c3bdfSopenharmony_ci Set_Long = 512, 107f08c3bdfSopenharmony_ci Set_Vlong = 1024, 108f08c3bdfSopenharmony_ci Set_Int128 = 2048, 109f08c3bdfSopenharmony_ci Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned 110f08c3bdfSopenharmony_ci}; 111f08c3bdfSopenharmony_ci 112f08c3bdfSopenharmony_cienum { 113f08c3bdfSopenharmony_ci CInt = 0, CSInt, CUInt, CReal, 114f08c3bdfSopenharmony_ci}; 115f08c3bdfSopenharmony_ci 116f08c3bdfSopenharmony_cistatic void asm_modifier(struct token *token, unsigned long *mods, unsigned long mod) 117f08c3bdfSopenharmony_ci{ 118f08c3bdfSopenharmony_ci if (*mods & mod) 119f08c3bdfSopenharmony_ci warning(token->pos, "duplicated asm modifier"); 120f08c3bdfSopenharmony_ci *mods |= mod; 121f08c3bdfSopenharmony_ci} 122f08c3bdfSopenharmony_ci 123f08c3bdfSopenharmony_cistatic struct symbol_op typedef_op = { 124f08c3bdfSopenharmony_ci .type = KW_MODIFIER, 125f08c3bdfSopenharmony_ci .declarator = storage_specifier, 126f08c3bdfSopenharmony_ci}; 127f08c3bdfSopenharmony_ci 128f08c3bdfSopenharmony_cistatic struct symbol_op inline_op = { 129f08c3bdfSopenharmony_ci .type = KW_MODIFIER, 130f08c3bdfSopenharmony_ci .declarator = generic_qualifier, 131f08c3bdfSopenharmony_ci .asm_modifier = asm_modifier, 132f08c3bdfSopenharmony_ci}; 133f08c3bdfSopenharmony_ci 134f08c3bdfSopenharmony_cistatic struct symbol_op noreturn_op = { 135f08c3bdfSopenharmony_ci .type = KW_MODIFIER, 136f08c3bdfSopenharmony_ci .declarator = generic_qualifier, 137f08c3bdfSopenharmony_ci}; 138f08c3bdfSopenharmony_ci 139f08c3bdfSopenharmony_cistatic declarator_t alignas_specifier; 140f08c3bdfSopenharmony_cistatic struct symbol_op alignas_op = { 141f08c3bdfSopenharmony_ci .type = KW_MODIFIER, 142f08c3bdfSopenharmony_ci .declarator = alignas_specifier, 143f08c3bdfSopenharmony_ci}; 144f08c3bdfSopenharmony_ci 145f08c3bdfSopenharmony_cistatic struct symbol_op auto_op = { 146f08c3bdfSopenharmony_ci .type = KW_MODIFIER, 147f08c3bdfSopenharmony_ci .declarator = storage_specifier, 148f08c3bdfSopenharmony_ci}; 149f08c3bdfSopenharmony_ci 150f08c3bdfSopenharmony_cistatic struct symbol_op register_op = { 151f08c3bdfSopenharmony_ci .type = KW_MODIFIER, 152f08c3bdfSopenharmony_ci .declarator = storage_specifier, 153f08c3bdfSopenharmony_ci}; 154f08c3bdfSopenharmony_ci 155f08c3bdfSopenharmony_cistatic struct symbol_op static_op = { 156f08c3bdfSopenharmony_ci .type = KW_MODIFIER|KW_STATIC, 157f08c3bdfSopenharmony_ci .declarator = storage_specifier, 158f08c3bdfSopenharmony_ci}; 159f08c3bdfSopenharmony_ci 160f08c3bdfSopenharmony_cistatic struct symbol_op extern_op = { 161f08c3bdfSopenharmony_ci .type = KW_MODIFIER, 162f08c3bdfSopenharmony_ci .declarator = storage_specifier, 163f08c3bdfSopenharmony_ci}; 164f08c3bdfSopenharmony_ci 165f08c3bdfSopenharmony_cistatic struct symbol_op thread_op = { 166f08c3bdfSopenharmony_ci .type = KW_MODIFIER, 167f08c3bdfSopenharmony_ci .declarator = thread_specifier, 168f08c3bdfSopenharmony_ci}; 169f08c3bdfSopenharmony_ci 170f08c3bdfSopenharmony_cistatic struct symbol_op const_op = { 171f08c3bdfSopenharmony_ci .type = KW_QUALIFIER, 172f08c3bdfSopenharmony_ci .declarator = generic_qualifier, 173f08c3bdfSopenharmony_ci}; 174f08c3bdfSopenharmony_ci 175f08c3bdfSopenharmony_cistatic struct symbol_op volatile_op = { 176f08c3bdfSopenharmony_ci .type = KW_QUALIFIER, 177f08c3bdfSopenharmony_ci .declarator = generic_qualifier, 178f08c3bdfSopenharmony_ci .asm_modifier = asm_modifier, 179f08c3bdfSopenharmony_ci}; 180f08c3bdfSopenharmony_ci 181f08c3bdfSopenharmony_cistatic struct symbol_op restrict_op = { 182f08c3bdfSopenharmony_ci .type = KW_QUALIFIER, 183f08c3bdfSopenharmony_ci .declarator = generic_qualifier, 184f08c3bdfSopenharmony_ci}; 185f08c3bdfSopenharmony_ci 186f08c3bdfSopenharmony_cistatic struct symbol_op atomic_op = { 187f08c3bdfSopenharmony_ci .type = KW_QUALIFIER, 188f08c3bdfSopenharmony_ci .declarator = generic_qualifier, 189f08c3bdfSopenharmony_ci}; 190f08c3bdfSopenharmony_ci 191f08c3bdfSopenharmony_cistatic struct symbol_op typeof_op = { 192f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 193f08c3bdfSopenharmony_ci .declarator = typeof_specifier, 194f08c3bdfSopenharmony_ci .test = Set_Any, 195f08c3bdfSopenharmony_ci .set = Set_S|Set_T, 196f08c3bdfSopenharmony_ci}; 197f08c3bdfSopenharmony_ci 198f08c3bdfSopenharmony_cistatic struct symbol_op autotype_op = { 199f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 200f08c3bdfSopenharmony_ci .declarator = autotype_specifier, 201f08c3bdfSopenharmony_ci .test = Set_Any, 202f08c3bdfSopenharmony_ci .set = Set_S|Set_T, 203f08c3bdfSopenharmony_ci}; 204f08c3bdfSopenharmony_ci 205f08c3bdfSopenharmony_cistatic struct symbol_op attribute_op = { 206f08c3bdfSopenharmony_ci .type = KW_ATTRIBUTE, 207f08c3bdfSopenharmony_ci .declarator = attribute_specifier, 208f08c3bdfSopenharmony_ci}; 209f08c3bdfSopenharmony_ci 210f08c3bdfSopenharmony_cistatic struct symbol_op struct_op = { 211f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 212f08c3bdfSopenharmony_ci .declarator = struct_specifier, 213f08c3bdfSopenharmony_ci .test = Set_Any, 214f08c3bdfSopenharmony_ci .set = Set_S|Set_T, 215f08c3bdfSopenharmony_ci}; 216f08c3bdfSopenharmony_ci 217f08c3bdfSopenharmony_cistatic struct symbol_op union_op = { 218f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 219f08c3bdfSopenharmony_ci .declarator = union_specifier, 220f08c3bdfSopenharmony_ci .test = Set_Any, 221f08c3bdfSopenharmony_ci .set = Set_S|Set_T, 222f08c3bdfSopenharmony_ci}; 223f08c3bdfSopenharmony_ci 224f08c3bdfSopenharmony_cistatic struct symbol_op enum_op = { 225f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 226f08c3bdfSopenharmony_ci .declarator = enum_specifier, 227f08c3bdfSopenharmony_ci .test = Set_Any, 228f08c3bdfSopenharmony_ci .set = Set_S|Set_T, 229f08c3bdfSopenharmony_ci}; 230f08c3bdfSopenharmony_ci 231f08c3bdfSopenharmony_cistatic struct symbol_op spec_op = { 232f08c3bdfSopenharmony_ci .type = KW_SPECIFIER | KW_EXACT, 233f08c3bdfSopenharmony_ci .test = Set_Any, 234f08c3bdfSopenharmony_ci .set = Set_S|Set_T, 235f08c3bdfSopenharmony_ci}; 236f08c3bdfSopenharmony_ci 237f08c3bdfSopenharmony_cistatic struct symbol_op char_op = { 238f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 239f08c3bdfSopenharmony_ci .test = Set_T|Set_Long|Set_Short, 240f08c3bdfSopenharmony_ci .set = Set_T|Set_Char, 241f08c3bdfSopenharmony_ci .class = CInt, 242f08c3bdfSopenharmony_ci}; 243f08c3bdfSopenharmony_ci 244f08c3bdfSopenharmony_cistatic struct symbol_op int_op = { 245f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 246f08c3bdfSopenharmony_ci .test = Set_T, 247f08c3bdfSopenharmony_ci .set = Set_T|Set_Int, 248f08c3bdfSopenharmony_ci}; 249f08c3bdfSopenharmony_ci 250f08c3bdfSopenharmony_cistatic struct symbol_op double_op = { 251f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 252f08c3bdfSopenharmony_ci .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong, 253f08c3bdfSopenharmony_ci .set = Set_T|Set_Double, 254f08c3bdfSopenharmony_ci .class = CReal, 255f08c3bdfSopenharmony_ci}; 256f08c3bdfSopenharmony_ci 257f08c3bdfSopenharmony_cistatic struct symbol_op float_op = { 258f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 259f08c3bdfSopenharmony_ci .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long, 260f08c3bdfSopenharmony_ci .set = Set_T|Set_Float, 261f08c3bdfSopenharmony_ci .class = CReal, 262f08c3bdfSopenharmony_ci}; 263f08c3bdfSopenharmony_ci 264f08c3bdfSopenharmony_cistatic struct symbol_op short_op = { 265f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 266f08c3bdfSopenharmony_ci .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short, 267f08c3bdfSopenharmony_ci .set = Set_Short, 268f08c3bdfSopenharmony_ci}; 269f08c3bdfSopenharmony_ci 270f08c3bdfSopenharmony_cistatic struct symbol_op signed_op = { 271f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 272f08c3bdfSopenharmony_ci .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned, 273f08c3bdfSopenharmony_ci .set = Set_Signed, 274f08c3bdfSopenharmony_ci .class = CSInt, 275f08c3bdfSopenharmony_ci}; 276f08c3bdfSopenharmony_ci 277f08c3bdfSopenharmony_cistatic struct symbol_op unsigned_op = { 278f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 279f08c3bdfSopenharmony_ci .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned, 280f08c3bdfSopenharmony_ci .set = Set_Unsigned, 281f08c3bdfSopenharmony_ci .class = CUInt, 282f08c3bdfSopenharmony_ci}; 283f08c3bdfSopenharmony_ci 284f08c3bdfSopenharmony_cistatic struct symbol_op long_op = { 285f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 286f08c3bdfSopenharmony_ci .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong, 287f08c3bdfSopenharmony_ci .set = Set_Long, 288f08c3bdfSopenharmony_ci}; 289f08c3bdfSopenharmony_ci 290f08c3bdfSopenharmony_cistatic struct symbol_op int128_op = { 291f08c3bdfSopenharmony_ci .type = KW_SPECIFIER, 292f08c3bdfSopenharmony_ci .test = Set_S|Set_T|Set_Char|Set_Short|Set_Int|Set_Float|Set_Double|Set_Long|Set_Vlong|Set_Int128, 293f08c3bdfSopenharmony_ci .set = Set_T|Set_Int128|Set_Vlong, 294f08c3bdfSopenharmony_ci .class = CInt, 295f08c3bdfSopenharmony_ci}; 296f08c3bdfSopenharmony_ci 297f08c3bdfSopenharmony_cistatic struct symbol_op if_op = { 298f08c3bdfSopenharmony_ci .statement = parse_if_statement, 299f08c3bdfSopenharmony_ci}; 300f08c3bdfSopenharmony_ci 301f08c3bdfSopenharmony_cistatic struct symbol_op return_op = { 302f08c3bdfSopenharmony_ci .statement = parse_return_statement, 303f08c3bdfSopenharmony_ci}; 304f08c3bdfSopenharmony_ci 305f08c3bdfSopenharmony_cistatic struct symbol_op loop_iter_op = { 306f08c3bdfSopenharmony_ci .statement = parse_loop_iterator, 307f08c3bdfSopenharmony_ci}; 308f08c3bdfSopenharmony_ci 309f08c3bdfSopenharmony_cistatic struct symbol_op default_op = { 310f08c3bdfSopenharmony_ci .statement = parse_default_statement, 311f08c3bdfSopenharmony_ci}; 312f08c3bdfSopenharmony_ci 313f08c3bdfSopenharmony_cistatic struct symbol_op case_op = { 314f08c3bdfSopenharmony_ci .statement = parse_case_statement, 315f08c3bdfSopenharmony_ci}; 316f08c3bdfSopenharmony_ci 317f08c3bdfSopenharmony_cistatic struct symbol_op switch_op = { 318f08c3bdfSopenharmony_ci .statement = parse_switch_statement, 319f08c3bdfSopenharmony_ci}; 320f08c3bdfSopenharmony_ci 321f08c3bdfSopenharmony_cistatic struct symbol_op for_op = { 322f08c3bdfSopenharmony_ci .statement = parse_for_statement, 323f08c3bdfSopenharmony_ci}; 324f08c3bdfSopenharmony_ci 325f08c3bdfSopenharmony_cistatic struct symbol_op while_op = { 326f08c3bdfSopenharmony_ci .statement = parse_while_statement, 327f08c3bdfSopenharmony_ci}; 328f08c3bdfSopenharmony_ci 329f08c3bdfSopenharmony_cistatic struct symbol_op do_op = { 330f08c3bdfSopenharmony_ci .statement = parse_do_statement, 331f08c3bdfSopenharmony_ci}; 332f08c3bdfSopenharmony_ci 333f08c3bdfSopenharmony_cistatic struct symbol_op goto_op = { 334f08c3bdfSopenharmony_ci .statement = parse_goto_statement, 335f08c3bdfSopenharmony_ci}; 336f08c3bdfSopenharmony_ci 337f08c3bdfSopenharmony_cistatic struct symbol_op __context___op = { 338f08c3bdfSopenharmony_ci .statement = parse_context_statement, 339f08c3bdfSopenharmony_ci .attribute = attribute_context, 340f08c3bdfSopenharmony_ci}; 341f08c3bdfSopenharmony_ci 342f08c3bdfSopenharmony_cistatic struct symbol_op range_op = { 343f08c3bdfSopenharmony_ci .statement = parse_range_statement, 344f08c3bdfSopenharmony_ci}; 345f08c3bdfSopenharmony_ci 346f08c3bdfSopenharmony_cistatic struct symbol_op asm_op = { 347f08c3bdfSopenharmony_ci .type = KW_ASM, 348f08c3bdfSopenharmony_ci .statement = parse_asm_statement, 349f08c3bdfSopenharmony_ci .toplevel = toplevel_asm_declaration, 350f08c3bdfSopenharmony_ci}; 351f08c3bdfSopenharmony_ci 352f08c3bdfSopenharmony_cistatic struct symbol_op static_assert_op = { 353f08c3bdfSopenharmony_ci .toplevel = parse_static_assert, 354f08c3bdfSopenharmony_ci}; 355f08c3bdfSopenharmony_ci 356f08c3bdfSopenharmony_cistatic struct symbol_op packed_op = { 357f08c3bdfSopenharmony_ci .attribute = attribute_packed, 358f08c3bdfSopenharmony_ci}; 359f08c3bdfSopenharmony_ci 360f08c3bdfSopenharmony_cistatic struct symbol_op aligned_op = { 361f08c3bdfSopenharmony_ci .attribute = attribute_aligned, 362f08c3bdfSopenharmony_ci}; 363f08c3bdfSopenharmony_ci 364f08c3bdfSopenharmony_cistatic struct symbol_op attr_mod_op = { 365f08c3bdfSopenharmony_ci .attribute = attribute_modifier, 366f08c3bdfSopenharmony_ci}; 367f08c3bdfSopenharmony_ci 368f08c3bdfSopenharmony_cistatic struct symbol_op attr_fun_op = { 369f08c3bdfSopenharmony_ci .attribute = attribute_function, 370f08c3bdfSopenharmony_ci}; 371f08c3bdfSopenharmony_ci 372f08c3bdfSopenharmony_cistatic struct symbol_op attr_bitwise_op = { 373f08c3bdfSopenharmony_ci .attribute = attribute_bitwise, 374f08c3bdfSopenharmony_ci}; 375f08c3bdfSopenharmony_ci 376f08c3bdfSopenharmony_cistatic struct symbol_op attr_force_op = { 377f08c3bdfSopenharmony_ci .attribute = attribute_force, 378f08c3bdfSopenharmony_ci}; 379f08c3bdfSopenharmony_ci 380f08c3bdfSopenharmony_cistatic struct symbol_op address_space_op = { 381f08c3bdfSopenharmony_ci .attribute = attribute_address_space, 382f08c3bdfSopenharmony_ci}; 383f08c3bdfSopenharmony_ci 384f08c3bdfSopenharmony_cistatic struct symbol_op mode_op = { 385f08c3bdfSopenharmony_ci .attribute = attribute_mode, 386f08c3bdfSopenharmony_ci}; 387f08c3bdfSopenharmony_ci 388f08c3bdfSopenharmony_cistatic struct symbol_op context_op = { 389f08c3bdfSopenharmony_ci .attribute = attribute_context, 390f08c3bdfSopenharmony_ci}; 391f08c3bdfSopenharmony_ci 392f08c3bdfSopenharmony_cistatic struct symbol_op designated_init_op = { 393f08c3bdfSopenharmony_ci .attribute = attribute_designated_init, 394f08c3bdfSopenharmony_ci}; 395f08c3bdfSopenharmony_ci 396f08c3bdfSopenharmony_cistatic struct symbol_op transparent_union_op = { 397f08c3bdfSopenharmony_ci .attribute = attribute_transparent_union, 398f08c3bdfSopenharmony_ci}; 399f08c3bdfSopenharmony_ci 400f08c3bdfSopenharmony_cistatic struct symbol_op ignore_attr_op = { 401f08c3bdfSopenharmony_ci .attribute = ignore_attribute, 402f08c3bdfSopenharmony_ci}; 403f08c3bdfSopenharmony_ci 404f08c3bdfSopenharmony_cistatic struct symbol_op mode_QI_op = { 405f08c3bdfSopenharmony_ci .type = KW_MODE, 406f08c3bdfSopenharmony_ci .to_mode = to_QI_mode 407f08c3bdfSopenharmony_ci}; 408f08c3bdfSopenharmony_ci 409f08c3bdfSopenharmony_cistatic struct symbol_op mode_HI_op = { 410f08c3bdfSopenharmony_ci .type = KW_MODE, 411f08c3bdfSopenharmony_ci .to_mode = to_HI_mode 412f08c3bdfSopenharmony_ci}; 413f08c3bdfSopenharmony_ci 414f08c3bdfSopenharmony_cistatic struct symbol_op mode_SI_op = { 415f08c3bdfSopenharmony_ci .type = KW_MODE, 416f08c3bdfSopenharmony_ci .to_mode = to_SI_mode 417f08c3bdfSopenharmony_ci}; 418f08c3bdfSopenharmony_ci 419f08c3bdfSopenharmony_cistatic struct symbol_op mode_DI_op = { 420f08c3bdfSopenharmony_ci .type = KW_MODE, 421f08c3bdfSopenharmony_ci .to_mode = to_DI_mode 422f08c3bdfSopenharmony_ci}; 423f08c3bdfSopenharmony_ci 424f08c3bdfSopenharmony_cistatic struct symbol_op mode_TI_op = { 425f08c3bdfSopenharmony_ci .type = KW_MODE, 426f08c3bdfSopenharmony_ci .to_mode = to_TI_mode 427f08c3bdfSopenharmony_ci}; 428f08c3bdfSopenharmony_ci 429f08c3bdfSopenharmony_cistatic struct symbol_op mode_pointer_op = { 430f08c3bdfSopenharmony_ci .type = KW_MODE, 431f08c3bdfSopenharmony_ci .to_mode = to_pointer_mode 432f08c3bdfSopenharmony_ci}; 433f08c3bdfSopenharmony_ci 434f08c3bdfSopenharmony_cistatic struct symbol_op mode_word_op = { 435f08c3bdfSopenharmony_ci .type = KW_MODE, 436f08c3bdfSopenharmony_ci .to_mode = to_word_mode 437f08c3bdfSopenharmony_ci}; 438f08c3bdfSopenharmony_ci 439f08c3bdfSopenharmony_ci/* 440f08c3bdfSopenharmony_ci * Define the keyword and their effects. 441f08c3bdfSopenharmony_ci * The entries in the 'typedef' and put in NS_TYPEDEF and 442f08c3bdfSopenharmony_ci * are automatically set as reserved keyword while the ones 443f08c3bdfSopenharmony_ci * in the 'keyword' table are just put in NS_KEYWORD. 444f08c3bdfSopenharmony_ci * 445f08c3bdfSopenharmony_ci * The entries are added via the 3 macros: 446f08c3bdfSopenharmony_ci * N() for entries with "name" only, 447f08c3bdfSopenharmony_ci * D() for entries with "name" & "__name__", 448f08c3bdfSopenharmony_ci * A() for entries with "name", "__name" & "__name__", 449f08c3bdfSopenharmony_ci * U() for entries with "__name" & "__name__". 450f08c3bdfSopenharmony_ci */ 451f08c3bdfSopenharmony_cistatic struct init_keyword { 452f08c3bdfSopenharmony_ci const char *name; 453f08c3bdfSopenharmony_ci struct symbol_op *op; 454f08c3bdfSopenharmony_ci struct symbol *type; 455f08c3bdfSopenharmony_ci unsigned long mods; 456f08c3bdfSopenharmony_ci} typedefs[] = { 457f08c3bdfSopenharmony_ci#define N(I, O,...) { I, O,##__VA_ARGS__ } 458f08c3bdfSopenharmony_ci#define D(I, O,...) N(I,O,##__VA_ARGS__ ), \ 459f08c3bdfSopenharmony_ci N("__" I "__",O,##__VA_ARGS__) 460f08c3bdfSopenharmony_ci#define A(I, O,...) N(I,O,##__VA_ARGS__ ), \ 461f08c3bdfSopenharmony_ci N("__" I,O,##__VA_ARGS__), \ 462f08c3bdfSopenharmony_ci N("__" I "__",O,##__VA_ARGS__) 463f08c3bdfSopenharmony_ci#define U(I, O,...) N("__" I,O,##__VA_ARGS__), \ 464f08c3bdfSopenharmony_ci N("__" I "__",O,##__VA_ARGS__) 465f08c3bdfSopenharmony_ci /* Storage classes */ 466f08c3bdfSopenharmony_ci N("auto", &auto_op, .mods = MOD_AUTO), 467f08c3bdfSopenharmony_ci N("register", ®ister_op, .mods = MOD_REGISTER), 468f08c3bdfSopenharmony_ci N("static", &static_op, .mods = MOD_STATIC), 469f08c3bdfSopenharmony_ci N("extern", &extern_op, .mods = MOD_EXTERN), 470f08c3bdfSopenharmony_ci N("__thread", &thread_op), 471f08c3bdfSopenharmony_ci N("_Thread_local", &thread_op), 472f08c3bdfSopenharmony_ci 473f08c3bdfSopenharmony_ci A("inline", &inline_op, .mods = MOD_INLINE), 474f08c3bdfSopenharmony_ci 475f08c3bdfSopenharmony_ci /* Typedef ... */ 476f08c3bdfSopenharmony_ci N("typedef", &typedef_op, .mods = MOD_USERTYPE), 477f08c3bdfSopenharmony_ci A("typeof", &typeof_op), 478f08c3bdfSopenharmony_ci N("__auto_type", &autotype_op), 479f08c3bdfSopenharmony_ci 480f08c3bdfSopenharmony_ci /* Type qualifiers */ 481f08c3bdfSopenharmony_ci A("const", &const_op, .mods = MOD_CONST), 482f08c3bdfSopenharmony_ci A("volatile", &volatile_op, .mods = MOD_VOLATILE), 483f08c3bdfSopenharmony_ci A("restrict", &restrict_op, .mods = MOD_RESTRICT), 484f08c3bdfSopenharmony_ci 485f08c3bdfSopenharmony_ci N("_Atomic", &atomic_op, .mods = MOD_ATOMIC), 486f08c3bdfSopenharmony_ci N("_Noreturn", &noreturn_op, .mods = MOD_NORETURN), 487f08c3bdfSopenharmony_ci N("_Alignas", &alignas_op), 488f08c3bdfSopenharmony_ci 489f08c3bdfSopenharmony_ci U("attribute", &attribute_op), 490f08c3bdfSopenharmony_ci 491f08c3bdfSopenharmony_ci /* Type specifiers */ 492f08c3bdfSopenharmony_ci N("struct", &struct_op), 493f08c3bdfSopenharmony_ci N("union", &union_op), 494f08c3bdfSopenharmony_ci N("enum", &enum_op), 495f08c3bdfSopenharmony_ci 496f08c3bdfSopenharmony_ci N("void", &spec_op, .type = &void_ctype), 497f08c3bdfSopenharmony_ci N("char", &char_op), 498f08c3bdfSopenharmony_ci N("short", &short_op), 499f08c3bdfSopenharmony_ci N("int", &int_op), 500f08c3bdfSopenharmony_ci N("long", &long_op), 501f08c3bdfSopenharmony_ci N("float", &float_op), 502f08c3bdfSopenharmony_ci N("double", &double_op), 503f08c3bdfSopenharmony_ci A("signed", &signed_op), 504f08c3bdfSopenharmony_ci N("unsigned", &unsigned_op), 505f08c3bdfSopenharmony_ci N("__int128", &int128_op), 506f08c3bdfSopenharmony_ci N("_Bool", &spec_op, .type = &bool_ctype), 507f08c3bdfSopenharmony_ci 508f08c3bdfSopenharmony_ci /* Predeclared types */ 509f08c3bdfSopenharmony_ci N("__builtin_va_list", &spec_op, .type = &ptr_ctype), 510f08c3bdfSopenharmony_ci N("__builtin_ms_va_list",&spec_op, .type = &ptr_ctype), 511f08c3bdfSopenharmony_ci N("__int128_t", &spec_op, .type = &sint128_ctype), 512f08c3bdfSopenharmony_ci N("__uint128_t", &spec_op, .type = &uint128_ctype), 513f08c3bdfSopenharmony_ci N("_Float32", &spec_op, .type = &float32_ctype), 514f08c3bdfSopenharmony_ci N("_Float32x", &spec_op, .type = &float32x_ctype), 515f08c3bdfSopenharmony_ci N("_Float64", &spec_op, .type = &float64_ctype), 516f08c3bdfSopenharmony_ci N("_Float64x", &spec_op, .type = &float64x_ctype), 517f08c3bdfSopenharmony_ci N("_Float128", &spec_op, .type = &float128_ctype), 518f08c3bdfSopenharmony_ci}, keywords[] = { 519f08c3bdfSopenharmony_ci /* Statements */ 520f08c3bdfSopenharmony_ci N("if", &if_op), 521f08c3bdfSopenharmony_ci N("return", &return_op), 522f08c3bdfSopenharmony_ci N("break", &loop_iter_op), 523f08c3bdfSopenharmony_ci N("continue", &loop_iter_op), 524f08c3bdfSopenharmony_ci N("default", &default_op), 525f08c3bdfSopenharmony_ci N("case", &case_op), 526f08c3bdfSopenharmony_ci N("switch", &switch_op), 527f08c3bdfSopenharmony_ci N("for", &for_op), 528f08c3bdfSopenharmony_ci N("while", &while_op), 529f08c3bdfSopenharmony_ci N("do", &do_op), 530f08c3bdfSopenharmony_ci N("goto", &goto_op), 531f08c3bdfSopenharmony_ci A("asm", &asm_op), 532f08c3bdfSopenharmony_ci N("context", &context_op), 533f08c3bdfSopenharmony_ci N("__context__", &__context___op), 534f08c3bdfSopenharmony_ci N("__range__", &range_op), 535f08c3bdfSopenharmony_ci N("_Static_assert", &static_assert_op), 536f08c3bdfSopenharmony_ci 537f08c3bdfSopenharmony_ci /* Attributes */ 538f08c3bdfSopenharmony_ci D("packed", &packed_op), 539f08c3bdfSopenharmony_ci D("aligned", &aligned_op), 540f08c3bdfSopenharmony_ci D("nocast", &attr_mod_op, .mods = MOD_NOCAST), 541f08c3bdfSopenharmony_ci D("noderef", &attr_mod_op, .mods = MOD_NODEREF), 542f08c3bdfSopenharmony_ci D("safe", &attr_mod_op, .mods = MOD_SAFE), 543f08c3bdfSopenharmony_ci D("unused", &attr_mod_op, .mods = MOD_UNUSED), 544f08c3bdfSopenharmony_ci D("externally_visible", &attr_mod_op, .mods = MOD_EXT_VISIBLE), 545f08c3bdfSopenharmony_ci D("force", &attr_force_op), 546f08c3bdfSopenharmony_ci D("bitwise", &attr_bitwise_op, .mods = MOD_BITWISE), 547f08c3bdfSopenharmony_ci D("address_space", &address_space_op), 548f08c3bdfSopenharmony_ci D("designated_init", &designated_init_op), 549f08c3bdfSopenharmony_ci D("transparent_union", &transparent_union_op), 550f08c3bdfSopenharmony_ci D("noreturn", &attr_fun_op, .mods = MOD_NORETURN), 551f08c3bdfSopenharmony_ci D("pure", &attr_fun_op, .mods = MOD_PURE), 552f08c3bdfSopenharmony_ci A("const", &attr_fun_op, .mods = MOD_PURE), 553f08c3bdfSopenharmony_ci D("gnu_inline", &attr_fun_op, .mods = MOD_GNU_INLINE), 554f08c3bdfSopenharmony_ci 555f08c3bdfSopenharmony_ci /* Modes */ 556f08c3bdfSopenharmony_ci D("mode", &mode_op), 557f08c3bdfSopenharmony_ci D("QI", &mode_QI_op), 558f08c3bdfSopenharmony_ci D("HI", &mode_HI_op), 559f08c3bdfSopenharmony_ci D("SI", &mode_SI_op), 560f08c3bdfSopenharmony_ci D("DI", &mode_DI_op), 561f08c3bdfSopenharmony_ci D("TI", &mode_TI_op), 562f08c3bdfSopenharmony_ci D("byte", &mode_QI_op), 563f08c3bdfSopenharmony_ci D("pointer", &mode_pointer_op), 564f08c3bdfSopenharmony_ci D("word", &mode_word_op), 565f08c3bdfSopenharmony_ci}; 566f08c3bdfSopenharmony_ci 567f08c3bdfSopenharmony_ci 568f08c3bdfSopenharmony_cistatic const char *ignored_attributes[] = { 569f08c3bdfSopenharmony_ci 570f08c3bdfSopenharmony_ci#define GCC_ATTR(x) \ 571f08c3bdfSopenharmony_ci STRINGIFY(x), \ 572f08c3bdfSopenharmony_ci STRINGIFY(__##x##__), 573f08c3bdfSopenharmony_ci 574f08c3bdfSopenharmony_ci#include "gcc-attr-list.h" 575f08c3bdfSopenharmony_ci 576f08c3bdfSopenharmony_ci#undef GCC_ATTR 577f08c3bdfSopenharmony_ci 578f08c3bdfSopenharmony_ci "bounded", 579f08c3bdfSopenharmony_ci "__bounded__", 580f08c3bdfSopenharmony_ci "__noclone", 581f08c3bdfSopenharmony_ci "__nonnull", 582f08c3bdfSopenharmony_ci "__nothrow", 583f08c3bdfSopenharmony_ci}; 584f08c3bdfSopenharmony_ci 585f08c3bdfSopenharmony_ci 586f08c3bdfSopenharmony_cistatic void init_keyword(int stream, struct init_keyword *kw, enum namespace ns) 587f08c3bdfSopenharmony_ci{ 588f08c3bdfSopenharmony_ci struct symbol *sym = create_symbol(stream, kw->name, SYM_KEYWORD, ns); 589f08c3bdfSopenharmony_ci sym->ident->keyword = 1; 590f08c3bdfSopenharmony_ci sym->ident->reserved |= (ns == NS_TYPEDEF); 591f08c3bdfSopenharmony_ci sym->ctype.modifiers = kw->mods; 592f08c3bdfSopenharmony_ci sym->ctype.base_type = kw->type; 593f08c3bdfSopenharmony_ci sym->op = kw->op; 594f08c3bdfSopenharmony_ci} 595f08c3bdfSopenharmony_ci 596f08c3bdfSopenharmony_civoid init_parser(int stream) 597f08c3bdfSopenharmony_ci{ 598f08c3bdfSopenharmony_ci int i; 599f08c3bdfSopenharmony_ci 600f08c3bdfSopenharmony_ci for (i = 0; i < ARRAY_SIZE(typedefs); i++) 601f08c3bdfSopenharmony_ci init_keyword(stream, &typedefs[i], NS_TYPEDEF); 602f08c3bdfSopenharmony_ci for (i = 0; i < ARRAY_SIZE(keywords); i++) 603f08c3bdfSopenharmony_ci init_keyword(stream, &keywords[i], NS_KEYWORD); 604f08c3bdfSopenharmony_ci 605f08c3bdfSopenharmony_ci for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) { 606f08c3bdfSopenharmony_ci const char * name = ignored_attributes[i]; 607f08c3bdfSopenharmony_ci struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD, 608f08c3bdfSopenharmony_ci NS_KEYWORD); 609f08c3bdfSopenharmony_ci if (!sym->op) { 610f08c3bdfSopenharmony_ci sym->ident->keyword = 1; 611f08c3bdfSopenharmony_ci sym->op = &ignore_attr_op; 612f08c3bdfSopenharmony_ci } 613f08c3bdfSopenharmony_ci } 614f08c3bdfSopenharmony_ci} 615f08c3bdfSopenharmony_ci 616f08c3bdfSopenharmony_ci 617f08c3bdfSopenharmony_cistatic struct token *skip_to(struct token *token, int op) 618f08c3bdfSopenharmony_ci{ 619f08c3bdfSopenharmony_ci while (!match_op(token, op) && !eof_token(token)) 620f08c3bdfSopenharmony_ci token = token->next; 621f08c3bdfSopenharmony_ci return token; 622f08c3bdfSopenharmony_ci} 623f08c3bdfSopenharmony_ci 624f08c3bdfSopenharmony_cistatic struct token bad_token = { .pos.type = TOKEN_BAD }; 625f08c3bdfSopenharmony_cistruct token *expect(struct token *token, int op, const char *where) 626f08c3bdfSopenharmony_ci{ 627f08c3bdfSopenharmony_ci if (!match_op(token, op)) { 628f08c3bdfSopenharmony_ci if (token != &bad_token) { 629f08c3bdfSopenharmony_ci bad_token.next = token; 630f08c3bdfSopenharmony_ci sparse_error(token->pos, "Expected %s %s", show_special(op), where); 631f08c3bdfSopenharmony_ci sparse_error(token->pos, "got %s", show_token(token)); 632f08c3bdfSopenharmony_ci } 633f08c3bdfSopenharmony_ci if (op == ';') 634f08c3bdfSopenharmony_ci return skip_to(token, op); 635f08c3bdfSopenharmony_ci return &bad_token; 636f08c3bdfSopenharmony_ci } 637f08c3bdfSopenharmony_ci return token->next; 638f08c3bdfSopenharmony_ci} 639f08c3bdfSopenharmony_ci 640f08c3bdfSopenharmony_ci/// 641f08c3bdfSopenharmony_ci// issue an error message on new parsing errors 642f08c3bdfSopenharmony_ci// @token: the current token 643f08c3bdfSopenharmony_ci// @errmsg: the error message 644f08c3bdfSopenharmony_ci// If the current token is from a previous error, an error message 645f08c3bdfSopenharmony_ci// has already been issued, so nothing more is done. 646f08c3bdfSopenharmony_ci// Otherwise, @errmsg is displayed followed by the current token. 647f08c3bdfSopenharmony_cistatic void unexpected(struct token *token, const char *errmsg) 648f08c3bdfSopenharmony_ci{ 649f08c3bdfSopenharmony_ci if (token == &bad_token) 650f08c3bdfSopenharmony_ci return; 651f08c3bdfSopenharmony_ci sparse_error(token->pos, "%s", errmsg); 652f08c3bdfSopenharmony_ci sparse_error(token->pos, "got %s", show_token(token)); 653f08c3bdfSopenharmony_ci} 654f08c3bdfSopenharmony_ci 655f08c3bdfSopenharmony_ci// Add a symbol to the list of function-local symbols 656f08c3bdfSopenharmony_cistatic void fn_local_symbol(struct symbol *sym) 657f08c3bdfSopenharmony_ci{ 658f08c3bdfSopenharmony_ci if (function_symbol_list) 659f08c3bdfSopenharmony_ci add_symbol(function_symbol_list, sym); 660f08c3bdfSopenharmony_ci} 661f08c3bdfSopenharmony_ci 662f08c3bdfSopenharmony_cistruct statement *alloc_statement(struct position pos, int type) 663f08c3bdfSopenharmony_ci{ 664f08c3bdfSopenharmony_ci struct statement *stmt = __alloc_statement(0); 665f08c3bdfSopenharmony_ci stmt->type = type; 666f08c3bdfSopenharmony_ci stmt->pos = pos; 667f08c3bdfSopenharmony_ci return stmt; 668f08c3bdfSopenharmony_ci} 669f08c3bdfSopenharmony_ci 670f08c3bdfSopenharmony_cistatic struct token *struct_declaration_list(struct token *token, struct symbol_list **list); 671f08c3bdfSopenharmony_ci 672f08c3bdfSopenharmony_cistatic void apply_ctype(struct position pos, struct ctype *dst, struct ctype *src); 673f08c3bdfSopenharmony_ci 674f08c3bdfSopenharmony_cistatic void apply_modifiers(struct position pos, struct decl_state *ctx) 675f08c3bdfSopenharmony_ci{ 676f08c3bdfSopenharmony_ci struct symbol *ctype; 677f08c3bdfSopenharmony_ci if (!ctx->mode) 678f08c3bdfSopenharmony_ci return; 679f08c3bdfSopenharmony_ci ctype = ctx->mode->to_mode(ctx->ctype.base_type); 680f08c3bdfSopenharmony_ci if (!ctype) 681f08c3bdfSopenharmony_ci sparse_error(pos, "don't know how to apply mode to %s", 682f08c3bdfSopenharmony_ci show_typename(ctx->ctype.base_type)); 683f08c3bdfSopenharmony_ci else 684f08c3bdfSopenharmony_ci ctx->ctype.base_type = ctype; 685f08c3bdfSopenharmony_ci 686f08c3bdfSopenharmony_ci} 687f08c3bdfSopenharmony_ci 688f08c3bdfSopenharmony_cistatic struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type) 689f08c3bdfSopenharmony_ci{ 690f08c3bdfSopenharmony_ci struct symbol *sym = alloc_symbol(pos, type); 691f08c3bdfSopenharmony_ci 692f08c3bdfSopenharmony_ci sym->ctype.base_type = ctype->base_type; 693f08c3bdfSopenharmony_ci sym->ctype.modifiers = ctype->modifiers; 694f08c3bdfSopenharmony_ci 695f08c3bdfSopenharmony_ci ctype->base_type = sym; 696f08c3bdfSopenharmony_ci ctype->modifiers = 0; 697f08c3bdfSopenharmony_ci return sym; 698f08c3bdfSopenharmony_ci} 699f08c3bdfSopenharmony_ci 700f08c3bdfSopenharmony_ci/* 701f08c3bdfSopenharmony_ci * NOTE! NS_LABEL is not just a different namespace, 702f08c3bdfSopenharmony_ci * it also ends up using function scope instead of the 703f08c3bdfSopenharmony_ci * regular symbol scope. 704f08c3bdfSopenharmony_ci */ 705f08c3bdfSopenharmony_cistruct symbol *label_symbol(struct token *token, int used) 706f08c3bdfSopenharmony_ci{ 707f08c3bdfSopenharmony_ci struct symbol *sym = lookup_symbol(token->ident, NS_LABEL); 708f08c3bdfSopenharmony_ci if (!sym) { 709f08c3bdfSopenharmony_ci sym = alloc_symbol(token->pos, SYM_LABEL); 710f08c3bdfSopenharmony_ci bind_symbol(sym, token->ident, NS_LABEL); 711f08c3bdfSopenharmony_ci if (used) 712f08c3bdfSopenharmony_ci sym->used = 1; 713f08c3bdfSopenharmony_ci fn_local_symbol(sym); 714f08c3bdfSopenharmony_ci } 715f08c3bdfSopenharmony_ci return sym; 716f08c3bdfSopenharmony_ci} 717f08c3bdfSopenharmony_ci 718f08c3bdfSopenharmony_cistatic struct token *struct_union_enum_specifier(enum type type, 719f08c3bdfSopenharmony_ci struct token *token, struct decl_state *ctx, 720f08c3bdfSopenharmony_ci struct token *(*parse)(struct token *, struct symbol *)) 721f08c3bdfSopenharmony_ci{ 722f08c3bdfSopenharmony_ci struct decl_state attr = { }; 723f08c3bdfSopenharmony_ci struct symbol *sym; 724f08c3bdfSopenharmony_ci struct position *repos; 725f08c3bdfSopenharmony_ci 726f08c3bdfSopenharmony_ci token = handle_attributes(token, &attr); 727f08c3bdfSopenharmony_ci if (token_type(token) == TOKEN_IDENT) { 728f08c3bdfSopenharmony_ci sym = lookup_symbol(token->ident, NS_STRUCT); 729f08c3bdfSopenharmony_ci if (!sym || 730f08c3bdfSopenharmony_ci (is_outer_scope(sym->scope) && 731f08c3bdfSopenharmony_ci (match_op(token->next,';') || match_op(token->next,'{')))) { 732f08c3bdfSopenharmony_ci // Either a new symbol, or else an out-of-scope 733f08c3bdfSopenharmony_ci // symbol being redefined. 734f08c3bdfSopenharmony_ci sym = alloc_symbol(token->pos, type); 735f08c3bdfSopenharmony_ci bind_symbol(sym, token->ident, NS_STRUCT); 736f08c3bdfSopenharmony_ci } 737f08c3bdfSopenharmony_ci if (sym->type != type) 738f08c3bdfSopenharmony_ci error_die(token->pos, "invalid tag applied to %s", show_typename (sym)); 739f08c3bdfSopenharmony_ci ctx->ctype.base_type = sym; 740f08c3bdfSopenharmony_ci repos = &token->pos; 741f08c3bdfSopenharmony_ci token = token->next; 742f08c3bdfSopenharmony_ci if (!match_op(token, '{')) 743f08c3bdfSopenharmony_ci return token; 744f08c3bdfSopenharmony_ci 745f08c3bdfSopenharmony_ci // The following test is actually wrong for empty 746f08c3bdfSopenharmony_ci // structs, but (1) they are not C99, (2) gcc does 747f08c3bdfSopenharmony_ci // the same thing, and (3) it's easier. 748f08c3bdfSopenharmony_ci if (sym->symbol_list) 749f08c3bdfSopenharmony_ci error_die(token->pos, "redefinition of %s", show_typename (sym)); 750f08c3bdfSopenharmony_ci sym->pos = *repos; 751f08c3bdfSopenharmony_ci 752f08c3bdfSopenharmony_ci // Mark the structure as needing re-examination 753f08c3bdfSopenharmony_ci sym->examined = 0; 754f08c3bdfSopenharmony_ci } else if (match_op(token, '{')) { 755f08c3bdfSopenharmony_ci // private struct/union/enum type 756f08c3bdfSopenharmony_ci sym = alloc_symbol(token->pos, type); 757f08c3bdfSopenharmony_ci set_current_scope(sym); // used by dissect 758f08c3bdfSopenharmony_ci ctx->ctype.base_type = sym; 759f08c3bdfSopenharmony_ci } else { 760f08c3bdfSopenharmony_ci sparse_error(token->pos, "expected declaration"); 761f08c3bdfSopenharmony_ci ctx->ctype.base_type = &bad_ctype; 762f08c3bdfSopenharmony_ci return token; 763f08c3bdfSopenharmony_ci } 764f08c3bdfSopenharmony_ci 765f08c3bdfSopenharmony_ci token = parse(token->next, sym); 766f08c3bdfSopenharmony_ci token = expect(token, '}', "at end of specifier"); 767f08c3bdfSopenharmony_ci attr.ctype.base_type = sym; 768f08c3bdfSopenharmony_ci token = handle_attributes(token, &attr); 769f08c3bdfSopenharmony_ci apply_ctype(token->pos, &sym->ctype, &attr.ctype); 770f08c3bdfSopenharmony_ci sym->packed = attr.packed; 771f08c3bdfSopenharmony_ci 772f08c3bdfSopenharmony_ci sym->endpos = token->pos; 773f08c3bdfSopenharmony_ci 774f08c3bdfSopenharmony_ci return token; 775f08c3bdfSopenharmony_ci} 776f08c3bdfSopenharmony_ci 777f08c3bdfSopenharmony_cistatic struct token *parse_struct_declaration(struct token *token, struct symbol *sym) 778f08c3bdfSopenharmony_ci{ 779f08c3bdfSopenharmony_ci struct symbol *field, *last = NULL; 780f08c3bdfSopenharmony_ci struct token *res; 781f08c3bdfSopenharmony_ci res = struct_declaration_list(token, &sym->symbol_list); 782f08c3bdfSopenharmony_ci FOR_EACH_PTR(sym->symbol_list, field) { 783f08c3bdfSopenharmony_ci if (!field->ident) { 784f08c3bdfSopenharmony_ci struct symbol *base = field->ctype.base_type; 785f08c3bdfSopenharmony_ci if (base && base->type == SYM_BITFIELD) 786f08c3bdfSopenharmony_ci continue; 787f08c3bdfSopenharmony_ci } 788f08c3bdfSopenharmony_ci if (last) 789f08c3bdfSopenharmony_ci last->next_subobject = field; 790f08c3bdfSopenharmony_ci last = field; 791f08c3bdfSopenharmony_ci } END_FOR_EACH_PTR(field); 792f08c3bdfSopenharmony_ci return res; 793f08c3bdfSopenharmony_ci} 794f08c3bdfSopenharmony_ci 795f08c3bdfSopenharmony_cistatic struct token *parse_union_declaration(struct token *token, struct symbol *sym) 796f08c3bdfSopenharmony_ci{ 797f08c3bdfSopenharmony_ci return struct_declaration_list(token, &sym->symbol_list); 798f08c3bdfSopenharmony_ci} 799f08c3bdfSopenharmony_ci 800f08c3bdfSopenharmony_cistatic struct token *struct_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 801f08c3bdfSopenharmony_ci{ 802f08c3bdfSopenharmony_ci return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration); 803f08c3bdfSopenharmony_ci} 804f08c3bdfSopenharmony_ci 805f08c3bdfSopenharmony_cistatic struct token *union_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 806f08c3bdfSopenharmony_ci{ 807f08c3bdfSopenharmony_ci return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration); 808f08c3bdfSopenharmony_ci} 809f08c3bdfSopenharmony_ci 810f08c3bdfSopenharmony_ci/// 811f08c3bdfSopenharmony_ci// safe right shift 812f08c3bdfSopenharmony_ci// 813f08c3bdfSopenharmony_ci// This allow to use a shift amount as big (or bigger) 814f08c3bdfSopenharmony_ci// than the width of the value to be shifted, in which case 815f08c3bdfSopenharmony_ci// the result is, of course, 0. 816f08c3bdfSopenharmony_cistatic unsigned long long rshift(unsigned long long val, unsigned int n) 817f08c3bdfSopenharmony_ci{ 818f08c3bdfSopenharmony_ci if (n >= (sizeof(val) * 8)) 819f08c3bdfSopenharmony_ci return 0; 820f08c3bdfSopenharmony_ci return val >> n; 821f08c3bdfSopenharmony_ci} 822f08c3bdfSopenharmony_ci 823f08c3bdfSopenharmony_cistruct range { 824f08c3bdfSopenharmony_ci long long neg; 825f08c3bdfSopenharmony_ci unsigned long long pos; 826f08c3bdfSopenharmony_ci}; 827f08c3bdfSopenharmony_ci 828f08c3bdfSopenharmony_cistatic void update_range(struct range *range, unsigned long long uval, struct symbol *vtype) 829f08c3bdfSopenharmony_ci{ 830f08c3bdfSopenharmony_ci long long sval = uval; 831f08c3bdfSopenharmony_ci 832f08c3bdfSopenharmony_ci if (is_signed_type(vtype) && (sval < 0)) { 833f08c3bdfSopenharmony_ci if (sval < range->neg) 834f08c3bdfSopenharmony_ci range->neg = sval; 835f08c3bdfSopenharmony_ci } else { 836f08c3bdfSopenharmony_ci if (uval > range->pos) 837f08c3bdfSopenharmony_ci range->pos = uval; 838f08c3bdfSopenharmony_ci } 839f08c3bdfSopenharmony_ci} 840f08c3bdfSopenharmony_ci 841f08c3bdfSopenharmony_cistatic int type_is_ok(struct symbol *type, struct range range) 842f08c3bdfSopenharmony_ci{ 843f08c3bdfSopenharmony_ci int shift = type->bit_size; 844f08c3bdfSopenharmony_ci int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED; 845f08c3bdfSopenharmony_ci 846f08c3bdfSopenharmony_ci if (!is_unsigned) 847f08c3bdfSopenharmony_ci shift--; 848f08c3bdfSopenharmony_ci if (rshift(range.pos, shift)) 849f08c3bdfSopenharmony_ci return 0; 850f08c3bdfSopenharmony_ci if (range.neg == 0) 851f08c3bdfSopenharmony_ci return 1; 852f08c3bdfSopenharmony_ci if (is_unsigned) 853f08c3bdfSopenharmony_ci return 0; 854f08c3bdfSopenharmony_ci if (rshift(~range.neg, shift)) 855f08c3bdfSopenharmony_ci return 0; 856f08c3bdfSopenharmony_ci return 1; 857f08c3bdfSopenharmony_ci} 858f08c3bdfSopenharmony_ci 859f08c3bdfSopenharmony_cistatic struct range type_range(struct symbol *type) 860f08c3bdfSopenharmony_ci{ 861f08c3bdfSopenharmony_ci struct range range; 862f08c3bdfSopenharmony_ci unsigned int size = type->bit_size; 863f08c3bdfSopenharmony_ci unsigned long long max; 864f08c3bdfSopenharmony_ci long long min; 865f08c3bdfSopenharmony_ci 866f08c3bdfSopenharmony_ci if (is_signed_type(type)) { 867f08c3bdfSopenharmony_ci min = sign_bit(size); 868f08c3bdfSopenharmony_ci max = min - 1; 869f08c3bdfSopenharmony_ci } else { 870f08c3bdfSopenharmony_ci min = 0; 871f08c3bdfSopenharmony_ci max = bits_mask(size); 872f08c3bdfSopenharmony_ci } 873f08c3bdfSopenharmony_ci 874f08c3bdfSopenharmony_ci range.pos = max; 875f08c3bdfSopenharmony_ci range.neg = min; 876f08c3bdfSopenharmony_ci return range; 877f08c3bdfSopenharmony_ci} 878f08c3bdfSopenharmony_ci 879f08c3bdfSopenharmony_cistatic int val_in_range(struct range *range, long long sval, struct symbol *vtype) 880f08c3bdfSopenharmony_ci{ 881f08c3bdfSopenharmony_ci unsigned long long uval = sval; 882f08c3bdfSopenharmony_ci 883f08c3bdfSopenharmony_ci if (is_signed_type(vtype) && (sval < 0)) 884f08c3bdfSopenharmony_ci return range->neg <= sval; 885f08c3bdfSopenharmony_ci else 886f08c3bdfSopenharmony_ci return uval <= range->pos; 887f08c3bdfSopenharmony_ci} 888f08c3bdfSopenharmony_ci 889f08c3bdfSopenharmony_cistatic void cast_enum_list(struct symbol_list *list, struct symbol *base_type) 890f08c3bdfSopenharmony_ci{ 891f08c3bdfSopenharmony_ci struct range irange = type_range(&int_ctype); 892f08c3bdfSopenharmony_ci struct symbol *sym; 893f08c3bdfSopenharmony_ci 894f08c3bdfSopenharmony_ci FOR_EACH_PTR(list, sym) { 895f08c3bdfSopenharmony_ci struct expression *expr = sym->initializer; 896f08c3bdfSopenharmony_ci struct symbol *ctype; 897f08c3bdfSopenharmony_ci long long val; 898f08c3bdfSopenharmony_ci if (expr->type != EXPR_VALUE) 899f08c3bdfSopenharmony_ci continue; 900f08c3bdfSopenharmony_ci ctype = expr->ctype; 901f08c3bdfSopenharmony_ci val = get_expression_value(expr); 902f08c3bdfSopenharmony_ci if (is_int_type(ctype) && val_in_range(&irange, val, ctype)) { 903f08c3bdfSopenharmony_ci expr->ctype = &int_ctype; 904f08c3bdfSopenharmony_ci continue; 905f08c3bdfSopenharmony_ci } 906f08c3bdfSopenharmony_ci cast_value(expr, base_type, expr, ctype); 907f08c3bdfSopenharmony_ci expr->ctype = base_type; 908f08c3bdfSopenharmony_ci } END_FOR_EACH_PTR(sym); 909f08c3bdfSopenharmony_ci} 910f08c3bdfSopenharmony_ci 911f08c3bdfSopenharmony_cistatic struct token *parse_enum_declaration(struct token *token, struct symbol *parent) 912f08c3bdfSopenharmony_ci{ 913f08c3bdfSopenharmony_ci unsigned long long lastval = 0; 914f08c3bdfSopenharmony_ci struct symbol *ctype = NULL, *base_type = NULL; 915f08c3bdfSopenharmony_ci struct range range = { }; 916f08c3bdfSopenharmony_ci int mix_bitwise = 0; 917f08c3bdfSopenharmony_ci 918f08c3bdfSopenharmony_ci parent->examined = 1; 919f08c3bdfSopenharmony_ci parent->ctype.base_type = &int_ctype; 920f08c3bdfSopenharmony_ci while (token_type(token) == TOKEN_IDENT) { 921f08c3bdfSopenharmony_ci struct expression *expr = NULL; 922f08c3bdfSopenharmony_ci struct token *next = token->next; 923f08c3bdfSopenharmony_ci struct decl_state ctx = { }; 924f08c3bdfSopenharmony_ci struct symbol *sym; 925f08c3bdfSopenharmony_ci 926f08c3bdfSopenharmony_ci // FIXME: only 'deprecated' should be accepted 927f08c3bdfSopenharmony_ci next = handle_attributes(next, &ctx); 928f08c3bdfSopenharmony_ci 929f08c3bdfSopenharmony_ci if (match_op(next, '=')) { 930f08c3bdfSopenharmony_ci next = constant_expression(next->next, &expr); 931f08c3bdfSopenharmony_ci lastval = get_expression_value(expr); 932f08c3bdfSopenharmony_ci ctype = &void_ctype; 933f08c3bdfSopenharmony_ci if (expr && expr->ctype) 934f08c3bdfSopenharmony_ci ctype = expr->ctype; 935f08c3bdfSopenharmony_ci } else if (!ctype) { 936f08c3bdfSopenharmony_ci ctype = &int_ctype; 937f08c3bdfSopenharmony_ci } else if (is_int_type(ctype)) { 938f08c3bdfSopenharmony_ci lastval++; 939f08c3bdfSopenharmony_ci } else { 940f08c3bdfSopenharmony_ci error_die(token->pos, "can't increment the last enum member"); 941f08c3bdfSopenharmony_ci } 942f08c3bdfSopenharmony_ci 943f08c3bdfSopenharmony_ci if (!expr) { 944f08c3bdfSopenharmony_ci expr = alloc_expression(token->pos, EXPR_VALUE); 945f08c3bdfSopenharmony_ci expr->value = lastval; 946f08c3bdfSopenharmony_ci expr->ctype = ctype; 947f08c3bdfSopenharmony_ci } 948f08c3bdfSopenharmony_ci 949f08c3bdfSopenharmony_ci sym = alloc_symbol(token->pos, SYM_NODE); 950f08c3bdfSopenharmony_ci bind_symbol(sym, token->ident, NS_SYMBOL); 951f08c3bdfSopenharmony_ci sym->ctype.modifiers &= ~MOD_ADDRESSABLE; 952f08c3bdfSopenharmony_ci sym->initializer = expr; 953f08c3bdfSopenharmony_ci sym->enum_member = 1; 954f08c3bdfSopenharmony_ci sym->ctype.base_type = parent; 955f08c3bdfSopenharmony_ci add_ptr_list(&parent->symbol_list, sym); 956f08c3bdfSopenharmony_ci 957f08c3bdfSopenharmony_ci if (base_type != &bad_ctype) { 958f08c3bdfSopenharmony_ci if (ctype->type == SYM_NODE) 959f08c3bdfSopenharmony_ci ctype = ctype->ctype.base_type; 960f08c3bdfSopenharmony_ci if (ctype->type == SYM_ENUM) { 961f08c3bdfSopenharmony_ci if (ctype == parent) 962f08c3bdfSopenharmony_ci ctype = base_type; 963f08c3bdfSopenharmony_ci else 964f08c3bdfSopenharmony_ci ctype = ctype->ctype.base_type; 965f08c3bdfSopenharmony_ci } 966f08c3bdfSopenharmony_ci /* 967f08c3bdfSopenharmony_ci * base_type rules: 968f08c3bdfSopenharmony_ci * - if all enums are of the same type, then 969f08c3bdfSopenharmony_ci * the base_type is that type (two first 970f08c3bdfSopenharmony_ci * cases) 971f08c3bdfSopenharmony_ci * - if enums are of different types, they 972f08c3bdfSopenharmony_ci * all have to be integer types, and the 973f08c3bdfSopenharmony_ci * base type is at least "int_ctype". 974f08c3bdfSopenharmony_ci * - otherwise the base_type is "bad_ctype". 975f08c3bdfSopenharmony_ci */ 976f08c3bdfSopenharmony_ci if (!base_type || ctype == &bad_ctype) { 977f08c3bdfSopenharmony_ci base_type = ctype; 978f08c3bdfSopenharmony_ci } else if (ctype == base_type) { 979f08c3bdfSopenharmony_ci /* nothing */ 980f08c3bdfSopenharmony_ci } else if (is_int_type(base_type) && is_int_type(ctype)) { 981f08c3bdfSopenharmony_ci base_type = &int_ctype; 982f08c3bdfSopenharmony_ci } else if (is_restricted_type(base_type) != is_restricted_type(ctype)) { 983f08c3bdfSopenharmony_ci if (!mix_bitwise++) { 984f08c3bdfSopenharmony_ci warning(expr->pos, "mixed bitwiseness"); 985f08c3bdfSopenharmony_ci } 986f08c3bdfSopenharmony_ci } else if (is_restricted_type(base_type) && base_type != ctype) { 987f08c3bdfSopenharmony_ci sparse_error(expr->pos, "incompatible restricted type"); 988f08c3bdfSopenharmony_ci info(expr->pos, " expected: %s", show_typename(base_type)); 989f08c3bdfSopenharmony_ci info(expr->pos, " got: %s", show_typename(ctype)); 990f08c3bdfSopenharmony_ci base_type = &bad_ctype; 991f08c3bdfSopenharmony_ci } else if (base_type != &bad_ctype) { 992f08c3bdfSopenharmony_ci sparse_error(token->pos, "bad enum definition"); 993f08c3bdfSopenharmony_ci base_type = &bad_ctype; 994f08c3bdfSopenharmony_ci } 995f08c3bdfSopenharmony_ci parent->ctype.base_type = base_type; 996f08c3bdfSopenharmony_ci } 997f08c3bdfSopenharmony_ci if (is_int_type(base_type)) { 998f08c3bdfSopenharmony_ci update_range(&range, lastval, ctype); 999f08c3bdfSopenharmony_ci } 1000f08c3bdfSopenharmony_ci token = next; 1001f08c3bdfSopenharmony_ci 1002f08c3bdfSopenharmony_ci sym->endpos = token->pos; 1003f08c3bdfSopenharmony_ci 1004f08c3bdfSopenharmony_ci if (!match_op(token, ',')) 1005f08c3bdfSopenharmony_ci break; 1006f08c3bdfSopenharmony_ci token = token->next; 1007f08c3bdfSopenharmony_ci } 1008f08c3bdfSopenharmony_ci if (!base_type) { 1009f08c3bdfSopenharmony_ci sparse_error(token->pos, "empty enum definition"); 1010f08c3bdfSopenharmony_ci base_type = &bad_ctype; 1011f08c3bdfSopenharmony_ci } 1012f08c3bdfSopenharmony_ci else if (!is_int_type(base_type)) 1013f08c3bdfSopenharmony_ci ; 1014f08c3bdfSopenharmony_ci else if (type_is_ok(&uint_ctype, range)) 1015f08c3bdfSopenharmony_ci base_type = &uint_ctype; 1016f08c3bdfSopenharmony_ci else if (type_is_ok(&int_ctype, range)) 1017f08c3bdfSopenharmony_ci base_type = &int_ctype; 1018f08c3bdfSopenharmony_ci else if (type_is_ok(&ulong_ctype, range)) 1019f08c3bdfSopenharmony_ci base_type = &ulong_ctype; 1020f08c3bdfSopenharmony_ci else if (type_is_ok(&long_ctype, range)) 1021f08c3bdfSopenharmony_ci base_type = &long_ctype; 1022f08c3bdfSopenharmony_ci else if (type_is_ok(&ullong_ctype, range)) 1023f08c3bdfSopenharmony_ci base_type = &ullong_ctype; 1024f08c3bdfSopenharmony_ci else if (type_is_ok(&llong_ctype, range)) 1025f08c3bdfSopenharmony_ci base_type = &llong_ctype; 1026f08c3bdfSopenharmony_ci else 1027f08c3bdfSopenharmony_ci base_type = &bad_ctype; 1028f08c3bdfSopenharmony_ci parent->ctype.base_type = base_type; 1029f08c3bdfSopenharmony_ci parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED); 1030f08c3bdfSopenharmony_ci parent->examined = 0; 1031f08c3bdfSopenharmony_ci 1032f08c3bdfSopenharmony_ci if (mix_bitwise) 1033f08c3bdfSopenharmony_ci return token; 1034f08c3bdfSopenharmony_ci cast_enum_list(parent->symbol_list, base_type); 1035f08c3bdfSopenharmony_ci 1036f08c3bdfSopenharmony_ci return token; 1037f08c3bdfSopenharmony_ci} 1038f08c3bdfSopenharmony_ci 1039f08c3bdfSopenharmony_cistatic struct token *enum_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 1040f08c3bdfSopenharmony_ci{ 1041f08c3bdfSopenharmony_ci struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration); 1042f08c3bdfSopenharmony_ci struct ctype *ctype = &ctx->ctype.base_type->ctype; 1043f08c3bdfSopenharmony_ci 1044f08c3bdfSopenharmony_ci if (!ctype->base_type) 1045f08c3bdfSopenharmony_ci ctype->base_type = &incomplete_ctype; 1046f08c3bdfSopenharmony_ci 1047f08c3bdfSopenharmony_ci return ret; 1048f08c3bdfSopenharmony_ci} 1049f08c3bdfSopenharmony_ci 1050f08c3bdfSopenharmony_cistatic struct token *typeof_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 1051f08c3bdfSopenharmony_ci{ 1052f08c3bdfSopenharmony_ci 1053f08c3bdfSopenharmony_ci if (!match_op(token, '(')) { 1054f08c3bdfSopenharmony_ci sparse_error(token->pos, "expected '(' after typeof"); 1055f08c3bdfSopenharmony_ci return token; 1056f08c3bdfSopenharmony_ci } 1057f08c3bdfSopenharmony_ci if (lookup_type(token->next)) { 1058f08c3bdfSopenharmony_ci struct symbol *sym; 1059f08c3bdfSopenharmony_ci token = typename(token->next, &sym, NULL); 1060f08c3bdfSopenharmony_ci ctx->ctype.base_type = sym->ctype.base_type; 1061f08c3bdfSopenharmony_ci apply_ctype(token->pos, &ctx->ctype, &sym->ctype); 1062f08c3bdfSopenharmony_ci } else { 1063f08c3bdfSopenharmony_ci struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF); 1064f08c3bdfSopenharmony_ci token = parse_expression(token->next, &typeof_sym->initializer); 1065f08c3bdfSopenharmony_ci 1066f08c3bdfSopenharmony_ci typeof_sym->endpos = token->pos; 1067f08c3bdfSopenharmony_ci if (!typeof_sym->initializer) { 1068f08c3bdfSopenharmony_ci sparse_error(token->pos, "expected expression after the '(' token"); 1069f08c3bdfSopenharmony_ci typeof_sym = &bad_ctype; 1070f08c3bdfSopenharmony_ci } 1071f08c3bdfSopenharmony_ci ctx->ctype.base_type = typeof_sym; 1072f08c3bdfSopenharmony_ci } 1073f08c3bdfSopenharmony_ci return expect(token, ')', "after typeof"); 1074f08c3bdfSopenharmony_ci} 1075f08c3bdfSopenharmony_ci 1076f08c3bdfSopenharmony_cistatic struct token *autotype_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 1077f08c3bdfSopenharmony_ci{ 1078f08c3bdfSopenharmony_ci ctx->ctype.base_type = &autotype_ctype; 1079f08c3bdfSopenharmony_ci ctx->autotype = 1; 1080f08c3bdfSopenharmony_ci return token; 1081f08c3bdfSopenharmony_ci} 1082f08c3bdfSopenharmony_ci 1083f08c3bdfSopenharmony_cistatic struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx) 1084f08c3bdfSopenharmony_ci{ 1085f08c3bdfSopenharmony_ci struct expression *expr = NULL; 1086f08c3bdfSopenharmony_ci if (match_op(token, '(')) 1087f08c3bdfSopenharmony_ci token = parens_expression(token, &expr, "in attribute"); 1088f08c3bdfSopenharmony_ci return token; 1089f08c3bdfSopenharmony_ci} 1090f08c3bdfSopenharmony_ci 1091f08c3bdfSopenharmony_cistatic struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx) 1092f08c3bdfSopenharmony_ci{ 1093f08c3bdfSopenharmony_ci if (!ctx->ctype.alignment) { 1094f08c3bdfSopenharmony_ci ctx->ctype.alignment = 1; 1095f08c3bdfSopenharmony_ci ctx->packed = 1; 1096f08c3bdfSopenharmony_ci } 1097f08c3bdfSopenharmony_ci return token; 1098f08c3bdfSopenharmony_ci} 1099f08c3bdfSopenharmony_ci 1100f08c3bdfSopenharmony_cistatic struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx) 1101f08c3bdfSopenharmony_ci{ 1102f08c3bdfSopenharmony_ci int alignment = max_alignment; 1103f08c3bdfSopenharmony_ci struct expression *expr = NULL; 1104f08c3bdfSopenharmony_ci 1105f08c3bdfSopenharmony_ci if (match_op(token, '(')) { 1106f08c3bdfSopenharmony_ci token = parens_expression(token, &expr, "in attribute"); 1107f08c3bdfSopenharmony_ci if (expr) 1108f08c3bdfSopenharmony_ci alignment = const_expression_value(expr); 1109f08c3bdfSopenharmony_ci } 1110f08c3bdfSopenharmony_ci if (alignment & (alignment-1)) { 1111f08c3bdfSopenharmony_ci warning(token->pos, "I don't like non-power-of-2 alignments"); 1112f08c3bdfSopenharmony_ci return token; 1113f08c3bdfSopenharmony_ci } else if (alignment > ctx->ctype.alignment) 1114f08c3bdfSopenharmony_ci ctx->ctype.alignment = alignment; 1115f08c3bdfSopenharmony_ci return token; 1116f08c3bdfSopenharmony_ci} 1117f08c3bdfSopenharmony_ci 1118f08c3bdfSopenharmony_cistatic void apply_mod(struct position *pos, unsigned long *mods, unsigned long mod) 1119f08c3bdfSopenharmony_ci{ 1120f08c3bdfSopenharmony_ci if (*mods & mod & ~MOD_DUP_OK) 1121f08c3bdfSopenharmony_ci warning(*pos, "duplicate %s", modifier_name(mod)); 1122f08c3bdfSopenharmony_ci *mods |= mod; 1123f08c3bdfSopenharmony_ci} 1124f08c3bdfSopenharmony_ci 1125f08c3bdfSopenharmony_cistatic void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual) 1126f08c3bdfSopenharmony_ci{ 1127f08c3bdfSopenharmony_ci apply_mod(pos, &ctx->modifiers, qual); 1128f08c3bdfSopenharmony_ci} 1129f08c3bdfSopenharmony_ci 1130f08c3bdfSopenharmony_cistatic struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx) 1131f08c3bdfSopenharmony_ci{ 1132f08c3bdfSopenharmony_ci apply_mod(&token->pos, &ctx->ctype.modifiers, attr->ctype.modifiers); 1133f08c3bdfSopenharmony_ci return token; 1134f08c3bdfSopenharmony_ci} 1135f08c3bdfSopenharmony_ci 1136f08c3bdfSopenharmony_cistatic struct token *attribute_function(struct token *token, struct symbol *attr, struct decl_state *ctx) 1137f08c3bdfSopenharmony_ci{ 1138f08c3bdfSopenharmony_ci apply_mod(&token->pos, &ctx->f_modifiers, attr->ctype.modifiers); 1139f08c3bdfSopenharmony_ci return token; 1140f08c3bdfSopenharmony_ci} 1141f08c3bdfSopenharmony_ci 1142f08c3bdfSopenharmony_cistatic struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state *ctx) 1143f08c3bdfSopenharmony_ci{ 1144f08c3bdfSopenharmony_ci if (Wbitwise) 1145f08c3bdfSopenharmony_ci attribute_modifier(token, attr, ctx); 1146f08c3bdfSopenharmony_ci return token; 1147f08c3bdfSopenharmony_ci} 1148f08c3bdfSopenharmony_ci 1149f08c3bdfSopenharmony_cistatic struct ident *numerical_address_space(int asn) 1150f08c3bdfSopenharmony_ci{ 1151f08c3bdfSopenharmony_ci char buff[32]; 1152f08c3bdfSopenharmony_ci 1153f08c3bdfSopenharmony_ci if (!asn) 1154f08c3bdfSopenharmony_ci return NULL; 1155f08c3bdfSopenharmony_ci sprintf(buff, "<asn:%d>", asn); 1156f08c3bdfSopenharmony_ci return built_in_ident(buff); 1157f08c3bdfSopenharmony_ci} 1158f08c3bdfSopenharmony_ci 1159f08c3bdfSopenharmony_cistatic struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx) 1160f08c3bdfSopenharmony_ci{ 1161f08c3bdfSopenharmony_ci struct expression *expr = NULL; 1162f08c3bdfSopenharmony_ci struct ident *as = NULL; 1163f08c3bdfSopenharmony_ci struct token *next; 1164f08c3bdfSopenharmony_ci 1165f08c3bdfSopenharmony_ci token = expect(token, '(', "after address_space attribute"); 1166f08c3bdfSopenharmony_ci switch (token_type(token)) { 1167f08c3bdfSopenharmony_ci case TOKEN_NUMBER: 1168f08c3bdfSopenharmony_ci next = primary_expression(token, &expr); 1169f08c3bdfSopenharmony_ci if (expr->type != EXPR_VALUE) 1170f08c3bdfSopenharmony_ci goto invalid; 1171f08c3bdfSopenharmony_ci as = numerical_address_space(expr->value); 1172f08c3bdfSopenharmony_ci break; 1173f08c3bdfSopenharmony_ci case TOKEN_IDENT: 1174f08c3bdfSopenharmony_ci next = token->next; 1175f08c3bdfSopenharmony_ci as = token->ident; 1176f08c3bdfSopenharmony_ci break; 1177f08c3bdfSopenharmony_ci default: 1178f08c3bdfSopenharmony_ci next = token->next; 1179f08c3bdfSopenharmony_ci invalid: 1180f08c3bdfSopenharmony_ci as = NULL; 1181f08c3bdfSopenharmony_ci warning(token->pos, "invalid address space name"); 1182f08c3bdfSopenharmony_ci } 1183f08c3bdfSopenharmony_ci 1184f08c3bdfSopenharmony_ci if (Waddress_space && as) { 1185f08c3bdfSopenharmony_ci if (ctx->ctype.as) 1186f08c3bdfSopenharmony_ci sparse_error(token->pos, 1187f08c3bdfSopenharmony_ci "multiple address spaces given: %s & %s", 1188f08c3bdfSopenharmony_ci show_as(ctx->ctype.as), show_as(as)); 1189f08c3bdfSopenharmony_ci ctx->ctype.as = as; 1190f08c3bdfSopenharmony_ci } 1191f08c3bdfSopenharmony_ci token = expect(next, ')', "after address_space attribute"); 1192f08c3bdfSopenharmony_ci return token; 1193f08c3bdfSopenharmony_ci} 1194f08c3bdfSopenharmony_ci 1195f08c3bdfSopenharmony_cistatic struct symbol *to_QI_mode(struct symbol *ctype) 1196f08c3bdfSopenharmony_ci{ 1197f08c3bdfSopenharmony_ci if (ctype->ctype.base_type != &int_type) 1198f08c3bdfSopenharmony_ci return NULL; 1199f08c3bdfSopenharmony_ci if (ctype == &char_ctype) 1200f08c3bdfSopenharmony_ci return ctype; 1201f08c3bdfSopenharmony_ci return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype 1202f08c3bdfSopenharmony_ci : &schar_ctype; 1203f08c3bdfSopenharmony_ci} 1204f08c3bdfSopenharmony_ci 1205f08c3bdfSopenharmony_cistatic struct symbol *to_HI_mode(struct symbol *ctype) 1206f08c3bdfSopenharmony_ci{ 1207f08c3bdfSopenharmony_ci if (ctype->ctype.base_type != &int_type) 1208f08c3bdfSopenharmony_ci return NULL; 1209f08c3bdfSopenharmony_ci return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype 1210f08c3bdfSopenharmony_ci : &sshort_ctype; 1211f08c3bdfSopenharmony_ci} 1212f08c3bdfSopenharmony_ci 1213f08c3bdfSopenharmony_cistatic struct symbol *to_SI_mode(struct symbol *ctype) 1214f08c3bdfSopenharmony_ci{ 1215f08c3bdfSopenharmony_ci if (ctype->ctype.base_type != &int_type) 1216f08c3bdfSopenharmony_ci return NULL; 1217f08c3bdfSopenharmony_ci return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype 1218f08c3bdfSopenharmony_ci : &sint_ctype; 1219f08c3bdfSopenharmony_ci} 1220f08c3bdfSopenharmony_ci 1221f08c3bdfSopenharmony_cistatic struct symbol *to_DI_mode(struct symbol *ctype) 1222f08c3bdfSopenharmony_ci{ 1223f08c3bdfSopenharmony_ci if (ctype->ctype.base_type != &int_type) 1224f08c3bdfSopenharmony_ci return NULL; 1225f08c3bdfSopenharmony_ci return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype 1226f08c3bdfSopenharmony_ci : &sllong_ctype; 1227f08c3bdfSopenharmony_ci} 1228f08c3bdfSopenharmony_ci 1229f08c3bdfSopenharmony_cistatic struct symbol *to_TI_mode(struct symbol *ctype) 1230f08c3bdfSopenharmony_ci{ 1231f08c3bdfSopenharmony_ci if (ctype->ctype.base_type != &int_type) 1232f08c3bdfSopenharmony_ci return NULL; 1233f08c3bdfSopenharmony_ci return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint128_ctype 1234f08c3bdfSopenharmony_ci : &sint128_ctype; 1235f08c3bdfSopenharmony_ci} 1236f08c3bdfSopenharmony_ci 1237f08c3bdfSopenharmony_cistatic struct symbol *to_pointer_mode(struct symbol *ctype) 1238f08c3bdfSopenharmony_ci{ 1239f08c3bdfSopenharmony_ci if (ctype->ctype.base_type != &int_type) 1240f08c3bdfSopenharmony_ci return NULL; 1241f08c3bdfSopenharmony_ci return ctype->ctype.modifiers & MOD_UNSIGNED ? uintptr_ctype 1242f08c3bdfSopenharmony_ci : intptr_ctype; 1243f08c3bdfSopenharmony_ci} 1244f08c3bdfSopenharmony_ci 1245f08c3bdfSopenharmony_cistatic struct symbol *to_word_mode(struct symbol *ctype) 1246f08c3bdfSopenharmony_ci{ 1247f08c3bdfSopenharmony_ci if (ctype->ctype.base_type != &int_type) 1248f08c3bdfSopenharmony_ci return NULL; 1249f08c3bdfSopenharmony_ci return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype 1250f08c3bdfSopenharmony_ci : &slong_ctype; 1251f08c3bdfSopenharmony_ci} 1252f08c3bdfSopenharmony_ci 1253f08c3bdfSopenharmony_cistatic struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx) 1254f08c3bdfSopenharmony_ci{ 1255f08c3bdfSopenharmony_ci token = expect(token, '(', "after mode attribute"); 1256f08c3bdfSopenharmony_ci if (token_type(token) == TOKEN_IDENT) { 1257f08c3bdfSopenharmony_ci struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD); 1258f08c3bdfSopenharmony_ci if (mode && mode->op->type & KW_MODE) 1259f08c3bdfSopenharmony_ci ctx->mode = mode->op; 1260f08c3bdfSopenharmony_ci else 1261f08c3bdfSopenharmony_ci sparse_error(token->pos, "unknown mode attribute %s", show_ident(token->ident)); 1262f08c3bdfSopenharmony_ci token = token->next; 1263f08c3bdfSopenharmony_ci } else 1264f08c3bdfSopenharmony_ci sparse_error(token->pos, "expect attribute mode symbol\n"); 1265f08c3bdfSopenharmony_ci token = expect(token, ')', "after mode attribute"); 1266f08c3bdfSopenharmony_ci return token; 1267f08c3bdfSopenharmony_ci} 1268f08c3bdfSopenharmony_ci 1269f08c3bdfSopenharmony_cistatic struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx) 1270f08c3bdfSopenharmony_ci{ 1271f08c3bdfSopenharmony_ci struct context *context = alloc_context(); 1272f08c3bdfSopenharmony_ci struct expression *args[3]; 1273f08c3bdfSopenharmony_ci int idx = 0; 1274f08c3bdfSopenharmony_ci 1275f08c3bdfSopenharmony_ci token = expect(token, '(', "after context attribute"); 1276f08c3bdfSopenharmony_ci token = conditional_expression(token, &args[0]); 1277f08c3bdfSopenharmony_ci token = expect(token, ',', "after context 1st argument"); 1278f08c3bdfSopenharmony_ci token = conditional_expression(token, &args[1]); 1279f08c3bdfSopenharmony_ci if (match_op(token, ',')) { 1280f08c3bdfSopenharmony_ci token = token->next; 1281f08c3bdfSopenharmony_ci token = conditional_expression(token, &args[2]); 1282f08c3bdfSopenharmony_ci token = expect(token, ')', "after context 3rd argument"); 1283f08c3bdfSopenharmony_ci context->context = args[0]; 1284f08c3bdfSopenharmony_ci idx++; 1285f08c3bdfSopenharmony_ci } else { 1286f08c3bdfSopenharmony_ci token = expect(token, ')', "after context 2nd argument"); 1287f08c3bdfSopenharmony_ci } 1288f08c3bdfSopenharmony_ci context->in = get_expression_value(args[idx++]); 1289f08c3bdfSopenharmony_ci context->out = get_expression_value(args[idx++]); 1290f08c3bdfSopenharmony_ci add_ptr_list(&ctx->ctype.contexts, context); 1291f08c3bdfSopenharmony_ci return token; 1292f08c3bdfSopenharmony_ci} 1293f08c3bdfSopenharmony_ci 1294f08c3bdfSopenharmony_cistatic struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx) 1295f08c3bdfSopenharmony_ci{ 1296f08c3bdfSopenharmony_ci if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT) 1297f08c3bdfSopenharmony_ci ctx->ctype.base_type->designated_init = 1; 1298f08c3bdfSopenharmony_ci else 1299f08c3bdfSopenharmony_ci warning(token->pos, "attribute designated_init applied to non-structure type"); 1300f08c3bdfSopenharmony_ci return token; 1301f08c3bdfSopenharmony_ci} 1302f08c3bdfSopenharmony_ci 1303f08c3bdfSopenharmony_cistatic struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx) 1304f08c3bdfSopenharmony_ci{ 1305f08c3bdfSopenharmony_ci if (Wtransparent_union) 1306f08c3bdfSopenharmony_ci warning(token->pos, "attribute __transparent_union__"); 1307f08c3bdfSopenharmony_ci 1308f08c3bdfSopenharmony_ci if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION) 1309f08c3bdfSopenharmony_ci ctx->ctype.base_type->transparent_union = 1; 1310f08c3bdfSopenharmony_ci else 1311f08c3bdfSopenharmony_ci warning(token->pos, "attribute __transparent_union__ applied to non-union type"); 1312f08c3bdfSopenharmony_ci return token; 1313f08c3bdfSopenharmony_ci} 1314f08c3bdfSopenharmony_ci 1315f08c3bdfSopenharmony_cistatic struct token *recover_unknown_attribute(struct token *token) 1316f08c3bdfSopenharmony_ci{ 1317f08c3bdfSopenharmony_ci struct expression *expr = NULL; 1318f08c3bdfSopenharmony_ci 1319f08c3bdfSopenharmony_ci if (Wunknown_attribute) 1320f08c3bdfSopenharmony_ci warning(token->pos, "unknown attribute '%s'", show_ident(token->ident)); 1321f08c3bdfSopenharmony_ci token = token->next; 1322f08c3bdfSopenharmony_ci if (match_op(token, '(')) 1323f08c3bdfSopenharmony_ci token = parens_expression(token, &expr, "in attribute"); 1324f08c3bdfSopenharmony_ci return token; 1325f08c3bdfSopenharmony_ci} 1326f08c3bdfSopenharmony_ci 1327f08c3bdfSopenharmony_cistatic struct token *attribute_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 1328f08c3bdfSopenharmony_ci{ 1329f08c3bdfSopenharmony_ci token = expect(token, '(', "after attribute"); 1330f08c3bdfSopenharmony_ci token = expect(token, '(', "after attribute"); 1331f08c3bdfSopenharmony_ci 1332f08c3bdfSopenharmony_ci while (token_type(token) == TOKEN_IDENT) { 1333f08c3bdfSopenharmony_ci struct symbol *attr = lookup_keyword(token->ident, NS_KEYWORD); 1334f08c3bdfSopenharmony_ci if (attr && attr->op->attribute) 1335f08c3bdfSopenharmony_ci token = attr->op->attribute(token->next, attr, ctx); 1336f08c3bdfSopenharmony_ci else 1337f08c3bdfSopenharmony_ci token = recover_unknown_attribute(token); 1338f08c3bdfSopenharmony_ci 1339f08c3bdfSopenharmony_ci if (!match_op(token, ',')) 1340f08c3bdfSopenharmony_ci break; 1341f08c3bdfSopenharmony_ci token = token->next; 1342f08c3bdfSopenharmony_ci } 1343f08c3bdfSopenharmony_ci 1344f08c3bdfSopenharmony_ci token = expect(token, ')', "after attribute"); 1345f08c3bdfSopenharmony_ci token = expect(token, ')', "after attribute"); 1346f08c3bdfSopenharmony_ci return token; 1347f08c3bdfSopenharmony_ci} 1348f08c3bdfSopenharmony_ci 1349f08c3bdfSopenharmony_cistatic unsigned long decl_modifiers(struct decl_state *ctx) 1350f08c3bdfSopenharmony_ci{ 1351f08c3bdfSopenharmony_ci unsigned long mods = ctx->ctype.modifiers & MOD_DECLARE; 1352f08c3bdfSopenharmony_ci ctx->ctype.modifiers &= ~MOD_DECLARE; 1353f08c3bdfSopenharmony_ci return ctx->storage_class | mods; 1354f08c3bdfSopenharmony_ci} 1355f08c3bdfSopenharmony_ci 1356f08c3bdfSopenharmony_cistatic struct token *storage_specifier(struct token *next, struct symbol *sym, struct decl_state *ctx) 1357f08c3bdfSopenharmony_ci{ 1358f08c3bdfSopenharmony_ci int is_tls = ctx->ctype.modifiers & MOD_TLS; 1359f08c3bdfSopenharmony_ci unsigned long class = sym->ctype.modifiers; 1360f08c3bdfSopenharmony_ci const char *storage = modifier_name(class); 1361f08c3bdfSopenharmony_ci 1362f08c3bdfSopenharmony_ci /* __thread can be used alone, or with extern or static */ 1363f08c3bdfSopenharmony_ci if (is_tls && (class & ~(MOD_STATIC|MOD_EXTERN))) 1364f08c3bdfSopenharmony_ci sparse_error(next->pos, "__thread cannot be used with '%s'", storage); 1365f08c3bdfSopenharmony_ci else if (!ctx->storage_class) 1366f08c3bdfSopenharmony_ci ctx->storage_class = class; 1367f08c3bdfSopenharmony_ci else if (ctx->storage_class == class) 1368f08c3bdfSopenharmony_ci sparse_error(next->pos, "duplicate %s", storage); 1369f08c3bdfSopenharmony_ci else 1370f08c3bdfSopenharmony_ci sparse_error(next->pos, "multiple storage classes"); 1371f08c3bdfSopenharmony_ci return next; 1372f08c3bdfSopenharmony_ci} 1373f08c3bdfSopenharmony_ci 1374f08c3bdfSopenharmony_cistatic struct token *thread_specifier(struct token *next, struct symbol *sym, struct decl_state *ctx) 1375f08c3bdfSopenharmony_ci{ 1376f08c3bdfSopenharmony_ci /* This GCC extension can be used alone, or with extern or static */ 1377f08c3bdfSopenharmony_ci if (!(ctx->storage_class & ~(MOD_STATIC|MOD_EXTERN))) { 1378f08c3bdfSopenharmony_ci apply_qualifier(&next->pos, &ctx->ctype, MOD_TLS); 1379f08c3bdfSopenharmony_ci } else { 1380f08c3bdfSopenharmony_ci sparse_error(next->pos, "__thread cannot be used with '%s'", 1381f08c3bdfSopenharmony_ci modifier_name(ctx->storage_class)); 1382f08c3bdfSopenharmony_ci } 1383f08c3bdfSopenharmony_ci 1384f08c3bdfSopenharmony_ci return next; 1385f08c3bdfSopenharmony_ci} 1386f08c3bdfSopenharmony_ci 1387f08c3bdfSopenharmony_cistatic struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx) 1388f08c3bdfSopenharmony_ci{ 1389f08c3bdfSopenharmony_ci ctx->forced = 1; 1390f08c3bdfSopenharmony_ci return token; 1391f08c3bdfSopenharmony_ci} 1392f08c3bdfSopenharmony_ci 1393f08c3bdfSopenharmony_cistatic struct token *alignas_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 1394f08c3bdfSopenharmony_ci{ 1395f08c3bdfSopenharmony_ci int alignment = 0; 1396f08c3bdfSopenharmony_ci 1397f08c3bdfSopenharmony_ci if (!match_op(token, '(')) { 1398f08c3bdfSopenharmony_ci sparse_error(token->pos, "expected '(' after _Alignas"); 1399f08c3bdfSopenharmony_ci return token; 1400f08c3bdfSopenharmony_ci } 1401f08c3bdfSopenharmony_ci if (lookup_type(token->next)) { 1402f08c3bdfSopenharmony_ci struct symbol *sym = NULL; 1403f08c3bdfSopenharmony_ci token = typename(token->next, &sym, NULL); 1404f08c3bdfSopenharmony_ci sym = examine_symbol_type(sym); 1405f08c3bdfSopenharmony_ci alignment = sym->ctype.alignment; 1406f08c3bdfSopenharmony_ci token = expect(token, ')', "after _Alignas(..."); 1407f08c3bdfSopenharmony_ci } else { 1408f08c3bdfSopenharmony_ci struct expression *expr = NULL; 1409f08c3bdfSopenharmony_ci token = parens_expression(token, &expr, "after _Alignas"); 1410f08c3bdfSopenharmony_ci if (!expr) 1411f08c3bdfSopenharmony_ci return token; 1412f08c3bdfSopenharmony_ci alignment = const_expression_value(expr); 1413f08c3bdfSopenharmony_ci } 1414f08c3bdfSopenharmony_ci 1415f08c3bdfSopenharmony_ci if (alignment < 0) { 1416f08c3bdfSopenharmony_ci warning(token->pos, "non-positive alignment"); 1417f08c3bdfSopenharmony_ci return token; 1418f08c3bdfSopenharmony_ci } 1419f08c3bdfSopenharmony_ci if (alignment & (alignment-1)) { 1420f08c3bdfSopenharmony_ci warning(token->pos, "non-power-of-2 alignment"); 1421f08c3bdfSopenharmony_ci return token; 1422f08c3bdfSopenharmony_ci } 1423f08c3bdfSopenharmony_ci if (alignment > ctx->ctype.alignment) 1424f08c3bdfSopenharmony_ci ctx->ctype.alignment = alignment; 1425f08c3bdfSopenharmony_ci return token; 1426f08c3bdfSopenharmony_ci} 1427f08c3bdfSopenharmony_ci 1428f08c3bdfSopenharmony_cistatic struct token *generic_qualifier(struct token *next, struct symbol *sym, struct decl_state *ctx) 1429f08c3bdfSopenharmony_ci{ 1430f08c3bdfSopenharmony_ci apply_qualifier(&next->pos, &ctx->ctype, sym->ctype.modifiers); 1431f08c3bdfSopenharmony_ci return next; 1432f08c3bdfSopenharmony_ci} 1433f08c3bdfSopenharmony_ci 1434f08c3bdfSopenharmony_cistatic void apply_ctype(struct position pos, struct ctype *dst, struct ctype *src) 1435f08c3bdfSopenharmony_ci{ 1436f08c3bdfSopenharmony_ci unsigned long mod = src->modifiers; 1437f08c3bdfSopenharmony_ci 1438f08c3bdfSopenharmony_ci if (mod) 1439f08c3bdfSopenharmony_ci apply_qualifier(&pos, dst, mod); 1440f08c3bdfSopenharmony_ci 1441f08c3bdfSopenharmony_ci /* Context */ 1442f08c3bdfSopenharmony_ci concat_ptr_list((struct ptr_list *)src->contexts, 1443f08c3bdfSopenharmony_ci (struct ptr_list **)&dst->contexts); 1444f08c3bdfSopenharmony_ci 1445f08c3bdfSopenharmony_ci /* Alignment */ 1446f08c3bdfSopenharmony_ci if (src->alignment > dst->alignment) 1447f08c3bdfSopenharmony_ci dst->alignment = src->alignment; 1448f08c3bdfSopenharmony_ci 1449f08c3bdfSopenharmony_ci /* Address space */ 1450f08c3bdfSopenharmony_ci if (src->as) 1451f08c3bdfSopenharmony_ci dst->as = src->as; 1452f08c3bdfSopenharmony_ci} 1453f08c3bdfSopenharmony_ci 1454f08c3bdfSopenharmony_cistatic void specifier_conflict(struct position pos, int what, struct ident *new) 1455f08c3bdfSopenharmony_ci{ 1456f08c3bdfSopenharmony_ci const char *old; 1457f08c3bdfSopenharmony_ci if (what & (Set_S | Set_T)) 1458f08c3bdfSopenharmony_ci goto Catch_all; 1459f08c3bdfSopenharmony_ci if (what & Set_Char) 1460f08c3bdfSopenharmony_ci old = "char"; 1461f08c3bdfSopenharmony_ci else if (what & Set_Double) 1462f08c3bdfSopenharmony_ci old = "double"; 1463f08c3bdfSopenharmony_ci else if (what & Set_Float) 1464f08c3bdfSopenharmony_ci old = "float"; 1465f08c3bdfSopenharmony_ci else if (what & Set_Signed) 1466f08c3bdfSopenharmony_ci old = "signed"; 1467f08c3bdfSopenharmony_ci else if (what & Set_Unsigned) 1468f08c3bdfSopenharmony_ci old = "unsigned"; 1469f08c3bdfSopenharmony_ci else if (what & Set_Short) 1470f08c3bdfSopenharmony_ci old = "short"; 1471f08c3bdfSopenharmony_ci else if (what & Set_Long) 1472f08c3bdfSopenharmony_ci old = "long"; 1473f08c3bdfSopenharmony_ci else 1474f08c3bdfSopenharmony_ci old = "long long"; 1475f08c3bdfSopenharmony_ci sparse_error(pos, "impossible combination of type specifiers: %s %s", 1476f08c3bdfSopenharmony_ci old, show_ident(new)); 1477f08c3bdfSopenharmony_ci return; 1478f08c3bdfSopenharmony_ci 1479f08c3bdfSopenharmony_ciCatch_all: 1480f08c3bdfSopenharmony_ci sparse_error(pos, "two or more data types in declaration specifiers"); 1481f08c3bdfSopenharmony_ci} 1482f08c3bdfSopenharmony_ci 1483f08c3bdfSopenharmony_cistatic struct symbol * const int_types[] = 1484f08c3bdfSopenharmony_ci {&char_ctype, &short_ctype, &int_ctype, &long_ctype, &llong_ctype, &int128_ctype}; 1485f08c3bdfSopenharmony_cistatic struct symbol * const signed_types[] = 1486f08c3bdfSopenharmony_ci {&schar_ctype, &sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype, 1487f08c3bdfSopenharmony_ci &sint128_ctype}; 1488f08c3bdfSopenharmony_cistatic struct symbol * const unsigned_types[] = 1489f08c3bdfSopenharmony_ci {&uchar_ctype, &ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype, 1490f08c3bdfSopenharmony_ci &uint128_ctype}; 1491f08c3bdfSopenharmony_cistatic struct symbol * const real_types[] = 1492f08c3bdfSopenharmony_ci {&float_ctype, &double_ctype, &ldouble_ctype}; 1493f08c3bdfSopenharmony_cistatic struct symbol * const * const types[] = { 1494f08c3bdfSopenharmony_ci [CInt] = int_types + 2, 1495f08c3bdfSopenharmony_ci [CSInt] = signed_types + 2, 1496f08c3bdfSopenharmony_ci [CUInt] = unsigned_types + 2, 1497f08c3bdfSopenharmony_ci [CReal] = real_types + 1, 1498f08c3bdfSopenharmony_ci}; 1499f08c3bdfSopenharmony_ci 1500f08c3bdfSopenharmony_cistruct symbol *ctype_integer(int size, int want_unsigned) 1501f08c3bdfSopenharmony_ci{ 1502f08c3bdfSopenharmony_ci return types[want_unsigned ? CUInt : CInt][size]; 1503f08c3bdfSopenharmony_ci} 1504f08c3bdfSopenharmony_ci 1505f08c3bdfSopenharmony_cistatic struct token *handle_qualifiers(struct token *t, struct decl_state *ctx) 1506f08c3bdfSopenharmony_ci{ 1507f08c3bdfSopenharmony_ci while (token_type(t) == TOKEN_IDENT) { 1508f08c3bdfSopenharmony_ci struct symbol *s = lookup_keyword(t->ident, NS_TYPEDEF); 1509f08c3bdfSopenharmony_ci if (!s) 1510f08c3bdfSopenharmony_ci break; 1511f08c3bdfSopenharmony_ci if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER))) 1512f08c3bdfSopenharmony_ci break; 1513f08c3bdfSopenharmony_ci t = t->next; 1514f08c3bdfSopenharmony_ci if (s->op->declarator) 1515f08c3bdfSopenharmony_ci t = s->op->declarator(t, s, ctx); 1516f08c3bdfSopenharmony_ci } 1517f08c3bdfSopenharmony_ci return t; 1518f08c3bdfSopenharmony_ci} 1519f08c3bdfSopenharmony_ci 1520f08c3bdfSopenharmony_cistatic struct token *declaration_specifiers(struct token *token, struct decl_state *ctx) 1521f08c3bdfSopenharmony_ci{ 1522f08c3bdfSopenharmony_ci int seen = 0; 1523f08c3bdfSopenharmony_ci int class = CInt; 1524f08c3bdfSopenharmony_ci int rank = 0; 1525f08c3bdfSopenharmony_ci 1526f08c3bdfSopenharmony_ci while (token_type(token) == TOKEN_IDENT) { 1527f08c3bdfSopenharmony_ci struct symbol *s = lookup_symbol(token->ident, 1528f08c3bdfSopenharmony_ci NS_TYPEDEF | NS_SYMBOL); 1529f08c3bdfSopenharmony_ci if (!s || !(s->namespace & NS_TYPEDEF)) 1530f08c3bdfSopenharmony_ci break; 1531f08c3bdfSopenharmony_ci if (s->type != SYM_KEYWORD) { 1532f08c3bdfSopenharmony_ci if (seen & Set_Any) 1533f08c3bdfSopenharmony_ci break; 1534f08c3bdfSopenharmony_ci seen |= Set_S | Set_T; 1535f08c3bdfSopenharmony_ci ctx->ctype.base_type = s->ctype.base_type; 1536f08c3bdfSopenharmony_ci apply_ctype(token->pos, &ctx->ctype, &s->ctype); 1537f08c3bdfSopenharmony_ci token = token->next; 1538f08c3bdfSopenharmony_ci continue; 1539f08c3bdfSopenharmony_ci } 1540f08c3bdfSopenharmony_ci if (s->op->type & KW_SPECIFIER) { 1541f08c3bdfSopenharmony_ci if (seen & s->op->test) { 1542f08c3bdfSopenharmony_ci specifier_conflict(token->pos, 1543f08c3bdfSopenharmony_ci seen & s->op->test, 1544f08c3bdfSopenharmony_ci token->ident); 1545f08c3bdfSopenharmony_ci break; 1546f08c3bdfSopenharmony_ci } 1547f08c3bdfSopenharmony_ci seen |= s->op->set; 1548f08c3bdfSopenharmony_ci class += s->op->class; 1549f08c3bdfSopenharmony_ci if (s->op->set & Set_Int128) 1550f08c3bdfSopenharmony_ci rank = 3; 1551f08c3bdfSopenharmony_ci else if (s->op->set & Set_Char) 1552f08c3bdfSopenharmony_ci rank = -2; 1553f08c3bdfSopenharmony_ci if (s->op->set & (Set_Short|Set_Float)) { 1554f08c3bdfSopenharmony_ci rank = -1; 1555f08c3bdfSopenharmony_ci } else if (s->op->set & Set_Long && rank++) { 1556f08c3bdfSopenharmony_ci if (class == CReal) { 1557f08c3bdfSopenharmony_ci specifier_conflict(token->pos, 1558f08c3bdfSopenharmony_ci Set_Vlong, 1559f08c3bdfSopenharmony_ci &double_ident); 1560f08c3bdfSopenharmony_ci break; 1561f08c3bdfSopenharmony_ci } 1562f08c3bdfSopenharmony_ci seen |= Set_Vlong; 1563f08c3bdfSopenharmony_ci } 1564f08c3bdfSopenharmony_ci } 1565f08c3bdfSopenharmony_ci token = token->next; 1566f08c3bdfSopenharmony_ci if (s->op->declarator) // Note: this eats attributes 1567f08c3bdfSopenharmony_ci token = s->op->declarator(token, s, ctx); 1568f08c3bdfSopenharmony_ci if (s->op->type & KW_EXACT) { 1569f08c3bdfSopenharmony_ci ctx->ctype.base_type = s->ctype.base_type; 1570f08c3bdfSopenharmony_ci ctx->ctype.modifiers |= s->ctype.modifiers; 1571f08c3bdfSopenharmony_ci } 1572f08c3bdfSopenharmony_ci } 1573f08c3bdfSopenharmony_ci 1574f08c3bdfSopenharmony_ci if (!(seen & Set_S)) { /* not set explicitly? */ 1575f08c3bdfSopenharmony_ci struct symbol *base = &incomplete_ctype; 1576f08c3bdfSopenharmony_ci if (seen & Set_Any) 1577f08c3bdfSopenharmony_ci base = types[class][rank]; 1578f08c3bdfSopenharmony_ci ctx->ctype.base_type = base; 1579f08c3bdfSopenharmony_ci } 1580f08c3bdfSopenharmony_ci 1581f08c3bdfSopenharmony_ci if (ctx->ctype.modifiers & MOD_BITWISE) { 1582f08c3bdfSopenharmony_ci struct symbol *type; 1583f08c3bdfSopenharmony_ci ctx->ctype.modifiers &= ~MOD_BITWISE; 1584f08c3bdfSopenharmony_ci if (!is_int_type(ctx->ctype.base_type)) { 1585f08c3bdfSopenharmony_ci sparse_error(token->pos, "invalid modifier"); 1586f08c3bdfSopenharmony_ci return token; 1587f08c3bdfSopenharmony_ci } 1588f08c3bdfSopenharmony_ci type = alloc_symbol(token->pos, SYM_BASETYPE); 1589f08c3bdfSopenharmony_ci *type = *ctx->ctype.base_type; 1590f08c3bdfSopenharmony_ci type->ctype.modifiers &= ~MOD_SPECIFIER; 1591f08c3bdfSopenharmony_ci type->ctype.base_type = ctx->ctype.base_type; 1592f08c3bdfSopenharmony_ci type->type = SYM_RESTRICT; 1593f08c3bdfSopenharmony_ci ctx->ctype.base_type = type; 1594f08c3bdfSopenharmony_ci create_fouled(type); 1595f08c3bdfSopenharmony_ci } 1596f08c3bdfSopenharmony_ci return token; 1597f08c3bdfSopenharmony_ci} 1598f08c3bdfSopenharmony_ci 1599f08c3bdfSopenharmony_cistatic struct token *abstract_array_declarator(struct token *token, struct symbol *sym) 1600f08c3bdfSopenharmony_ci{ 1601f08c3bdfSopenharmony_ci struct expression *expr = NULL; 1602f08c3bdfSopenharmony_ci int has_static = 0; 1603f08c3bdfSopenharmony_ci 1604f08c3bdfSopenharmony_ci while (token_type(token) == TOKEN_IDENT) { 1605f08c3bdfSopenharmony_ci struct symbol *sym = lookup_keyword(token->ident, NS_TYPEDEF); 1606f08c3bdfSopenharmony_ci if (!sym || !(sym->op->type & (KW_STATIC|KW_QUALIFIER))) 1607f08c3bdfSopenharmony_ci break; 1608f08c3bdfSopenharmony_ci if (has_static && (sym->op->type & KW_STATIC)) 1609f08c3bdfSopenharmony_ci sparse_error(token->pos, "duplicate array static declarator"); 1610f08c3bdfSopenharmony_ci has_static |= (sym->op->type & KW_STATIC); 1611f08c3bdfSopenharmony_ci token = token->next; 1612f08c3bdfSopenharmony_ci } 1613f08c3bdfSopenharmony_ci if (match_op(token, '*') && match_op(token->next, ']')) { 1614f08c3bdfSopenharmony_ci // FIXME: '[*]' is treated like '[]' 1615f08c3bdfSopenharmony_ci token = token->next; 1616f08c3bdfSopenharmony_ci } else { 1617f08c3bdfSopenharmony_ci token = assignment_expression(token, &expr); 1618f08c3bdfSopenharmony_ci } 1619f08c3bdfSopenharmony_ci sym->array_size = expr; 1620f08c3bdfSopenharmony_ci return token; 1621f08c3bdfSopenharmony_ci} 1622f08c3bdfSopenharmony_ci 1623f08c3bdfSopenharmony_cistatic struct token *parameter_type_list(struct token *, struct symbol *); 1624f08c3bdfSopenharmony_cistatic struct token *identifier_list(struct token *, struct symbol *); 1625f08c3bdfSopenharmony_cistatic struct token *declarator(struct token *token, struct decl_state *ctx); 1626f08c3bdfSopenharmony_ci 1627f08c3bdfSopenharmony_cistatic struct token *handle_asm_name(struct token *token, struct decl_state *ctx) 1628f08c3bdfSopenharmony_ci{ 1629f08c3bdfSopenharmony_ci struct expression *expr; 1630f08c3bdfSopenharmony_ci struct symbol *keyword; 1631f08c3bdfSopenharmony_ci 1632f08c3bdfSopenharmony_ci if (token_type(token) != TOKEN_IDENT) 1633f08c3bdfSopenharmony_ci return token; 1634f08c3bdfSopenharmony_ci keyword = lookup_keyword(token->ident, NS_KEYWORD); 1635f08c3bdfSopenharmony_ci if (!keyword) 1636f08c3bdfSopenharmony_ci return token; 1637f08c3bdfSopenharmony_ci if (!(keyword->op->type & KW_ASM)) 1638f08c3bdfSopenharmony_ci return token; 1639f08c3bdfSopenharmony_ci 1640f08c3bdfSopenharmony_ci token = token->next; 1641f08c3bdfSopenharmony_ci token = expect(token, '(', "after asm"); 1642f08c3bdfSopenharmony_ci token = string_expression(token, &expr, "asm name"); 1643f08c3bdfSopenharmony_ci token = expect(token, ')', "after asm"); 1644f08c3bdfSopenharmony_ci return token; 1645f08c3bdfSopenharmony_ci} 1646f08c3bdfSopenharmony_ci 1647f08c3bdfSopenharmony_ci/// 1648f08c3bdfSopenharmony_ci// test if @token is '__attribute__' (or one of its variant) 1649f08c3bdfSopenharmony_cistatic bool match_attribute(struct token *token) 1650f08c3bdfSopenharmony_ci{ 1651f08c3bdfSopenharmony_ci struct symbol *sym; 1652f08c3bdfSopenharmony_ci 1653f08c3bdfSopenharmony_ci if (token_type(token) != TOKEN_IDENT) 1654f08c3bdfSopenharmony_ci return false; 1655f08c3bdfSopenharmony_ci sym = lookup_keyword(token->ident, NS_TYPEDEF); 1656f08c3bdfSopenharmony_ci if (!sym || !sym->op) 1657f08c3bdfSopenharmony_ci return false; 1658f08c3bdfSopenharmony_ci return sym->op->type & KW_ATTRIBUTE; 1659f08c3bdfSopenharmony_ci} 1660f08c3bdfSopenharmony_ci 1661f08c3bdfSopenharmony_cistatic struct token *skip_attribute(struct token *token) 1662f08c3bdfSopenharmony_ci{ 1663f08c3bdfSopenharmony_ci token = token->next; 1664f08c3bdfSopenharmony_ci if (match_op(token, '(')) { 1665f08c3bdfSopenharmony_ci int depth = 1; 1666f08c3bdfSopenharmony_ci token = token->next; 1667f08c3bdfSopenharmony_ci while (depth && !eof_token(token)) { 1668f08c3bdfSopenharmony_ci if (token_type(token) == TOKEN_SPECIAL) { 1669f08c3bdfSopenharmony_ci if (token->special == '(') 1670f08c3bdfSopenharmony_ci depth++; 1671f08c3bdfSopenharmony_ci else if (token->special == ')') 1672f08c3bdfSopenharmony_ci depth--; 1673f08c3bdfSopenharmony_ci } 1674f08c3bdfSopenharmony_ci token = token->next; 1675f08c3bdfSopenharmony_ci } 1676f08c3bdfSopenharmony_ci } 1677f08c3bdfSopenharmony_ci return token; 1678f08c3bdfSopenharmony_ci} 1679f08c3bdfSopenharmony_ci 1680f08c3bdfSopenharmony_cistatic struct token *skip_attributes(struct token *token) 1681f08c3bdfSopenharmony_ci{ 1682f08c3bdfSopenharmony_ci while (match_attribute(token)) { 1683f08c3bdfSopenharmony_ci token = expect(token->next, '(', "after attribute"); 1684f08c3bdfSopenharmony_ci token = expect(token, '(', "after attribute"); 1685f08c3bdfSopenharmony_ci while (token_type(token) == TOKEN_IDENT) { 1686f08c3bdfSopenharmony_ci token = skip_attribute(token); 1687f08c3bdfSopenharmony_ci if (!match_op(token, ',')) 1688f08c3bdfSopenharmony_ci break; 1689f08c3bdfSopenharmony_ci token = token->next; 1690f08c3bdfSopenharmony_ci } 1691f08c3bdfSopenharmony_ci token = expect(token, ')', "after attribute"); 1692f08c3bdfSopenharmony_ci token = expect(token, ')', "after attribute"); 1693f08c3bdfSopenharmony_ci } 1694f08c3bdfSopenharmony_ci return token; 1695f08c3bdfSopenharmony_ci} 1696f08c3bdfSopenharmony_ci 1697f08c3bdfSopenharmony_cistatic struct token *handle_attributes(struct token *token, struct decl_state *ctx) 1698f08c3bdfSopenharmony_ci{ 1699f08c3bdfSopenharmony_ci while (match_attribute(token)) 1700f08c3bdfSopenharmony_ci token = attribute_specifier(token->next, NULL, ctx); 1701f08c3bdfSopenharmony_ci return token; 1702f08c3bdfSopenharmony_ci} 1703f08c3bdfSopenharmony_ci 1704f08c3bdfSopenharmony_cistatic int is_nested(struct token *token, struct token **p, 1705f08c3bdfSopenharmony_ci int prefer_abstract) 1706f08c3bdfSopenharmony_ci{ 1707f08c3bdfSopenharmony_ci /* 1708f08c3bdfSopenharmony_ci * This can be either a parameter list or a grouping. 1709f08c3bdfSopenharmony_ci * For the direct (non-abstract) case, we know if must be 1710f08c3bdfSopenharmony_ci * a parameter list if we already saw the identifier. 1711f08c3bdfSopenharmony_ci * For the abstract case, we know if must be a parameter 1712f08c3bdfSopenharmony_ci * list if it is empty or starts with a type. 1713f08c3bdfSopenharmony_ci */ 1714f08c3bdfSopenharmony_ci struct token *next = token->next; 1715f08c3bdfSopenharmony_ci 1716f08c3bdfSopenharmony_ci *p = next = skip_attributes(next); 1717f08c3bdfSopenharmony_ci 1718f08c3bdfSopenharmony_ci if (token_type(next) == TOKEN_IDENT) { 1719f08c3bdfSopenharmony_ci if (lookup_type(next)) 1720f08c3bdfSopenharmony_ci return !prefer_abstract; 1721f08c3bdfSopenharmony_ci return 1; 1722f08c3bdfSopenharmony_ci } 1723f08c3bdfSopenharmony_ci 1724f08c3bdfSopenharmony_ci if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS)) 1725f08c3bdfSopenharmony_ci return 0; 1726f08c3bdfSopenharmony_ci 1727f08c3bdfSopenharmony_ci return 1; 1728f08c3bdfSopenharmony_ci} 1729f08c3bdfSopenharmony_ci 1730f08c3bdfSopenharmony_cienum kind { 1731f08c3bdfSopenharmony_ci Empty, K_R, Proto, Bad_Func, 1732f08c3bdfSopenharmony_ci}; 1733f08c3bdfSopenharmony_ci 1734f08c3bdfSopenharmony_cistatic enum kind which_func(struct token *token, 1735f08c3bdfSopenharmony_ci struct ident **n, 1736f08c3bdfSopenharmony_ci int prefer_abstract) 1737f08c3bdfSopenharmony_ci{ 1738f08c3bdfSopenharmony_ci struct token *next = token->next; 1739f08c3bdfSopenharmony_ci 1740f08c3bdfSopenharmony_ci if (token_type(next) == TOKEN_IDENT) { 1741f08c3bdfSopenharmony_ci if (lookup_type(next)) 1742f08c3bdfSopenharmony_ci return Proto; 1743f08c3bdfSopenharmony_ci /* identifier list not in definition; complain */ 1744f08c3bdfSopenharmony_ci if (prefer_abstract) 1745f08c3bdfSopenharmony_ci warning(token->pos, 1746f08c3bdfSopenharmony_ci "identifier list not in definition"); 1747f08c3bdfSopenharmony_ci return K_R; 1748f08c3bdfSopenharmony_ci } 1749f08c3bdfSopenharmony_ci 1750f08c3bdfSopenharmony_ci if (token_type(next) != TOKEN_SPECIAL) 1751f08c3bdfSopenharmony_ci return Bad_Func; 1752f08c3bdfSopenharmony_ci 1753f08c3bdfSopenharmony_ci if (next->special == ')') { 1754f08c3bdfSopenharmony_ci /* don't complain about those */ 1755f08c3bdfSopenharmony_ci if (!n || match_op(next->next, ';') || match_op(next->next, ',')) 1756f08c3bdfSopenharmony_ci return Empty; 1757f08c3bdfSopenharmony_ci if (Wstrict_prototypes) 1758f08c3bdfSopenharmony_ci warning(next->pos, 1759f08c3bdfSopenharmony_ci "non-ANSI function declaration of function '%s'", 1760f08c3bdfSopenharmony_ci show_ident(*n)); 1761f08c3bdfSopenharmony_ci return Empty; 1762f08c3bdfSopenharmony_ci } 1763f08c3bdfSopenharmony_ci 1764f08c3bdfSopenharmony_ci if (next->special == SPECIAL_ELLIPSIS) { 1765f08c3bdfSopenharmony_ci warning(next->pos, 1766f08c3bdfSopenharmony_ci "variadic functions must have one named argument"); 1767f08c3bdfSopenharmony_ci return Proto; 1768f08c3bdfSopenharmony_ci } 1769f08c3bdfSopenharmony_ci 1770f08c3bdfSopenharmony_ci return Bad_Func; 1771f08c3bdfSopenharmony_ci} 1772f08c3bdfSopenharmony_ci 1773f08c3bdfSopenharmony_cistatic struct token *direct_declarator(struct token *token, struct decl_state *ctx) 1774f08c3bdfSopenharmony_ci{ 1775f08c3bdfSopenharmony_ci struct ctype *ctype = &ctx->ctype; 1776f08c3bdfSopenharmony_ci struct token *next; 1777f08c3bdfSopenharmony_ci struct ident **p = ctx->ident; 1778f08c3bdfSopenharmony_ci 1779f08c3bdfSopenharmony_ci if (ctx->ident && token_type(token) == TOKEN_IDENT) { 1780f08c3bdfSopenharmony_ci *ctx->ident = token->ident; 1781f08c3bdfSopenharmony_ci token = token->next; 1782f08c3bdfSopenharmony_ci } else if (match_op(token, '(') && 1783f08c3bdfSopenharmony_ci is_nested(token, &next, ctx->prefer_abstract)) { 1784f08c3bdfSopenharmony_ci struct symbol *base_type = ctype->base_type; 1785f08c3bdfSopenharmony_ci if (token->next != next) 1786f08c3bdfSopenharmony_ci next = handle_attributes(token->next, ctx); 1787f08c3bdfSopenharmony_ci token = declarator(next, ctx); 1788f08c3bdfSopenharmony_ci token = expect(token, ')', "in nested declarator"); 1789f08c3bdfSopenharmony_ci while (ctype->base_type != base_type) 1790f08c3bdfSopenharmony_ci ctype = &ctype->base_type->ctype; 1791f08c3bdfSopenharmony_ci p = NULL; 1792f08c3bdfSopenharmony_ci } 1793f08c3bdfSopenharmony_ci 1794f08c3bdfSopenharmony_ci if (match_op(token, '(')) { 1795f08c3bdfSopenharmony_ci enum kind kind = which_func(token, p, ctx->prefer_abstract); 1796f08c3bdfSopenharmony_ci struct symbol *fn; 1797f08c3bdfSopenharmony_ci fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN); 1798f08c3bdfSopenharmony_ci ctype->modifiers |= ctx->f_modifiers; 1799f08c3bdfSopenharmony_ci token = token->next; 1800f08c3bdfSopenharmony_ci if (kind == K_R) 1801f08c3bdfSopenharmony_ci token = identifier_list(token, fn); 1802f08c3bdfSopenharmony_ci else if (kind == Proto) 1803f08c3bdfSopenharmony_ci token = parameter_type_list(token, fn); 1804f08c3bdfSopenharmony_ci token = expect(token, ')', "in function declarator"); 1805f08c3bdfSopenharmony_ci fn->endpos = token->pos; 1806f08c3bdfSopenharmony_ci return token; 1807f08c3bdfSopenharmony_ci } 1808f08c3bdfSopenharmony_ci 1809f08c3bdfSopenharmony_ci while (match_op(token, '[')) { 1810f08c3bdfSopenharmony_ci struct symbol *array; 1811f08c3bdfSopenharmony_ci array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY); 1812f08c3bdfSopenharmony_ci token = abstract_array_declarator(token->next, array); 1813f08c3bdfSopenharmony_ci token = expect(token, ']', "in abstract_array_declarator"); 1814f08c3bdfSopenharmony_ci array->endpos = token->pos; 1815f08c3bdfSopenharmony_ci ctype = &array->ctype; 1816f08c3bdfSopenharmony_ci } 1817f08c3bdfSopenharmony_ci return token; 1818f08c3bdfSopenharmony_ci} 1819f08c3bdfSopenharmony_ci 1820f08c3bdfSopenharmony_cistatic struct token *pointer(struct token *token, struct decl_state *ctx) 1821f08c3bdfSopenharmony_ci{ 1822f08c3bdfSopenharmony_ci while (match_op(token,'*')) { 1823f08c3bdfSopenharmony_ci struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR); 1824f08c3bdfSopenharmony_ci ptr->ctype.modifiers = ctx->ctype.modifiers; 1825f08c3bdfSopenharmony_ci ptr->ctype.base_type = ctx->ctype.base_type; 1826f08c3bdfSopenharmony_ci ptr->ctype.as = ctx->ctype.as; 1827f08c3bdfSopenharmony_ci ptr->ctype.contexts = ctx->ctype.contexts; 1828f08c3bdfSopenharmony_ci ctx->ctype.modifiers = 0; 1829f08c3bdfSopenharmony_ci ctx->ctype.base_type = ptr; 1830f08c3bdfSopenharmony_ci ctx->ctype.as = NULL; 1831f08c3bdfSopenharmony_ci ctx->ctype.contexts = NULL; 1832f08c3bdfSopenharmony_ci ctx->ctype.alignment = 0; 1833f08c3bdfSopenharmony_ci 1834f08c3bdfSopenharmony_ci token = handle_qualifiers(token->next, ctx); 1835f08c3bdfSopenharmony_ci ctx->ctype.base_type->endpos = token->pos; 1836f08c3bdfSopenharmony_ci } 1837f08c3bdfSopenharmony_ci return token; 1838f08c3bdfSopenharmony_ci} 1839f08c3bdfSopenharmony_ci 1840f08c3bdfSopenharmony_cistatic struct token *declarator(struct token *token, struct decl_state *ctx) 1841f08c3bdfSopenharmony_ci{ 1842f08c3bdfSopenharmony_ci token = pointer(token, ctx); 1843f08c3bdfSopenharmony_ci return direct_declarator(token, ctx); 1844f08c3bdfSopenharmony_ci} 1845f08c3bdfSopenharmony_ci 1846f08c3bdfSopenharmony_cistatic struct token *handle_bitfield(struct token *token, struct decl_state *ctx) 1847f08c3bdfSopenharmony_ci{ 1848f08c3bdfSopenharmony_ci struct ctype *ctype = &ctx->ctype; 1849f08c3bdfSopenharmony_ci struct expression *expr; 1850f08c3bdfSopenharmony_ci struct symbol *bitfield; 1851f08c3bdfSopenharmony_ci long long width; 1852f08c3bdfSopenharmony_ci 1853f08c3bdfSopenharmony_ci if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) { 1854f08c3bdfSopenharmony_ci sparse_error(token->pos, "invalid bitfield specifier for type %s.", 1855f08c3bdfSopenharmony_ci show_typename(ctype->base_type)); 1856f08c3bdfSopenharmony_ci // Parse this to recover gracefully. 1857f08c3bdfSopenharmony_ci return conditional_expression(token->next, &expr); 1858f08c3bdfSopenharmony_ci } 1859f08c3bdfSopenharmony_ci 1860f08c3bdfSopenharmony_ci bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD); 1861f08c3bdfSopenharmony_ci token = conditional_expression(token->next, &expr); 1862f08c3bdfSopenharmony_ci width = const_expression_value(expr); 1863f08c3bdfSopenharmony_ci bitfield->bit_size = width; 1864f08c3bdfSopenharmony_ci 1865f08c3bdfSopenharmony_ci if (width < 0 || width > INT_MAX || (*ctx->ident && width == 0)) { 1866f08c3bdfSopenharmony_ci sparse_error(token->pos, "bitfield '%s' has invalid width (%lld)", 1867f08c3bdfSopenharmony_ci show_ident(*ctx->ident), width); 1868f08c3bdfSopenharmony_ci width = -1; 1869f08c3bdfSopenharmony_ci } else if (*ctx->ident) { 1870f08c3bdfSopenharmony_ci struct symbol *base_type = bitfield->ctype.base_type; 1871f08c3bdfSopenharmony_ci struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type; 1872f08c3bdfSopenharmony_ci int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED); 1873f08c3bdfSopenharmony_ci if (Wone_bit_signed_bitfield && width == 1 && is_signed) { 1874f08c3bdfSopenharmony_ci // Valid values are either {-1;0} or {0}, depending on integer 1875f08c3bdfSopenharmony_ci // representation. The latter makes for very efficient code... 1876f08c3bdfSopenharmony_ci sparse_error(token->pos, "dubious one-bit signed bitfield"); 1877f08c3bdfSopenharmony_ci } 1878f08c3bdfSopenharmony_ci if (Wdefault_bitfield_sign && 1879f08c3bdfSopenharmony_ci bitfield_type->type != SYM_ENUM && 1880f08c3bdfSopenharmony_ci !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) && 1881f08c3bdfSopenharmony_ci is_signed) { 1882f08c3bdfSopenharmony_ci // The sign of bitfields is unspecified by default. 1883f08c3bdfSopenharmony_ci warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'"); 1884f08c3bdfSopenharmony_ci } 1885f08c3bdfSopenharmony_ci } 1886f08c3bdfSopenharmony_ci bitfield->bit_size = width; 1887f08c3bdfSopenharmony_ci bitfield->endpos = token->pos; 1888f08c3bdfSopenharmony_ci bitfield->ident = *ctx->ident; 1889f08c3bdfSopenharmony_ci return token; 1890f08c3bdfSopenharmony_ci} 1891f08c3bdfSopenharmony_ci 1892f08c3bdfSopenharmony_cistatic struct token *declaration_list(struct token *token, struct symbol_list **list) 1893f08c3bdfSopenharmony_ci{ 1894f08c3bdfSopenharmony_ci struct decl_state ctx = {.prefer_abstract = 0}; 1895f08c3bdfSopenharmony_ci struct ctype saved; 1896f08c3bdfSopenharmony_ci unsigned long mod; 1897f08c3bdfSopenharmony_ci 1898f08c3bdfSopenharmony_ci token = declaration_specifiers(token, &ctx); 1899f08c3bdfSopenharmony_ci mod = decl_modifiers(&ctx); 1900f08c3bdfSopenharmony_ci saved = ctx.ctype; 1901f08c3bdfSopenharmony_ci for (;;) { 1902f08c3bdfSopenharmony_ci struct symbol *decl = alloc_symbol(token->pos, SYM_NODE); 1903f08c3bdfSopenharmony_ci ctx.ident = &decl->ident; 1904f08c3bdfSopenharmony_ci 1905f08c3bdfSopenharmony_ci token = declarator(token, &ctx); 1906f08c3bdfSopenharmony_ci if (match_op(token, ':')) 1907f08c3bdfSopenharmony_ci token = handle_bitfield(token, &ctx); 1908f08c3bdfSopenharmony_ci 1909f08c3bdfSopenharmony_ci token = handle_attributes(token, &ctx); 1910f08c3bdfSopenharmony_ci apply_modifiers(token->pos, &ctx); 1911f08c3bdfSopenharmony_ci 1912f08c3bdfSopenharmony_ci decl->ctype = ctx.ctype; 1913f08c3bdfSopenharmony_ci decl->ctype.modifiers |= mod; 1914f08c3bdfSopenharmony_ci decl->endpos = token->pos; 1915f08c3bdfSopenharmony_ci add_symbol(list, decl); 1916f08c3bdfSopenharmony_ci if (!match_op(token, ',')) 1917f08c3bdfSopenharmony_ci break; 1918f08c3bdfSopenharmony_ci token = token->next; 1919f08c3bdfSopenharmony_ci ctx.ctype = saved; 1920f08c3bdfSopenharmony_ci } 1921f08c3bdfSopenharmony_ci return token; 1922f08c3bdfSopenharmony_ci} 1923f08c3bdfSopenharmony_ci 1924f08c3bdfSopenharmony_cistatic struct token *struct_declaration_list(struct token *token, struct symbol_list **list) 1925f08c3bdfSopenharmony_ci{ 1926f08c3bdfSopenharmony_ci while (!match_op(token, '}')) { 1927f08c3bdfSopenharmony_ci if (match_ident(token, &_Static_assert_ident)) { 1928f08c3bdfSopenharmony_ci token = parse_static_assert(token, NULL); 1929f08c3bdfSopenharmony_ci continue; 1930f08c3bdfSopenharmony_ci } 1931f08c3bdfSopenharmony_ci if (!match_op(token, ';')) 1932f08c3bdfSopenharmony_ci token = declaration_list(token, list); 1933f08c3bdfSopenharmony_ci if (!match_op(token, ';')) { 1934f08c3bdfSopenharmony_ci sparse_error(token->pos, "expected ; at end of declaration"); 1935f08c3bdfSopenharmony_ci break; 1936f08c3bdfSopenharmony_ci } 1937f08c3bdfSopenharmony_ci token = token->next; 1938f08c3bdfSopenharmony_ci } 1939f08c3bdfSopenharmony_ci return token; 1940f08c3bdfSopenharmony_ci} 1941f08c3bdfSopenharmony_ci 1942f08c3bdfSopenharmony_cistatic struct token *parameter_declaration(struct token *token, struct symbol *sym) 1943f08c3bdfSopenharmony_ci{ 1944f08c3bdfSopenharmony_ci struct decl_state ctx = {.prefer_abstract = 1}; 1945f08c3bdfSopenharmony_ci 1946f08c3bdfSopenharmony_ci token = declaration_specifiers(token, &ctx); 1947f08c3bdfSopenharmony_ci ctx.ident = &sym->ident; 1948f08c3bdfSopenharmony_ci token = declarator(token, &ctx); 1949f08c3bdfSopenharmony_ci token = handle_attributes(token, &ctx); 1950f08c3bdfSopenharmony_ci apply_modifiers(token->pos, &ctx); 1951f08c3bdfSopenharmony_ci sym->ctype = ctx.ctype; 1952f08c3bdfSopenharmony_ci sym->ctype.modifiers |= decl_modifiers(&ctx); 1953f08c3bdfSopenharmony_ci sym->endpos = token->pos; 1954f08c3bdfSopenharmony_ci sym->forced_arg = ctx.forced; 1955f08c3bdfSopenharmony_ci return token; 1956f08c3bdfSopenharmony_ci} 1957f08c3bdfSopenharmony_ci 1958f08c3bdfSopenharmony_cistruct token *typename(struct token *token, struct symbol **p, int *forced) 1959f08c3bdfSopenharmony_ci{ 1960f08c3bdfSopenharmony_ci struct decl_state ctx = {.prefer_abstract = 1}; 1961f08c3bdfSopenharmony_ci unsigned long class; 1962f08c3bdfSopenharmony_ci struct symbol *sym = alloc_symbol(token->pos, SYM_NODE); 1963f08c3bdfSopenharmony_ci *p = sym; 1964f08c3bdfSopenharmony_ci token = declaration_specifiers(token, &ctx); 1965f08c3bdfSopenharmony_ci token = declarator(token, &ctx); 1966f08c3bdfSopenharmony_ci apply_modifiers(token->pos, &ctx); 1967f08c3bdfSopenharmony_ci sym->ctype = ctx.ctype; 1968f08c3bdfSopenharmony_ci sym->endpos = token->pos; 1969f08c3bdfSopenharmony_ci class = ctx.storage_class; 1970f08c3bdfSopenharmony_ci if (forced) 1971f08c3bdfSopenharmony_ci *forced = ctx.forced; 1972f08c3bdfSopenharmony_ci if (class) 1973f08c3bdfSopenharmony_ci warning(sym->pos, "storage class in typename (%s%s)", 1974f08c3bdfSopenharmony_ci modifier_string(class), show_typename(sym)); 1975f08c3bdfSopenharmony_ci return token; 1976f08c3bdfSopenharmony_ci} 1977f08c3bdfSopenharmony_ci 1978f08c3bdfSopenharmony_cistatic struct token *expression_statement(struct token *token, struct expression **tree) 1979f08c3bdfSopenharmony_ci{ 1980f08c3bdfSopenharmony_ci token = parse_expression(token, tree); 1981f08c3bdfSopenharmony_ci return expect(token, ';', "at end of statement"); 1982f08c3bdfSopenharmony_ci} 1983f08c3bdfSopenharmony_ci 1984f08c3bdfSopenharmony_cistatic struct token *parse_asm_operands(struct token *token, struct statement *stmt, 1985f08c3bdfSopenharmony_ci struct asm_operand_list **inout) 1986f08c3bdfSopenharmony_ci{ 1987f08c3bdfSopenharmony_ci /* Allow empty operands */ 1988f08c3bdfSopenharmony_ci if (match_op(token->next, ':') || match_op(token->next, ')')) 1989f08c3bdfSopenharmony_ci return token->next; 1990f08c3bdfSopenharmony_ci do { 1991f08c3bdfSopenharmony_ci struct asm_operand *op = __alloc_asm_operand(0); 1992f08c3bdfSopenharmony_ci if (match_op(token->next, '[') && 1993f08c3bdfSopenharmony_ci token_type(token->next->next) == TOKEN_IDENT && 1994f08c3bdfSopenharmony_ci match_op(token->next->next->next, ']')) { 1995f08c3bdfSopenharmony_ci op->name = token->next->next->ident; 1996f08c3bdfSopenharmony_ci token = token->next->next->next; 1997f08c3bdfSopenharmony_ci } 1998f08c3bdfSopenharmony_ci token = token->next; 1999f08c3bdfSopenharmony_ci token = string_expression(token, &op->constraint, "asm constraint"); 2000f08c3bdfSopenharmony_ci token = parens_expression(token, &op->expr, "in asm parameter"); 2001f08c3bdfSopenharmony_ci add_ptr_list(inout, op); 2002f08c3bdfSopenharmony_ci } while (match_op(token, ',')); 2003f08c3bdfSopenharmony_ci return token; 2004f08c3bdfSopenharmony_ci} 2005f08c3bdfSopenharmony_ci 2006f08c3bdfSopenharmony_cistatic struct token *parse_asm_clobbers(struct token *token, struct statement *stmt, 2007f08c3bdfSopenharmony_ci struct expression_list **clobbers) 2008f08c3bdfSopenharmony_ci{ 2009f08c3bdfSopenharmony_ci struct expression *expr; 2010f08c3bdfSopenharmony_ci 2011f08c3bdfSopenharmony_ci do { 2012f08c3bdfSopenharmony_ci token = primary_expression(token->next, &expr); 2013f08c3bdfSopenharmony_ci if (expr) 2014f08c3bdfSopenharmony_ci add_expression(clobbers, expr); 2015f08c3bdfSopenharmony_ci } while (match_op(token, ',')); 2016f08c3bdfSopenharmony_ci return token; 2017f08c3bdfSopenharmony_ci} 2018f08c3bdfSopenharmony_ci 2019f08c3bdfSopenharmony_cistatic struct token *parse_asm_labels(struct token *token, struct statement *stmt, 2020f08c3bdfSopenharmony_ci struct symbol_list **labels) 2021f08c3bdfSopenharmony_ci{ 2022f08c3bdfSopenharmony_ci struct symbol *label; 2023f08c3bdfSopenharmony_ci 2024f08c3bdfSopenharmony_ci do { 2025f08c3bdfSopenharmony_ci token = token->next; /* skip ':' and ',' */ 2026f08c3bdfSopenharmony_ci if (token_type(token) != TOKEN_IDENT) 2027f08c3bdfSopenharmony_ci return token; 2028f08c3bdfSopenharmony_ci label = label_symbol(token, 1); 2029f08c3bdfSopenharmony_ci add_symbol(labels, label); 2030f08c3bdfSopenharmony_ci token = token->next; 2031f08c3bdfSopenharmony_ci } while (match_op(token, ',')); 2032f08c3bdfSopenharmony_ci return token; 2033f08c3bdfSopenharmony_ci} 2034f08c3bdfSopenharmony_ci 2035f08c3bdfSopenharmony_cistatic struct token *parse_asm_statement(struct token *token, struct statement *stmt) 2036f08c3bdfSopenharmony_ci{ 2037f08c3bdfSopenharmony_ci unsigned long mods = 0; 2038f08c3bdfSopenharmony_ci 2039f08c3bdfSopenharmony_ci token = token->next; 2040f08c3bdfSopenharmony_ci stmt->type = STMT_ASM; 2041f08c3bdfSopenharmony_ci while (token_type(token) == TOKEN_IDENT) { 2042f08c3bdfSopenharmony_ci struct symbol *s = lookup_keyword(token->ident, NS_TYPEDEF); 2043f08c3bdfSopenharmony_ci if (s && s->op->asm_modifier) 2044f08c3bdfSopenharmony_ci s->op->asm_modifier(token, &mods, s->ctype.modifiers); 2045f08c3bdfSopenharmony_ci else if (token->ident == &goto_ident) 2046f08c3bdfSopenharmony_ci asm_modifier(token, &mods, MOD_ASM_GOTO); 2047f08c3bdfSopenharmony_ci token = token->next; 2048f08c3bdfSopenharmony_ci } 2049f08c3bdfSopenharmony_ci token = expect(token, '(', "after asm"); 2050f08c3bdfSopenharmony_ci token = string_expression(token, &stmt->asm_string, "inline asm"); 2051f08c3bdfSopenharmony_ci if (match_op(token, ':')) 2052f08c3bdfSopenharmony_ci token = parse_asm_operands(token, stmt, &stmt->asm_outputs); 2053f08c3bdfSopenharmony_ci if (match_op(token, ':')) 2054f08c3bdfSopenharmony_ci token = parse_asm_operands(token, stmt, &stmt->asm_inputs); 2055f08c3bdfSopenharmony_ci if (match_op(token, ':')) 2056f08c3bdfSopenharmony_ci token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers); 2057f08c3bdfSopenharmony_ci if (match_op(token, ':') && (mods & MOD_ASM_GOTO)) 2058f08c3bdfSopenharmony_ci token = parse_asm_labels(token, stmt, &stmt->asm_labels); 2059f08c3bdfSopenharmony_ci token = expect(token, ')', "after asm"); 2060f08c3bdfSopenharmony_ci return expect(token, ';', "at end of asm-statement"); 2061f08c3bdfSopenharmony_ci} 2062f08c3bdfSopenharmony_ci 2063f08c3bdfSopenharmony_cistatic struct token *parse_static_assert(struct token *token, struct symbol_list **unused) 2064f08c3bdfSopenharmony_ci{ 2065f08c3bdfSopenharmony_ci struct expression *cond = NULL, *message = NULL; 2066f08c3bdfSopenharmony_ci 2067f08c3bdfSopenharmony_ci token = expect(token->next, '(', "after _Static_assert"); 2068f08c3bdfSopenharmony_ci token = constant_expression(token, &cond); 2069f08c3bdfSopenharmony_ci if (!cond) 2070f08c3bdfSopenharmony_ci sparse_error(token->pos, "Expected constant expression"); 2071f08c3bdfSopenharmony_ci if (match_op(token, ',')) { 2072f08c3bdfSopenharmony_ci token = token->next; 2073f08c3bdfSopenharmony_ci token = string_expression(token, &message, "_Static_assert()"); 2074f08c3bdfSopenharmony_ci if (!message) 2075f08c3bdfSopenharmony_ci cond = NULL; 2076f08c3bdfSopenharmony_ci } 2077f08c3bdfSopenharmony_ci token = expect(token, ')', "after diagnostic message in _Static_assert"); 2078f08c3bdfSopenharmony_ci token = expect(token, ';', "after _Static_assert()"); 2079f08c3bdfSopenharmony_ci 2080f08c3bdfSopenharmony_ci if (cond && !const_expression_value(cond) && cond->type == EXPR_VALUE) { 2081f08c3bdfSopenharmony_ci const char *sep = "", *msg = ""; 2082f08c3bdfSopenharmony_ci 2083f08c3bdfSopenharmony_ci if (message) { 2084f08c3bdfSopenharmony_ci sep = ": "; 2085f08c3bdfSopenharmony_ci msg = show_string(message->string); 2086f08c3bdfSopenharmony_ci } 2087f08c3bdfSopenharmony_ci sparse_error(cond->pos, "static assertion failed%s%s", sep, msg); 2088f08c3bdfSopenharmony_ci } 2089f08c3bdfSopenharmony_ci 2090f08c3bdfSopenharmony_ci return token; 2091f08c3bdfSopenharmony_ci} 2092f08c3bdfSopenharmony_ci 2093f08c3bdfSopenharmony_ci/* Make a statement out of an expression */ 2094f08c3bdfSopenharmony_cistatic struct statement *make_statement(struct expression *expr) 2095f08c3bdfSopenharmony_ci{ 2096f08c3bdfSopenharmony_ci struct statement *stmt; 2097f08c3bdfSopenharmony_ci 2098f08c3bdfSopenharmony_ci if (!expr) 2099f08c3bdfSopenharmony_ci return NULL; 2100f08c3bdfSopenharmony_ci stmt = alloc_statement(expr->pos, STMT_EXPRESSION); 2101f08c3bdfSopenharmony_ci stmt->expression = expr; 2102f08c3bdfSopenharmony_ci return stmt; 2103f08c3bdfSopenharmony_ci} 2104f08c3bdfSopenharmony_ci 2105f08c3bdfSopenharmony_ci/* 2106f08c3bdfSopenharmony_ci * All iterators have two symbols associated with them: 2107f08c3bdfSopenharmony_ci * the "continue" and "break" symbols, which are targets 2108f08c3bdfSopenharmony_ci * for continue and break statements respectively. 2109f08c3bdfSopenharmony_ci * 2110f08c3bdfSopenharmony_ci * They are in a special name-space, but they follow 2111f08c3bdfSopenharmony_ci * all the normal visibility rules, so nested iterators 2112f08c3bdfSopenharmony_ci * automatically work right. 2113f08c3bdfSopenharmony_ci */ 2114f08c3bdfSopenharmony_cistatic void start_iterator(struct statement *stmt) 2115f08c3bdfSopenharmony_ci{ 2116f08c3bdfSopenharmony_ci struct symbol *cont, *brk; 2117f08c3bdfSopenharmony_ci 2118f08c3bdfSopenharmony_ci start_block_scope(); 2119f08c3bdfSopenharmony_ci cont = alloc_symbol(stmt->pos, SYM_NODE); 2120f08c3bdfSopenharmony_ci bind_symbol(cont, &continue_ident, NS_ITERATOR); 2121f08c3bdfSopenharmony_ci brk = alloc_symbol(stmt->pos, SYM_NODE); 2122f08c3bdfSopenharmony_ci bind_symbol(brk, &break_ident, NS_ITERATOR); 2123f08c3bdfSopenharmony_ci 2124f08c3bdfSopenharmony_ci stmt->type = STMT_ITERATOR; 2125f08c3bdfSopenharmony_ci stmt->iterator_break = brk; 2126f08c3bdfSopenharmony_ci stmt->iterator_continue = cont; 2127f08c3bdfSopenharmony_ci fn_local_symbol(brk); 2128f08c3bdfSopenharmony_ci fn_local_symbol(cont); 2129f08c3bdfSopenharmony_ci} 2130f08c3bdfSopenharmony_ci 2131f08c3bdfSopenharmony_cistatic void end_iterator(struct statement *stmt) 2132f08c3bdfSopenharmony_ci{ 2133f08c3bdfSopenharmony_ci end_block_scope(); 2134f08c3bdfSopenharmony_ci} 2135f08c3bdfSopenharmony_ci 2136f08c3bdfSopenharmony_cistatic struct statement *start_function(struct symbol *sym) 2137f08c3bdfSopenharmony_ci{ 2138f08c3bdfSopenharmony_ci struct symbol *ret; 2139f08c3bdfSopenharmony_ci struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND); 2140f08c3bdfSopenharmony_ci 2141f08c3bdfSopenharmony_ci start_function_scope(); 2142f08c3bdfSopenharmony_ci ret = alloc_symbol(sym->pos, SYM_NODE); 2143f08c3bdfSopenharmony_ci ret->ctype = sym->ctype.base_type->ctype; 2144f08c3bdfSopenharmony_ci ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_QUALIFIER | MOD_TLS | MOD_ACCESS | MOD_NOCAST | MOD_NODEREF); 2145f08c3bdfSopenharmony_ci ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER); 2146f08c3bdfSopenharmony_ci bind_symbol(ret, &return_ident, NS_ITERATOR); 2147f08c3bdfSopenharmony_ci stmt->ret = ret; 2148f08c3bdfSopenharmony_ci fn_local_symbol(ret); 2149f08c3bdfSopenharmony_ci 2150f08c3bdfSopenharmony_ci // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__ 2151f08c3bdfSopenharmony_ci current_fn = sym; 2152f08c3bdfSopenharmony_ci 2153f08c3bdfSopenharmony_ci return stmt; 2154f08c3bdfSopenharmony_ci} 2155f08c3bdfSopenharmony_ci 2156f08c3bdfSopenharmony_cistatic void end_function(struct symbol *sym) 2157f08c3bdfSopenharmony_ci{ 2158f08c3bdfSopenharmony_ci current_fn = NULL; 2159f08c3bdfSopenharmony_ci end_function_scope(); 2160f08c3bdfSopenharmony_ci} 2161f08c3bdfSopenharmony_ci 2162f08c3bdfSopenharmony_ci/* 2163f08c3bdfSopenharmony_ci * A "switch()" statement, like an iterator, has a 2164f08c3bdfSopenharmony_ci * the "break" symbol associated with it. It works 2165f08c3bdfSopenharmony_ci * exactly like the iterator break - it's the target 2166f08c3bdfSopenharmony_ci * for any break-statements in scope, and means that 2167f08c3bdfSopenharmony_ci * "break" handling doesn't even need to know whether 2168f08c3bdfSopenharmony_ci * it's breaking out of an iterator or a switch. 2169f08c3bdfSopenharmony_ci * 2170f08c3bdfSopenharmony_ci * In addition, the "case" symbol is a marker for the 2171f08c3bdfSopenharmony_ci * case/default statements to find the switch statement 2172f08c3bdfSopenharmony_ci * that they are associated with. 2173f08c3bdfSopenharmony_ci */ 2174f08c3bdfSopenharmony_cistatic void start_switch(struct statement *stmt) 2175f08c3bdfSopenharmony_ci{ 2176f08c3bdfSopenharmony_ci struct symbol *brk, *switch_case; 2177f08c3bdfSopenharmony_ci 2178f08c3bdfSopenharmony_ci start_block_scope(); 2179f08c3bdfSopenharmony_ci brk = alloc_symbol(stmt->pos, SYM_NODE); 2180f08c3bdfSopenharmony_ci bind_symbol(brk, &break_ident, NS_ITERATOR); 2181f08c3bdfSopenharmony_ci 2182f08c3bdfSopenharmony_ci switch_case = alloc_symbol(stmt->pos, SYM_NODE); 2183f08c3bdfSopenharmony_ci bind_symbol(switch_case, &case_ident, NS_ITERATOR); 2184f08c3bdfSopenharmony_ci switch_case->stmt = stmt; 2185f08c3bdfSopenharmony_ci 2186f08c3bdfSopenharmony_ci stmt->type = STMT_SWITCH; 2187f08c3bdfSopenharmony_ci stmt->switch_break = brk; 2188f08c3bdfSopenharmony_ci stmt->switch_case = switch_case; 2189f08c3bdfSopenharmony_ci 2190f08c3bdfSopenharmony_ci fn_local_symbol(brk); 2191f08c3bdfSopenharmony_ci fn_local_symbol(switch_case); 2192f08c3bdfSopenharmony_ci} 2193f08c3bdfSopenharmony_ci 2194f08c3bdfSopenharmony_cistatic void end_switch(struct statement *stmt) 2195f08c3bdfSopenharmony_ci{ 2196f08c3bdfSopenharmony_ci if (!stmt->switch_case->symbol_list) 2197f08c3bdfSopenharmony_ci warning(stmt->pos, "switch with no cases"); 2198f08c3bdfSopenharmony_ci end_block_scope(); 2199f08c3bdfSopenharmony_ci} 2200f08c3bdfSopenharmony_ci 2201f08c3bdfSopenharmony_cistatic void add_case_statement(struct statement *stmt) 2202f08c3bdfSopenharmony_ci{ 2203f08c3bdfSopenharmony_ci struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR); 2204f08c3bdfSopenharmony_ci struct symbol *sym; 2205f08c3bdfSopenharmony_ci 2206f08c3bdfSopenharmony_ci if (!target) { 2207f08c3bdfSopenharmony_ci sparse_error(stmt->pos, "not in switch scope"); 2208f08c3bdfSopenharmony_ci stmt->type = STMT_NONE; 2209f08c3bdfSopenharmony_ci return; 2210f08c3bdfSopenharmony_ci } 2211f08c3bdfSopenharmony_ci sym = alloc_symbol(stmt->pos, SYM_NODE); 2212f08c3bdfSopenharmony_ci add_symbol(&target->symbol_list, sym); 2213f08c3bdfSopenharmony_ci sym->stmt = stmt; 2214f08c3bdfSopenharmony_ci stmt->case_label = sym; 2215f08c3bdfSopenharmony_ci fn_local_symbol(sym); 2216f08c3bdfSopenharmony_ci} 2217f08c3bdfSopenharmony_ci 2218f08c3bdfSopenharmony_cistatic struct token *parse_return_statement(struct token *token, struct statement *stmt) 2219f08c3bdfSopenharmony_ci{ 2220f08c3bdfSopenharmony_ci struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR); 2221f08c3bdfSopenharmony_ci 2222f08c3bdfSopenharmony_ci if (!target) 2223f08c3bdfSopenharmony_ci error_die(token->pos, "internal error: return without a function target"); 2224f08c3bdfSopenharmony_ci stmt->type = STMT_RETURN; 2225f08c3bdfSopenharmony_ci stmt->ret_target = target; 2226f08c3bdfSopenharmony_ci return expression_statement(token->next, &stmt->ret_value); 2227f08c3bdfSopenharmony_ci} 2228f08c3bdfSopenharmony_ci 2229f08c3bdfSopenharmony_cistatic void validate_for_loop_decl(struct symbol *sym) 2230f08c3bdfSopenharmony_ci{ 2231f08c3bdfSopenharmony_ci unsigned long storage = sym->ctype.modifiers & MOD_STORAGE; 2232f08c3bdfSopenharmony_ci 2233f08c3bdfSopenharmony_ci if (storage & ~(MOD_AUTO | MOD_REGISTER)) { 2234f08c3bdfSopenharmony_ci const char *name = show_ident(sym->ident); 2235f08c3bdfSopenharmony_ci sparse_error(sym->pos, "non-local var '%s' in for-loop initializer", name); 2236f08c3bdfSopenharmony_ci sym->ctype.modifiers &= ~MOD_STORAGE; 2237f08c3bdfSopenharmony_ci } 2238f08c3bdfSopenharmony_ci} 2239f08c3bdfSopenharmony_ci 2240f08c3bdfSopenharmony_cistatic struct token *parse_for_statement(struct token *token, struct statement *stmt) 2241f08c3bdfSopenharmony_ci{ 2242f08c3bdfSopenharmony_ci struct symbol_list *syms; 2243f08c3bdfSopenharmony_ci struct expression *e1, *e2, *e3; 2244f08c3bdfSopenharmony_ci struct statement *iterator; 2245f08c3bdfSopenharmony_ci 2246f08c3bdfSopenharmony_ci start_iterator(stmt); 2247f08c3bdfSopenharmony_ci token = expect(token->next, '(', "after 'for'"); 2248f08c3bdfSopenharmony_ci 2249f08c3bdfSopenharmony_ci syms = NULL; 2250f08c3bdfSopenharmony_ci e1 = NULL; 2251f08c3bdfSopenharmony_ci /* C99 variable declaration? */ 2252f08c3bdfSopenharmony_ci if (lookup_type(token)) { 2253f08c3bdfSopenharmony_ci token = external_declaration(token, &syms, validate_for_loop_decl); 2254f08c3bdfSopenharmony_ci } else { 2255f08c3bdfSopenharmony_ci token = parse_expression(token, &e1); 2256f08c3bdfSopenharmony_ci token = expect(token, ';', "in 'for'"); 2257f08c3bdfSopenharmony_ci } 2258f08c3bdfSopenharmony_ci token = parse_expression(token, &e2); 2259f08c3bdfSopenharmony_ci token = expect(token, ';', "in 'for'"); 2260f08c3bdfSopenharmony_ci token = parse_expression(token, &e3); 2261f08c3bdfSopenharmony_ci token = expect(token, ')', "in 'for'"); 2262f08c3bdfSopenharmony_ci token = statement(token, &iterator); 2263f08c3bdfSopenharmony_ci 2264f08c3bdfSopenharmony_ci stmt->iterator_syms = syms; 2265f08c3bdfSopenharmony_ci stmt->iterator_pre_statement = make_statement(e1); 2266f08c3bdfSopenharmony_ci stmt->iterator_pre_condition = e2; 2267f08c3bdfSopenharmony_ci stmt->iterator_post_statement = make_statement(e3); 2268f08c3bdfSopenharmony_ci stmt->iterator_post_condition = NULL; 2269f08c3bdfSopenharmony_ci stmt->iterator_statement = iterator; 2270f08c3bdfSopenharmony_ci end_iterator(stmt); 2271f08c3bdfSopenharmony_ci 2272f08c3bdfSopenharmony_ci return token; 2273f08c3bdfSopenharmony_ci} 2274f08c3bdfSopenharmony_ci 2275f08c3bdfSopenharmony_cistatic struct token *parse_while_statement(struct token *token, struct statement *stmt) 2276f08c3bdfSopenharmony_ci{ 2277f08c3bdfSopenharmony_ci struct expression *expr; 2278f08c3bdfSopenharmony_ci struct statement *iterator; 2279f08c3bdfSopenharmony_ci 2280f08c3bdfSopenharmony_ci start_iterator(stmt); 2281f08c3bdfSopenharmony_ci token = parens_expression(token->next, &expr, "after 'while'"); 2282f08c3bdfSopenharmony_ci token = statement(token, &iterator); 2283f08c3bdfSopenharmony_ci 2284f08c3bdfSopenharmony_ci stmt->iterator_pre_condition = expr; 2285f08c3bdfSopenharmony_ci stmt->iterator_post_condition = NULL; 2286f08c3bdfSopenharmony_ci stmt->iterator_statement = iterator; 2287f08c3bdfSopenharmony_ci end_iterator(stmt); 2288f08c3bdfSopenharmony_ci 2289f08c3bdfSopenharmony_ci return token; 2290f08c3bdfSopenharmony_ci} 2291f08c3bdfSopenharmony_ci 2292f08c3bdfSopenharmony_cistatic struct token *parse_do_statement(struct token *token, struct statement *stmt) 2293f08c3bdfSopenharmony_ci{ 2294f08c3bdfSopenharmony_ci struct expression *expr; 2295f08c3bdfSopenharmony_ci struct statement *iterator; 2296f08c3bdfSopenharmony_ci 2297f08c3bdfSopenharmony_ci start_iterator(stmt); 2298f08c3bdfSopenharmony_ci token = statement(token->next, &iterator); 2299f08c3bdfSopenharmony_ci if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident) 2300f08c3bdfSopenharmony_ci token = token->next; 2301f08c3bdfSopenharmony_ci else 2302f08c3bdfSopenharmony_ci sparse_error(token->pos, "expected 'while' after 'do'"); 2303f08c3bdfSopenharmony_ci token = parens_expression(token, &expr, "after 'do-while'"); 2304f08c3bdfSopenharmony_ci 2305f08c3bdfSopenharmony_ci stmt->iterator_post_condition = expr; 2306f08c3bdfSopenharmony_ci stmt->iterator_statement = iterator; 2307f08c3bdfSopenharmony_ci end_iterator(stmt); 2308f08c3bdfSopenharmony_ci 2309f08c3bdfSopenharmony_ci if (iterator && iterator->type != STMT_COMPOUND && Wdo_while) 2310f08c3bdfSopenharmony_ci warning(iterator->pos, "do-while statement is not a compound statement"); 2311f08c3bdfSopenharmony_ci 2312f08c3bdfSopenharmony_ci return expect(token, ';', "after statement"); 2313f08c3bdfSopenharmony_ci} 2314f08c3bdfSopenharmony_ci 2315f08c3bdfSopenharmony_cistatic struct token *parse_if_statement(struct token *token, struct statement *stmt) 2316f08c3bdfSopenharmony_ci{ 2317f08c3bdfSopenharmony_ci stmt->type = STMT_IF; 2318f08c3bdfSopenharmony_ci token = parens_expression(token->next, &stmt->if_conditional, "after if"); 2319f08c3bdfSopenharmony_ci token = statement(token, &stmt->if_true); 2320f08c3bdfSopenharmony_ci if (token_type(token) != TOKEN_IDENT) 2321f08c3bdfSopenharmony_ci return token; 2322f08c3bdfSopenharmony_ci if (token->ident != &else_ident) 2323f08c3bdfSopenharmony_ci return token; 2324f08c3bdfSopenharmony_ci return statement(token->next, &stmt->if_false); 2325f08c3bdfSopenharmony_ci} 2326f08c3bdfSopenharmony_ci 2327f08c3bdfSopenharmony_cistatic inline struct token *case_statement(struct token *token, struct statement *stmt) 2328f08c3bdfSopenharmony_ci{ 2329f08c3bdfSopenharmony_ci stmt->type = STMT_CASE; 2330f08c3bdfSopenharmony_ci token = expect(token, ':', "after default/case"); 2331f08c3bdfSopenharmony_ci add_case_statement(stmt); 2332f08c3bdfSopenharmony_ci return statement(token, &stmt->case_statement); 2333f08c3bdfSopenharmony_ci} 2334f08c3bdfSopenharmony_ci 2335f08c3bdfSopenharmony_cistatic struct token *parse_case_statement(struct token *token, struct statement *stmt) 2336f08c3bdfSopenharmony_ci{ 2337f08c3bdfSopenharmony_ci token = parse_expression(token->next, &stmt->case_expression); 2338f08c3bdfSopenharmony_ci if (match_op(token, SPECIAL_ELLIPSIS)) 2339f08c3bdfSopenharmony_ci token = parse_expression(token->next, &stmt->case_to); 2340f08c3bdfSopenharmony_ci return case_statement(token, stmt); 2341f08c3bdfSopenharmony_ci} 2342f08c3bdfSopenharmony_ci 2343f08c3bdfSopenharmony_cistatic struct token *parse_default_statement(struct token *token, struct statement *stmt) 2344f08c3bdfSopenharmony_ci{ 2345f08c3bdfSopenharmony_ci return case_statement(token->next, stmt); 2346f08c3bdfSopenharmony_ci} 2347f08c3bdfSopenharmony_ci 2348f08c3bdfSopenharmony_cistatic struct token *parse_loop_iterator(struct token *token, struct statement *stmt) 2349f08c3bdfSopenharmony_ci{ 2350f08c3bdfSopenharmony_ci struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR); 2351f08c3bdfSopenharmony_ci stmt->type = STMT_GOTO; 2352f08c3bdfSopenharmony_ci stmt->goto_label = target; 2353f08c3bdfSopenharmony_ci if (!target) 2354f08c3bdfSopenharmony_ci sparse_error(stmt->pos, "break/continue not in iterator scope"); 2355f08c3bdfSopenharmony_ci return expect(token->next, ';', "at end of statement"); 2356f08c3bdfSopenharmony_ci} 2357f08c3bdfSopenharmony_ci 2358f08c3bdfSopenharmony_cistatic struct token *parse_switch_statement(struct token *token, struct statement *stmt) 2359f08c3bdfSopenharmony_ci{ 2360f08c3bdfSopenharmony_ci stmt->type = STMT_SWITCH; 2361f08c3bdfSopenharmony_ci start_switch(stmt); 2362f08c3bdfSopenharmony_ci token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'"); 2363f08c3bdfSopenharmony_ci token = statement(token, &stmt->switch_statement); 2364f08c3bdfSopenharmony_ci end_switch(stmt); 2365f08c3bdfSopenharmony_ci return token; 2366f08c3bdfSopenharmony_ci} 2367f08c3bdfSopenharmony_ci 2368f08c3bdfSopenharmony_cistatic void warn_label_usage(struct position def, struct position use, struct ident *ident) 2369f08c3bdfSopenharmony_ci{ 2370f08c3bdfSopenharmony_ci const char *id = show_ident(ident); 2371f08c3bdfSopenharmony_ci sparse_error(use, "label '%s' used outside statement expression", id); 2372f08c3bdfSopenharmony_ci info(def, " label '%s' defined here", id); 2373f08c3bdfSopenharmony_ci current_fn->bogus_linear = 1; 2374f08c3bdfSopenharmony_ci} 2375f08c3bdfSopenharmony_ci 2376f08c3bdfSopenharmony_civoid check_label_usage(struct symbol *label, struct position use_pos) 2377f08c3bdfSopenharmony_ci{ 2378f08c3bdfSopenharmony_ci struct statement *def = label->stmt; 2379f08c3bdfSopenharmony_ci 2380f08c3bdfSopenharmony_ci if (def) { 2381f08c3bdfSopenharmony_ci if (!is_in_scope(def->label_scope, label_scope)) 2382f08c3bdfSopenharmony_ci warn_label_usage(def->pos, use_pos, label->ident); 2383f08c3bdfSopenharmony_ci } else if (!label->label_scope) { 2384f08c3bdfSopenharmony_ci label->label_scope = label_scope; 2385f08c3bdfSopenharmony_ci label->label_pos = use_pos; 2386f08c3bdfSopenharmony_ci } 2387f08c3bdfSopenharmony_ci} 2388f08c3bdfSopenharmony_ci 2389f08c3bdfSopenharmony_cistatic struct token *parse_goto_statement(struct token *token, struct statement *stmt) 2390f08c3bdfSopenharmony_ci{ 2391f08c3bdfSopenharmony_ci stmt->type = STMT_GOTO; 2392f08c3bdfSopenharmony_ci token = token->next; 2393f08c3bdfSopenharmony_ci if (match_op(token, '*')) { 2394f08c3bdfSopenharmony_ci token = parse_expression(token->next, &stmt->goto_expression); 2395f08c3bdfSopenharmony_ci add_statement(&function_computed_goto_list, stmt); 2396f08c3bdfSopenharmony_ci } else if (token_type(token) == TOKEN_IDENT) { 2397f08c3bdfSopenharmony_ci struct symbol *label = label_symbol(token, 1); 2398f08c3bdfSopenharmony_ci stmt->goto_label = label; 2399f08c3bdfSopenharmony_ci check_label_usage(label, stmt->pos); 2400f08c3bdfSopenharmony_ci token = token->next; 2401f08c3bdfSopenharmony_ci } else { 2402f08c3bdfSopenharmony_ci sparse_error(token->pos, "Expected identifier or goto expression"); 2403f08c3bdfSopenharmony_ci } 2404f08c3bdfSopenharmony_ci return expect(token, ';', "at end of statement"); 2405f08c3bdfSopenharmony_ci} 2406f08c3bdfSopenharmony_ci 2407f08c3bdfSopenharmony_cistatic struct token *parse_context_statement(struct token *token, struct statement *stmt) 2408f08c3bdfSopenharmony_ci{ 2409f08c3bdfSopenharmony_ci stmt->type = STMT_CONTEXT; 2410f08c3bdfSopenharmony_ci token = token->next; 2411f08c3bdfSopenharmony_ci token = expect(token, '(', "after __context__ statement"); 2412f08c3bdfSopenharmony_ci token = assignment_expression(token, &stmt->expression); 2413f08c3bdfSopenharmony_ci if (!stmt->expression) 2414f08c3bdfSopenharmony_ci unexpected(token, "expression expected after '('"); 2415f08c3bdfSopenharmony_ci if (match_op(token, ',')) { 2416f08c3bdfSopenharmony_ci token = token->next; 2417f08c3bdfSopenharmony_ci stmt->context = stmt->expression; 2418f08c3bdfSopenharmony_ci token = assignment_expression(token, &stmt->expression); 2419f08c3bdfSopenharmony_ci if (!stmt->expression) 2420f08c3bdfSopenharmony_ci unexpected(token, "expression expected after ','"); 2421f08c3bdfSopenharmony_ci } 2422f08c3bdfSopenharmony_ci token = expect(token, ')', "at end of __context__ statement"); 2423f08c3bdfSopenharmony_ci return expect(token, ';', "at end of statement"); 2424f08c3bdfSopenharmony_ci} 2425f08c3bdfSopenharmony_ci 2426f08c3bdfSopenharmony_cistatic struct token *parse_range_statement(struct token *token, struct statement *stmt) 2427f08c3bdfSopenharmony_ci{ 2428f08c3bdfSopenharmony_ci stmt->type = STMT_RANGE; 2429f08c3bdfSopenharmony_ci token = token->next; 2430f08c3bdfSopenharmony_ci token = expect(token, '(', "after __range__ statement"); 2431f08c3bdfSopenharmony_ci token = assignment_expression(token, &stmt->range_expression); 2432f08c3bdfSopenharmony_ci token = expect(token, ',', "after range expression"); 2433f08c3bdfSopenharmony_ci token = assignment_expression(token, &stmt->range_low); 2434f08c3bdfSopenharmony_ci token = expect(token, ',', "after low range"); 2435f08c3bdfSopenharmony_ci token = assignment_expression(token, &stmt->range_high); 2436f08c3bdfSopenharmony_ci token = expect(token, ')', "after range statement"); 2437f08c3bdfSopenharmony_ci return expect(token, ';', "after range statement"); 2438f08c3bdfSopenharmony_ci} 2439f08c3bdfSopenharmony_ci 2440f08c3bdfSopenharmony_cistatic struct token *handle_label_attributes(struct token *token, struct symbol *label) 2441f08c3bdfSopenharmony_ci{ 2442f08c3bdfSopenharmony_ci struct decl_state ctx = { }; 2443f08c3bdfSopenharmony_ci 2444f08c3bdfSopenharmony_ci token = handle_attributes(token, &ctx); 2445f08c3bdfSopenharmony_ci label->label_modifiers = ctx.ctype.modifiers; 2446f08c3bdfSopenharmony_ci return token; 2447f08c3bdfSopenharmony_ci} 2448f08c3bdfSopenharmony_ci 2449f08c3bdfSopenharmony_cistatic struct token *statement(struct token *token, struct statement **tree) 2450f08c3bdfSopenharmony_ci{ 2451f08c3bdfSopenharmony_ci struct statement *stmt = alloc_statement(token->pos, STMT_NONE); 2452f08c3bdfSopenharmony_ci 2453f08c3bdfSopenharmony_ci *tree = stmt; 2454f08c3bdfSopenharmony_ci if (token_type(token) == TOKEN_IDENT) { 2455f08c3bdfSopenharmony_ci struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD); 2456f08c3bdfSopenharmony_ci if (s && s->op->statement) 2457f08c3bdfSopenharmony_ci return s->op->statement(token, stmt); 2458f08c3bdfSopenharmony_ci 2459f08c3bdfSopenharmony_ci if (match_op(token->next, ':')) { 2460f08c3bdfSopenharmony_ci struct symbol *s = label_symbol(token, 0); 2461f08c3bdfSopenharmony_ci token = handle_label_attributes(token->next->next, s); 2462f08c3bdfSopenharmony_ci if (s->stmt) { 2463f08c3bdfSopenharmony_ci sparse_error(stmt->pos, "label '%s' redefined", show_ident(s->ident)); 2464f08c3bdfSopenharmony_ci // skip the label to avoid multiple definitions 2465f08c3bdfSopenharmony_ci return statement(token, tree); 2466f08c3bdfSopenharmony_ci } 2467f08c3bdfSopenharmony_ci stmt->type = STMT_LABEL; 2468f08c3bdfSopenharmony_ci stmt->label_identifier = s; 2469f08c3bdfSopenharmony_ci stmt->label_scope = label_scope; 2470f08c3bdfSopenharmony_ci if (s->label_scope) { 2471f08c3bdfSopenharmony_ci if (!is_in_scope(label_scope, s->label_scope)) 2472f08c3bdfSopenharmony_ci warn_label_usage(stmt->pos, s->label_pos, s->ident); 2473f08c3bdfSopenharmony_ci } 2474f08c3bdfSopenharmony_ci s->stmt = stmt; 2475f08c3bdfSopenharmony_ci if (match_op(token, '}')) { 2476f08c3bdfSopenharmony_ci warning(token->pos, "statement expected after label"); 2477f08c3bdfSopenharmony_ci stmt->label_statement = alloc_statement(token->pos, STMT_NONE); 2478f08c3bdfSopenharmony_ci return token; 2479f08c3bdfSopenharmony_ci } 2480f08c3bdfSopenharmony_ci return statement(token, &stmt->label_statement); 2481f08c3bdfSopenharmony_ci } 2482f08c3bdfSopenharmony_ci } 2483f08c3bdfSopenharmony_ci 2484f08c3bdfSopenharmony_ci if (match_op(token, '{')) { 2485f08c3bdfSopenharmony_ci token = compound_statement(token->next, stmt); 2486f08c3bdfSopenharmony_ci return expect(token, '}', "at end of compound statement"); 2487f08c3bdfSopenharmony_ci } 2488f08c3bdfSopenharmony_ci 2489f08c3bdfSopenharmony_ci stmt->type = STMT_EXPRESSION; 2490f08c3bdfSopenharmony_ci return expression_statement(token, &stmt->expression); 2491f08c3bdfSopenharmony_ci} 2492f08c3bdfSopenharmony_ci 2493f08c3bdfSopenharmony_ci/* gcc extension - __label__ ident-list; in the beginning of compound stmt */ 2494f08c3bdfSopenharmony_cistatic struct token *label_statement(struct token *token) 2495f08c3bdfSopenharmony_ci{ 2496f08c3bdfSopenharmony_ci while (token_type(token) == TOKEN_IDENT) { 2497f08c3bdfSopenharmony_ci struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL); 2498f08c3bdfSopenharmony_ci /* it's block-scope, but we want label namespace */ 2499f08c3bdfSopenharmony_ci bind_symbol_with_scope(sym, token->ident, NS_LABEL, block_scope); 2500f08c3bdfSopenharmony_ci fn_local_symbol(sym); 2501f08c3bdfSopenharmony_ci token = token->next; 2502f08c3bdfSopenharmony_ci if (!match_op(token, ',')) 2503f08c3bdfSopenharmony_ci break; 2504f08c3bdfSopenharmony_ci token = token->next; 2505f08c3bdfSopenharmony_ci } 2506f08c3bdfSopenharmony_ci return expect(token, ';', "at end of label declaration"); 2507f08c3bdfSopenharmony_ci} 2508f08c3bdfSopenharmony_ci 2509f08c3bdfSopenharmony_cistatic struct token * statement_list(struct token *token, struct statement_list **list) 2510f08c3bdfSopenharmony_ci{ 2511f08c3bdfSopenharmony_ci int seen_statement = 0; 2512f08c3bdfSopenharmony_ci while (token_type(token) == TOKEN_IDENT && 2513f08c3bdfSopenharmony_ci token->ident == &__label___ident) 2514f08c3bdfSopenharmony_ci token = label_statement(token->next); 2515f08c3bdfSopenharmony_ci for (;;) { 2516f08c3bdfSopenharmony_ci struct statement * stmt; 2517f08c3bdfSopenharmony_ci if (eof_token(token)) 2518f08c3bdfSopenharmony_ci break; 2519f08c3bdfSopenharmony_ci if (match_op(token, '}')) 2520f08c3bdfSopenharmony_ci break; 2521f08c3bdfSopenharmony_ci if (match_ident(token, &_Static_assert_ident)) { 2522f08c3bdfSopenharmony_ci token = parse_static_assert(token, NULL); 2523f08c3bdfSopenharmony_ci continue; 2524f08c3bdfSopenharmony_ci } 2525f08c3bdfSopenharmony_ci if (lookup_type(token)) { 2526f08c3bdfSopenharmony_ci if (seen_statement) { 2527f08c3bdfSopenharmony_ci warning(token->pos, "mixing declarations and code"); 2528f08c3bdfSopenharmony_ci seen_statement = 0; 2529f08c3bdfSopenharmony_ci } 2530f08c3bdfSopenharmony_ci stmt = alloc_statement(token->pos, STMT_DECLARATION); 2531f08c3bdfSopenharmony_ci token = external_declaration(token, &stmt->declaration, NULL); 2532f08c3bdfSopenharmony_ci } else { 2533f08c3bdfSopenharmony_ci seen_statement = Wdeclarationafterstatement; 2534f08c3bdfSopenharmony_ci token = statement(token, &stmt); 2535f08c3bdfSopenharmony_ci } 2536f08c3bdfSopenharmony_ci add_statement(list, stmt); 2537f08c3bdfSopenharmony_ci } 2538f08c3bdfSopenharmony_ci return token; 2539f08c3bdfSopenharmony_ci} 2540f08c3bdfSopenharmony_ci 2541f08c3bdfSopenharmony_cistatic struct token *identifier_list(struct token *token, struct symbol *fn) 2542f08c3bdfSopenharmony_ci{ 2543f08c3bdfSopenharmony_ci struct symbol_list **list = &fn->arguments; 2544f08c3bdfSopenharmony_ci for (;;) { 2545f08c3bdfSopenharmony_ci struct symbol *sym = alloc_symbol(token->pos, SYM_NODE); 2546f08c3bdfSopenharmony_ci sym->ident = token->ident; 2547f08c3bdfSopenharmony_ci token = token->next; 2548f08c3bdfSopenharmony_ci sym->endpos = token->pos; 2549f08c3bdfSopenharmony_ci sym->ctype.base_type = &incomplete_ctype; 2550f08c3bdfSopenharmony_ci add_symbol(list, sym); 2551f08c3bdfSopenharmony_ci if (!match_op(token, ',') || 2552f08c3bdfSopenharmony_ci token_type(token->next) != TOKEN_IDENT || 2553f08c3bdfSopenharmony_ci lookup_type(token->next)) 2554f08c3bdfSopenharmony_ci break; 2555f08c3bdfSopenharmony_ci token = token->next; 2556f08c3bdfSopenharmony_ci } 2557f08c3bdfSopenharmony_ci return token; 2558f08c3bdfSopenharmony_ci} 2559f08c3bdfSopenharmony_ci 2560f08c3bdfSopenharmony_cistatic struct token *parameter_type_list(struct token *token, struct symbol *fn) 2561f08c3bdfSopenharmony_ci{ 2562f08c3bdfSopenharmony_ci struct symbol_list **list = &fn->arguments; 2563f08c3bdfSopenharmony_ci 2564f08c3bdfSopenharmony_ci for (;;) { 2565f08c3bdfSopenharmony_ci struct symbol *sym; 2566f08c3bdfSopenharmony_ci 2567f08c3bdfSopenharmony_ci if (match_op(token, SPECIAL_ELLIPSIS)) { 2568f08c3bdfSopenharmony_ci fn->variadic = 1; 2569f08c3bdfSopenharmony_ci token = token->next; 2570f08c3bdfSopenharmony_ci break; 2571f08c3bdfSopenharmony_ci } 2572f08c3bdfSopenharmony_ci 2573f08c3bdfSopenharmony_ci sym = alloc_symbol(token->pos, SYM_NODE); 2574f08c3bdfSopenharmony_ci token = parameter_declaration(token, sym); 2575f08c3bdfSopenharmony_ci if (sym->ctype.base_type == &void_ctype) { 2576f08c3bdfSopenharmony_ci /* Special case: (void) */ 2577f08c3bdfSopenharmony_ci if (!*list && !sym->ident) 2578f08c3bdfSopenharmony_ci break; 2579f08c3bdfSopenharmony_ci warning(token->pos, "void parameter"); 2580f08c3bdfSopenharmony_ci } 2581f08c3bdfSopenharmony_ci add_symbol(list, sym); 2582f08c3bdfSopenharmony_ci if (!match_op(token, ',')) 2583f08c3bdfSopenharmony_ci break; 2584f08c3bdfSopenharmony_ci token = token->next; 2585f08c3bdfSopenharmony_ci } 2586f08c3bdfSopenharmony_ci return token; 2587f08c3bdfSopenharmony_ci} 2588f08c3bdfSopenharmony_ci 2589f08c3bdfSopenharmony_cistruct token *compound_statement(struct token *token, struct statement *stmt) 2590f08c3bdfSopenharmony_ci{ 2591f08c3bdfSopenharmony_ci stmt->type = STMT_COMPOUND; 2592f08c3bdfSopenharmony_ci start_block_scope(); 2593f08c3bdfSopenharmony_ci token = statement_list(token, &stmt->stmts); 2594f08c3bdfSopenharmony_ci end_block_scope(); 2595f08c3bdfSopenharmony_ci return token; 2596f08c3bdfSopenharmony_ci} 2597f08c3bdfSopenharmony_ci 2598f08c3bdfSopenharmony_cistatic struct expression *identifier_expression(struct token *token) 2599f08c3bdfSopenharmony_ci{ 2600f08c3bdfSopenharmony_ci struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER); 2601f08c3bdfSopenharmony_ci expr->expr_ident = token->ident; 2602f08c3bdfSopenharmony_ci return expr; 2603f08c3bdfSopenharmony_ci} 2604f08c3bdfSopenharmony_ci 2605f08c3bdfSopenharmony_cistatic struct expression *index_expression(struct expression *from, struct expression *to) 2606f08c3bdfSopenharmony_ci{ 2607f08c3bdfSopenharmony_ci int idx_from, idx_to; 2608f08c3bdfSopenharmony_ci struct expression *expr = alloc_expression(from->pos, EXPR_INDEX); 2609f08c3bdfSopenharmony_ci 2610f08c3bdfSopenharmony_ci idx_from = const_expression_value(from); 2611f08c3bdfSopenharmony_ci idx_to = idx_from; 2612f08c3bdfSopenharmony_ci if (to) { 2613f08c3bdfSopenharmony_ci idx_to = const_expression_value(to); 2614f08c3bdfSopenharmony_ci if (idx_to < idx_from || idx_from < 0) 2615f08c3bdfSopenharmony_ci warning(from->pos, "nonsense array initializer index range"); 2616f08c3bdfSopenharmony_ci } 2617f08c3bdfSopenharmony_ci expr->idx_from = idx_from; 2618f08c3bdfSopenharmony_ci expr->idx_to = idx_to; 2619f08c3bdfSopenharmony_ci return expr; 2620f08c3bdfSopenharmony_ci} 2621f08c3bdfSopenharmony_ci 2622f08c3bdfSopenharmony_cistatic struct token *single_initializer(struct expression **ep, struct token *token) 2623f08c3bdfSopenharmony_ci{ 2624f08c3bdfSopenharmony_ci int expect_equal = 0; 2625f08c3bdfSopenharmony_ci struct token *next = token->next; 2626f08c3bdfSopenharmony_ci struct expression **tail = ep; 2627f08c3bdfSopenharmony_ci int nested; 2628f08c3bdfSopenharmony_ci 2629f08c3bdfSopenharmony_ci *ep = NULL; 2630f08c3bdfSopenharmony_ci 2631f08c3bdfSopenharmony_ci if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) { 2632f08c3bdfSopenharmony_ci struct expression *expr = identifier_expression(token); 2633f08c3bdfSopenharmony_ci if (Wold_initializer) 2634f08c3bdfSopenharmony_ci warning(token->pos, "obsolete struct initializer, use C99 syntax"); 2635f08c3bdfSopenharmony_ci token = initializer(&expr->ident_expression, next->next); 2636f08c3bdfSopenharmony_ci if (expr->ident_expression) 2637f08c3bdfSopenharmony_ci *ep = expr; 2638f08c3bdfSopenharmony_ci return token; 2639f08c3bdfSopenharmony_ci } 2640f08c3bdfSopenharmony_ci 2641f08c3bdfSopenharmony_ci for (tail = ep, nested = 0; ; nested++, next = token->next) { 2642f08c3bdfSopenharmony_ci if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) { 2643f08c3bdfSopenharmony_ci struct expression *expr = identifier_expression(next); 2644f08c3bdfSopenharmony_ci *tail = expr; 2645f08c3bdfSopenharmony_ci tail = &expr->ident_expression; 2646f08c3bdfSopenharmony_ci expect_equal = 1; 2647f08c3bdfSopenharmony_ci token = next->next; 2648f08c3bdfSopenharmony_ci } else if (match_op(token, '[')) { 2649f08c3bdfSopenharmony_ci struct expression *from = NULL, *to = NULL, *expr; 2650f08c3bdfSopenharmony_ci token = constant_expression(token->next, &from); 2651f08c3bdfSopenharmony_ci if (!from) { 2652f08c3bdfSopenharmony_ci sparse_error(token->pos, "Expected constant expression"); 2653f08c3bdfSopenharmony_ci break; 2654f08c3bdfSopenharmony_ci } 2655f08c3bdfSopenharmony_ci if (match_op(token, SPECIAL_ELLIPSIS)) 2656f08c3bdfSopenharmony_ci token = constant_expression(token->next, &to); 2657f08c3bdfSopenharmony_ci expr = index_expression(from, to); 2658f08c3bdfSopenharmony_ci *tail = expr; 2659f08c3bdfSopenharmony_ci tail = &expr->idx_expression; 2660f08c3bdfSopenharmony_ci token = expect(token, ']', "at end of initializer index"); 2661f08c3bdfSopenharmony_ci if (nested) 2662f08c3bdfSopenharmony_ci expect_equal = 1; 2663f08c3bdfSopenharmony_ci } else { 2664f08c3bdfSopenharmony_ci break; 2665f08c3bdfSopenharmony_ci } 2666f08c3bdfSopenharmony_ci } 2667f08c3bdfSopenharmony_ci if (nested && !expect_equal) { 2668f08c3bdfSopenharmony_ci if (!match_op(token, '=')) 2669f08c3bdfSopenharmony_ci warning(token->pos, "obsolete array initializer, use C99 syntax"); 2670f08c3bdfSopenharmony_ci else 2671f08c3bdfSopenharmony_ci expect_equal = 1; 2672f08c3bdfSopenharmony_ci } 2673f08c3bdfSopenharmony_ci if (expect_equal) 2674f08c3bdfSopenharmony_ci token = expect(token, '=', "at end of initializer index"); 2675f08c3bdfSopenharmony_ci 2676f08c3bdfSopenharmony_ci token = initializer(tail, token); 2677f08c3bdfSopenharmony_ci if (!*tail) 2678f08c3bdfSopenharmony_ci *ep = NULL; 2679f08c3bdfSopenharmony_ci return token; 2680f08c3bdfSopenharmony_ci} 2681f08c3bdfSopenharmony_ci 2682f08c3bdfSopenharmony_cistatic struct token *initializer_list(struct expression_list **list, struct token *token) 2683f08c3bdfSopenharmony_ci{ 2684f08c3bdfSopenharmony_ci struct expression *expr; 2685f08c3bdfSopenharmony_ci 2686f08c3bdfSopenharmony_ci for (;;) { 2687f08c3bdfSopenharmony_ci token = single_initializer(&expr, token); 2688f08c3bdfSopenharmony_ci if (!expr) 2689f08c3bdfSopenharmony_ci break; 2690f08c3bdfSopenharmony_ci add_expression(list, expr); 2691f08c3bdfSopenharmony_ci if (!match_op(token, ',')) 2692f08c3bdfSopenharmony_ci break; 2693f08c3bdfSopenharmony_ci token = token->next; 2694f08c3bdfSopenharmony_ci } 2695f08c3bdfSopenharmony_ci return token; 2696f08c3bdfSopenharmony_ci} 2697f08c3bdfSopenharmony_ci 2698f08c3bdfSopenharmony_cistruct token *initializer(struct expression **tree, struct token *token) 2699f08c3bdfSopenharmony_ci{ 2700f08c3bdfSopenharmony_ci if (match_op(token, '{')) { 2701f08c3bdfSopenharmony_ci struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER); 2702f08c3bdfSopenharmony_ci *tree = expr; 2703f08c3bdfSopenharmony_ci if (!Wuniversal_initializer) { 2704f08c3bdfSopenharmony_ci struct token *next = token->next; 2705f08c3bdfSopenharmony_ci // '{ 0 }' is equivalent to '{ }' except for some 2706f08c3bdfSopenharmony_ci // warnings, like using 0 to initialize a null-pointer. 2707f08c3bdfSopenharmony_ci if (match_token_zero(next)) { 2708f08c3bdfSopenharmony_ci if (match_op(next->next, '}')) 2709f08c3bdfSopenharmony_ci expr->zero_init = 1; 2710f08c3bdfSopenharmony_ci } 2711f08c3bdfSopenharmony_ci } 2712f08c3bdfSopenharmony_ci 2713f08c3bdfSopenharmony_ci token = initializer_list(&expr->expr_list, token->next); 2714f08c3bdfSopenharmony_ci return expect(token, '}', "at end of initializer"); 2715f08c3bdfSopenharmony_ci } 2716f08c3bdfSopenharmony_ci return assignment_expression(token, tree); 2717f08c3bdfSopenharmony_ci} 2718f08c3bdfSopenharmony_ci 2719f08c3bdfSopenharmony_cistatic void declare_argument(struct symbol *sym, struct symbol *fn) 2720f08c3bdfSopenharmony_ci{ 2721f08c3bdfSopenharmony_ci if (!sym->ident) { 2722f08c3bdfSopenharmony_ci sparse_error(sym->pos, "no identifier for function argument"); 2723f08c3bdfSopenharmony_ci return; 2724f08c3bdfSopenharmony_ci } 2725f08c3bdfSopenharmony_ci if (sym->ctype.base_type == &incomplete_ctype) { 2726f08c3bdfSopenharmony_ci sym->ctype.base_type = &int_ctype; 2727f08c3bdfSopenharmony_ci 2728f08c3bdfSopenharmony_ci if (Wimplicit_int) { 2729f08c3bdfSopenharmony_ci sparse_error(sym->pos, "missing type declaration for parameter '%s'", 2730f08c3bdfSopenharmony_ci show_ident(sym->ident)); 2731f08c3bdfSopenharmony_ci } 2732f08c3bdfSopenharmony_ci } 2733f08c3bdfSopenharmony_ci bind_symbol(sym, sym->ident, NS_SYMBOL); 2734f08c3bdfSopenharmony_ci} 2735f08c3bdfSopenharmony_ci 2736f08c3bdfSopenharmony_cistatic struct token *parse_function_body(struct token *token, struct symbol *decl, 2737f08c3bdfSopenharmony_ci struct symbol_list **list) 2738f08c3bdfSopenharmony_ci{ 2739f08c3bdfSopenharmony_ci struct symbol_list **old_symbol_list; 2740f08c3bdfSopenharmony_ci struct symbol *base_type = decl->ctype.base_type; 2741f08c3bdfSopenharmony_ci struct statement *stmt, **p; 2742f08c3bdfSopenharmony_ci struct symbol *prev; 2743f08c3bdfSopenharmony_ci struct symbol *arg; 2744f08c3bdfSopenharmony_ci 2745f08c3bdfSopenharmony_ci old_symbol_list = function_symbol_list; 2746f08c3bdfSopenharmony_ci if (decl->ctype.modifiers & MOD_INLINE) { 2747f08c3bdfSopenharmony_ci function_symbol_list = &decl->inline_symbol_list; 2748f08c3bdfSopenharmony_ci p = &base_type->inline_stmt; 2749f08c3bdfSopenharmony_ci } else { 2750f08c3bdfSopenharmony_ci function_symbol_list = &decl->symbol_list; 2751f08c3bdfSopenharmony_ci p = &base_type->stmt; 2752f08c3bdfSopenharmony_ci } 2753f08c3bdfSopenharmony_ci function_computed_target_list = NULL; 2754f08c3bdfSopenharmony_ci function_computed_goto_list = NULL; 2755f08c3bdfSopenharmony_ci 2756f08c3bdfSopenharmony_ci if ((decl->ctype.modifiers & (MOD_EXTERN|MOD_INLINE)) == MOD_EXTERN) { 2757f08c3bdfSopenharmony_ci if (Wexternal_function_has_definition) 2758f08c3bdfSopenharmony_ci warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident)); 2759f08c3bdfSopenharmony_ci } 2760f08c3bdfSopenharmony_ci if (!(decl->ctype.modifiers & MOD_STATIC)) 2761f08c3bdfSopenharmony_ci decl->ctype.modifiers |= MOD_EXTERN; 2762f08c3bdfSopenharmony_ci 2763f08c3bdfSopenharmony_ci stmt = start_function(decl); 2764f08c3bdfSopenharmony_ci *p = stmt; 2765f08c3bdfSopenharmony_ci 2766f08c3bdfSopenharmony_ci FOR_EACH_PTR (base_type->arguments, arg) { 2767f08c3bdfSopenharmony_ci declare_argument(arg, base_type); 2768f08c3bdfSopenharmony_ci } END_FOR_EACH_PTR(arg); 2769f08c3bdfSopenharmony_ci 2770f08c3bdfSopenharmony_ci token = statement_list(token->next, &stmt->stmts); 2771f08c3bdfSopenharmony_ci end_function(decl); 2772f08c3bdfSopenharmony_ci 2773f08c3bdfSopenharmony_ci if (!(decl->ctype.modifiers & MOD_INLINE)) 2774f08c3bdfSopenharmony_ci add_symbol(list, decl); 2775f08c3bdfSopenharmony_ci check_declaration(decl); 2776f08c3bdfSopenharmony_ci decl->definition = decl; 2777f08c3bdfSopenharmony_ci prev = decl->same_symbol; 2778f08c3bdfSopenharmony_ci if (prev && prev->definition) { 2779f08c3bdfSopenharmony_ci warning(decl->pos, "multiple definitions for function '%s'", 2780f08c3bdfSopenharmony_ci show_ident(decl->ident)); 2781f08c3bdfSopenharmony_ci info(prev->definition->pos, " the previous one is here"); 2782f08c3bdfSopenharmony_ci } else { 2783f08c3bdfSopenharmony_ci while (prev) { 2784f08c3bdfSopenharmony_ci rebind_scope(prev, decl->scope); 2785f08c3bdfSopenharmony_ci prev->definition = decl; 2786f08c3bdfSopenharmony_ci prev = prev->same_symbol; 2787f08c3bdfSopenharmony_ci } 2788f08c3bdfSopenharmony_ci } 2789f08c3bdfSopenharmony_ci function_symbol_list = old_symbol_list; 2790f08c3bdfSopenharmony_ci if (function_computed_goto_list) { 2791f08c3bdfSopenharmony_ci if (!function_computed_target_list) 2792f08c3bdfSopenharmony_ci warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident)); 2793f08c3bdfSopenharmony_ci else { 2794f08c3bdfSopenharmony_ci FOR_EACH_PTR(function_computed_goto_list, stmt) { 2795f08c3bdfSopenharmony_ci stmt->target_list = function_computed_target_list; 2796f08c3bdfSopenharmony_ci } END_FOR_EACH_PTR(stmt); 2797f08c3bdfSopenharmony_ci } 2798f08c3bdfSopenharmony_ci } 2799f08c3bdfSopenharmony_ci return expect(token, '}', "at end of function"); 2800f08c3bdfSopenharmony_ci} 2801f08c3bdfSopenharmony_ci 2802f08c3bdfSopenharmony_cistatic void promote_k_r_types(struct symbol *arg) 2803f08c3bdfSopenharmony_ci{ 2804f08c3bdfSopenharmony_ci struct symbol *base = arg->ctype.base_type; 2805f08c3bdfSopenharmony_ci if (base && base->ctype.base_type == &int_type && base->rank < 0) { 2806f08c3bdfSopenharmony_ci arg->ctype.base_type = &int_ctype; 2807f08c3bdfSopenharmony_ci } 2808f08c3bdfSopenharmony_ci} 2809f08c3bdfSopenharmony_ci 2810f08c3bdfSopenharmony_cistatic void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn) 2811f08c3bdfSopenharmony_ci{ 2812f08c3bdfSopenharmony_ci struct symbol_list *real_args = fn->ctype.base_type->arguments; 2813f08c3bdfSopenharmony_ci struct symbol *arg; 2814f08c3bdfSopenharmony_ci 2815f08c3bdfSopenharmony_ci FOR_EACH_PTR(real_args, arg) { 2816f08c3bdfSopenharmony_ci struct symbol *type; 2817f08c3bdfSopenharmony_ci 2818f08c3bdfSopenharmony_ci /* This is quadratic in the number of arguments. We _really_ don't care */ 2819f08c3bdfSopenharmony_ci FOR_EACH_PTR(argtypes, type) { 2820f08c3bdfSopenharmony_ci if (type->ident == arg->ident) 2821f08c3bdfSopenharmony_ci goto match; 2822f08c3bdfSopenharmony_ci } END_FOR_EACH_PTR(type); 2823f08c3bdfSopenharmony_ci if (Wimplicit_int) { 2824f08c3bdfSopenharmony_ci warning(arg->pos, "missing type declaration for parameter '%s'", 2825f08c3bdfSopenharmony_ci show_ident(arg->ident)); 2826f08c3bdfSopenharmony_ci } 2827f08c3bdfSopenharmony_ci type = alloc_symbol(arg->pos, SYM_NODE); 2828f08c3bdfSopenharmony_ci type->ident = arg->ident; 2829f08c3bdfSopenharmony_ci type->ctype.base_type = &int_ctype; 2830f08c3bdfSopenharmony_cimatch: 2831f08c3bdfSopenharmony_ci type->used = 1; 2832f08c3bdfSopenharmony_ci /* "char" and "short" promote to "int" */ 2833f08c3bdfSopenharmony_ci promote_k_r_types(type); 2834f08c3bdfSopenharmony_ci 2835f08c3bdfSopenharmony_ci arg->ctype = type->ctype; 2836f08c3bdfSopenharmony_ci } END_FOR_EACH_PTR(arg); 2837f08c3bdfSopenharmony_ci 2838f08c3bdfSopenharmony_ci FOR_EACH_PTR(argtypes, arg) { 2839f08c3bdfSopenharmony_ci if (!arg->used) 2840f08c3bdfSopenharmony_ci warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident)); 2841f08c3bdfSopenharmony_ci } END_FOR_EACH_PTR(arg); 2842f08c3bdfSopenharmony_ci 2843f08c3bdfSopenharmony_ci} 2844f08c3bdfSopenharmony_ci 2845f08c3bdfSopenharmony_cistatic struct token *parse_k_r_arguments(struct token *token, struct symbol *decl, 2846f08c3bdfSopenharmony_ci struct symbol_list **list) 2847f08c3bdfSopenharmony_ci{ 2848f08c3bdfSopenharmony_ci struct symbol_list *args = NULL; 2849f08c3bdfSopenharmony_ci 2850f08c3bdfSopenharmony_ci if (Wold_style_definition) 2851f08c3bdfSopenharmony_ci warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident)); 2852f08c3bdfSopenharmony_ci 2853f08c3bdfSopenharmony_ci do { 2854f08c3bdfSopenharmony_ci token = declaration_list(token, &args); 2855f08c3bdfSopenharmony_ci if (!match_op(token, ';')) { 2856f08c3bdfSopenharmony_ci sparse_error(token->pos, "expected ';' at end of parameter declaration"); 2857f08c3bdfSopenharmony_ci break; 2858f08c3bdfSopenharmony_ci } 2859f08c3bdfSopenharmony_ci token = token->next; 2860f08c3bdfSopenharmony_ci } while (lookup_type(token)); 2861f08c3bdfSopenharmony_ci 2862f08c3bdfSopenharmony_ci apply_k_r_types(args, decl); 2863f08c3bdfSopenharmony_ci 2864f08c3bdfSopenharmony_ci if (!match_op(token, '{')) { 2865f08c3bdfSopenharmony_ci sparse_error(token->pos, "expected function body"); 2866f08c3bdfSopenharmony_ci return token; 2867f08c3bdfSopenharmony_ci } 2868f08c3bdfSopenharmony_ci return parse_function_body(token, decl, list); 2869f08c3bdfSopenharmony_ci} 2870f08c3bdfSopenharmony_ci 2871f08c3bdfSopenharmony_cistatic struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list) 2872f08c3bdfSopenharmony_ci{ 2873f08c3bdfSopenharmony_ci struct symbol *anon = alloc_symbol(token->pos, SYM_NODE); 2874f08c3bdfSopenharmony_ci struct symbol *fn = alloc_symbol(token->pos, SYM_FN); 2875f08c3bdfSopenharmony_ci struct statement *stmt; 2876f08c3bdfSopenharmony_ci 2877f08c3bdfSopenharmony_ci anon->ctype.base_type = fn; 2878f08c3bdfSopenharmony_ci stmt = alloc_statement(token->pos, STMT_NONE); 2879f08c3bdfSopenharmony_ci fn->stmt = stmt; 2880f08c3bdfSopenharmony_ci 2881f08c3bdfSopenharmony_ci token = parse_asm_statement(token, stmt); 2882f08c3bdfSopenharmony_ci 2883f08c3bdfSopenharmony_ci // FIXME: add_symbol(list, anon); 2884f08c3bdfSopenharmony_ci return token; 2885f08c3bdfSopenharmony_ci} 2886f08c3bdfSopenharmony_ci 2887f08c3bdfSopenharmony_cistruct token *external_declaration(struct token *token, struct symbol_list **list, 2888f08c3bdfSopenharmony_ci validate_decl_t validate_decl) 2889f08c3bdfSopenharmony_ci{ 2890f08c3bdfSopenharmony_ci struct ident *ident = NULL; 2891f08c3bdfSopenharmony_ci struct symbol *decl; 2892f08c3bdfSopenharmony_ci struct decl_state ctx = { .ident = &ident }; 2893f08c3bdfSopenharmony_ci struct ctype saved; 2894f08c3bdfSopenharmony_ci struct symbol *base_type; 2895f08c3bdfSopenharmony_ci unsigned long mod; 2896f08c3bdfSopenharmony_ci int is_typedef; 2897f08c3bdfSopenharmony_ci 2898f08c3bdfSopenharmony_ci /* Top-level inline asm or static assertion? */ 2899f08c3bdfSopenharmony_ci if (token_type(token) == TOKEN_IDENT) { 2900f08c3bdfSopenharmony_ci struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD); 2901f08c3bdfSopenharmony_ci if (s && s->op->toplevel) 2902f08c3bdfSopenharmony_ci return s->op->toplevel(token, list); 2903f08c3bdfSopenharmony_ci } 2904f08c3bdfSopenharmony_ci 2905f08c3bdfSopenharmony_ci /* Parse declaration-specifiers, if any */ 2906f08c3bdfSopenharmony_ci token = declaration_specifiers(token, &ctx); 2907f08c3bdfSopenharmony_ci mod = decl_modifiers(&ctx); 2908f08c3bdfSopenharmony_ci decl = alloc_symbol(token->pos, SYM_NODE); 2909f08c3bdfSopenharmony_ci /* Just a type declaration? */ 2910f08c3bdfSopenharmony_ci if (match_op(token, ';')) { 2911f08c3bdfSopenharmony_ci apply_modifiers(token->pos, &ctx); 2912f08c3bdfSopenharmony_ci return token->next; 2913f08c3bdfSopenharmony_ci } 2914f08c3bdfSopenharmony_ci 2915f08c3bdfSopenharmony_ci saved = ctx.ctype; 2916f08c3bdfSopenharmony_ci token = declarator(token, &ctx); 2917f08c3bdfSopenharmony_ci token = handle_asm_name(token, &ctx); 2918f08c3bdfSopenharmony_ci token = handle_attributes(token, &ctx); 2919f08c3bdfSopenharmony_ci apply_modifiers(token->pos, &ctx); 2920f08c3bdfSopenharmony_ci 2921f08c3bdfSopenharmony_ci decl->ctype = ctx.ctype; 2922f08c3bdfSopenharmony_ci decl->ctype.modifiers |= mod; 2923f08c3bdfSopenharmony_ci decl->endpos = token->pos; 2924f08c3bdfSopenharmony_ci 2925f08c3bdfSopenharmony_ci /* Just a type declaration? */ 2926f08c3bdfSopenharmony_ci if (!ident) { 2927f08c3bdfSopenharmony_ci warning(token->pos, "missing identifier in declaration"); 2928f08c3bdfSopenharmony_ci return expect(token, ';', "at the end of type declaration"); 2929f08c3bdfSopenharmony_ci } 2930f08c3bdfSopenharmony_ci 2931f08c3bdfSopenharmony_ci /* type define declaration? */ 2932f08c3bdfSopenharmony_ci is_typedef = ctx.storage_class == MOD_USERTYPE; 2933f08c3bdfSopenharmony_ci 2934f08c3bdfSopenharmony_ci /* Typedefs don't have meaningful storage */ 2935f08c3bdfSopenharmony_ci if (is_typedef) 2936f08c3bdfSopenharmony_ci decl->ctype.modifiers |= MOD_USERTYPE; 2937f08c3bdfSopenharmony_ci 2938f08c3bdfSopenharmony_ci bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL); 2939f08c3bdfSopenharmony_ci 2940f08c3bdfSopenharmony_ci base_type = decl->ctype.base_type; 2941f08c3bdfSopenharmony_ci 2942f08c3bdfSopenharmony_ci if (is_typedef) { 2943f08c3bdfSopenharmony_ci if (base_type && !base_type->ident) { 2944f08c3bdfSopenharmony_ci switch (base_type->type) { 2945f08c3bdfSopenharmony_ci case SYM_STRUCT: 2946f08c3bdfSopenharmony_ci case SYM_UNION: 2947f08c3bdfSopenharmony_ci case SYM_ENUM: 2948f08c3bdfSopenharmony_ci case SYM_RESTRICT: 2949f08c3bdfSopenharmony_ci base_type->ident = ident; 2950f08c3bdfSopenharmony_ci break; 2951f08c3bdfSopenharmony_ci default: 2952f08c3bdfSopenharmony_ci break; 2953f08c3bdfSopenharmony_ci } 2954f08c3bdfSopenharmony_ci } 2955f08c3bdfSopenharmony_ci } else if (base_type && base_type->type == SYM_FN) { 2956f08c3bdfSopenharmony_ci if (base_type->ctype.base_type == &autotype_ctype) { 2957f08c3bdfSopenharmony_ci sparse_error(decl->pos, "'%s()' has __auto_type return type", 2958f08c3bdfSopenharmony_ci show_ident(decl->ident)); 2959f08c3bdfSopenharmony_ci base_type->ctype.base_type = &int_ctype; 2960f08c3bdfSopenharmony_ci } 2961f08c3bdfSopenharmony_ci if (base_type->ctype.base_type == &incomplete_ctype) { 2962f08c3bdfSopenharmony_ci warning(decl->pos, "'%s()' has implicit return type", 2963f08c3bdfSopenharmony_ci show_ident(decl->ident)); 2964f08c3bdfSopenharmony_ci base_type->ctype.base_type = &int_ctype; 2965f08c3bdfSopenharmony_ci } 2966f08c3bdfSopenharmony_ci /* apply attributes placed after the declarator */ 2967f08c3bdfSopenharmony_ci decl->ctype.modifiers |= ctx.f_modifiers; 2968f08c3bdfSopenharmony_ci 2969f08c3bdfSopenharmony_ci /* K&R argument declaration? */ 2970f08c3bdfSopenharmony_ci if (lookup_type(token)) 2971f08c3bdfSopenharmony_ci return parse_k_r_arguments(token, decl, list); 2972f08c3bdfSopenharmony_ci if (match_op(token, '{')) 2973f08c3bdfSopenharmony_ci return parse_function_body(token, decl, list); 2974f08c3bdfSopenharmony_ci 2975f08c3bdfSopenharmony_ci if (!(decl->ctype.modifiers & MOD_STATIC)) 2976f08c3bdfSopenharmony_ci decl->ctype.modifiers |= MOD_EXTERN; 2977f08c3bdfSopenharmony_ci } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) { 2978f08c3bdfSopenharmony_ci sparse_error(token->pos, "void declaration"); 2979f08c3bdfSopenharmony_ci } 2980f08c3bdfSopenharmony_ci if (base_type == &incomplete_ctype) { 2981f08c3bdfSopenharmony_ci warning(decl->pos, "'%s' has implicit type", show_ident(decl->ident)); 2982f08c3bdfSopenharmony_ci decl->ctype.base_type = &int_ctype;; 2983f08c3bdfSopenharmony_ci } 2984f08c3bdfSopenharmony_ci 2985f08c3bdfSopenharmony_ci for (;;) { 2986f08c3bdfSopenharmony_ci if (!is_typedef && match_op(token, '=')) { 2987f08c3bdfSopenharmony_ci struct token *next = token->next; 2988f08c3bdfSopenharmony_ci token = initializer(&decl->initializer, next); 2989f08c3bdfSopenharmony_ci if (token == next) 2990f08c3bdfSopenharmony_ci sparse_error(token->pos, "expression expected before '%s'", show_token(token)); 2991f08c3bdfSopenharmony_ci } 2992f08c3bdfSopenharmony_ci if (!is_typedef) { 2993f08c3bdfSopenharmony_ci if (validate_decl) 2994f08c3bdfSopenharmony_ci validate_decl(decl); 2995f08c3bdfSopenharmony_ci 2996f08c3bdfSopenharmony_ci if (decl->initializer && decl->ctype.modifiers & MOD_EXTERN) { 2997f08c3bdfSopenharmony_ci warning(decl->pos, "symbol with external linkage has initializer"); 2998f08c3bdfSopenharmony_ci decl->ctype.modifiers &= ~MOD_EXTERN; 2999f08c3bdfSopenharmony_ci } 3000f08c3bdfSopenharmony_ci 3001f08c3bdfSopenharmony_ci if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) { 3002f08c3bdfSopenharmony_ci add_symbol(list, decl); 3003f08c3bdfSopenharmony_ci fn_local_symbol(decl); 3004f08c3bdfSopenharmony_ci } 3005f08c3bdfSopenharmony_ci } 3006f08c3bdfSopenharmony_ci check_declaration(decl); 3007f08c3bdfSopenharmony_ci if (decl->same_symbol) { 3008f08c3bdfSopenharmony_ci decl->definition = decl->same_symbol->definition; 3009f08c3bdfSopenharmony_ci decl->op = decl->same_symbol->op; 3010f08c3bdfSopenharmony_ci if (is_typedef) { 3011f08c3bdfSopenharmony_ci // TODO: handle -std=c89 --pedantic 3012f08c3bdfSopenharmony_ci check_duplicates(decl); 3013f08c3bdfSopenharmony_ci } 3014f08c3bdfSopenharmony_ci } 3015f08c3bdfSopenharmony_ci 3016f08c3bdfSopenharmony_ci if (ctx.autotype) { 3017f08c3bdfSopenharmony_ci const char *msg = NULL; 3018f08c3bdfSopenharmony_ci if (decl->ctype.base_type != &autotype_ctype) 3019f08c3bdfSopenharmony_ci msg = "on non-identifier"; 3020f08c3bdfSopenharmony_ci else if (match_op(token, ',')) 3021f08c3bdfSopenharmony_ci msg = "on declaration list"; 3022f08c3bdfSopenharmony_ci else if (!decl->initializer) 3023f08c3bdfSopenharmony_ci msg = "without initializer"; 3024f08c3bdfSopenharmony_ci else if (decl->initializer->type == EXPR_SYMBOL && 3025f08c3bdfSopenharmony_ci decl->initializer->symbol == decl) 3026f08c3bdfSopenharmony_ci msg = "on self-init var"; 3027f08c3bdfSopenharmony_ci if (msg) { 3028f08c3bdfSopenharmony_ci sparse_error(decl->pos, "__auto_type %s", msg); 3029f08c3bdfSopenharmony_ci decl->ctype.base_type = &bad_ctype; 3030f08c3bdfSopenharmony_ci } 3031f08c3bdfSopenharmony_ci } 3032f08c3bdfSopenharmony_ci 3033f08c3bdfSopenharmony_ci if (!match_op(token, ',')) 3034f08c3bdfSopenharmony_ci break; 3035f08c3bdfSopenharmony_ci 3036f08c3bdfSopenharmony_ci token = token->next; 3037f08c3bdfSopenharmony_ci ident = NULL; 3038f08c3bdfSopenharmony_ci decl = alloc_symbol(token->pos, SYM_NODE); 3039f08c3bdfSopenharmony_ci ctx.ctype = saved; 3040f08c3bdfSopenharmony_ci token = handle_attributes(token, &ctx); 3041f08c3bdfSopenharmony_ci token = declarator(token, &ctx); 3042f08c3bdfSopenharmony_ci token = handle_asm_name(token, &ctx); 3043f08c3bdfSopenharmony_ci token = handle_attributes(token, &ctx); 3044f08c3bdfSopenharmony_ci apply_modifiers(token->pos, &ctx); 3045f08c3bdfSopenharmony_ci decl->ctype = ctx.ctype; 3046f08c3bdfSopenharmony_ci decl->ctype.modifiers |= mod; 3047f08c3bdfSopenharmony_ci decl->endpos = token->pos; 3048f08c3bdfSopenharmony_ci if (!ident) { 3049f08c3bdfSopenharmony_ci sparse_error(token->pos, "expected identifier name in type definition"); 3050f08c3bdfSopenharmony_ci return token; 3051f08c3bdfSopenharmony_ci } 3052f08c3bdfSopenharmony_ci 3053f08c3bdfSopenharmony_ci if (is_typedef) 3054f08c3bdfSopenharmony_ci decl->ctype.modifiers |= MOD_USERTYPE; 3055f08c3bdfSopenharmony_ci 3056f08c3bdfSopenharmony_ci bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL); 3057f08c3bdfSopenharmony_ci 3058f08c3bdfSopenharmony_ci /* Function declarations are automatically extern unless specifically static */ 3059f08c3bdfSopenharmony_ci base_type = decl->ctype.base_type; 3060f08c3bdfSopenharmony_ci if (!is_typedef && base_type && base_type->type == SYM_FN) { 3061f08c3bdfSopenharmony_ci if (!(decl->ctype.modifiers & MOD_STATIC)) 3062f08c3bdfSopenharmony_ci decl->ctype.modifiers |= MOD_EXTERN; 3063f08c3bdfSopenharmony_ci } 3064f08c3bdfSopenharmony_ci } 3065f08c3bdfSopenharmony_ci return expect(token, ';', "at end of declaration"); 3066f08c3bdfSopenharmony_ci} 3067