def _build_declarations(self, spec, decls, typedef_namespace=False): """ 构建 declarations. Args: spec: 一个 dict: {qual=[], storage=[], type=[], function=[]} 来自 declaration_specifiers decls: dict 列表:[{decl=, init=}],来自 init_declarator_list typedef_namespace: Returns: 构建好的 declarations 列表,元素为 Typedef 或 Decl. """ is_typedef = 'typedef' in spec['storage'] declarations = [] for decl in decls: assert decl['decl'] is not None if is_typedef: declaration = c_ast.Typedef( name=None, quals=spec['qual'], storage=spec['storage'], type=decl['decl'], coord=decl['decl'].coord ) else: declaration = c_ast.Decl( name=None, quals=spec['qual'], storage=spec['storage'], funcspec=spec['function'], type=decl['decl'], init = decl.get('init'), bitsize=None, # TODO: Parser全部测试完成后删除此参数 coord=decl['decl'].coord ) if isinstance(declaration.type, (c_ast.Struct, c_ast.IdentifierType)): fixed_decl = declaration else: fixed_decl = self._fix_decl_name_type(declaration, spec['type']) # 添加到 scope 中,在语法分析器中使用 # if typedef_namespace: if is_typedef: self._add_typedef_name(fixed_decl.name, fixed_decl.coord) else: self._add_identifier(fixed_decl.name, fixed_decl.coord) declarations.append(fixed_decl) return declarations
def p_parameter_declaration_1(self, p): """ parameter_declaration : declaration_specifiers declarator """ spec = p[1] decl = p[2] decl = c_ast.Decl(name=None, quals=spec['qual'], storage=spec['storage'], type=decl, init=None, bitsize=None, coord=decl.coord) typename = spec['type'] or ['int'] p[0] = self._fix_decl_name_type(decl, typename)
def _build_function_definition(self, decl, spec, param_decls, body): """ Builds a function definition. """ declaration = c_ast.Decl(name=None, quals=spec['qual'], storage=spec['storage'], type=decl, init=None, bitsize=None, coord=decl.coord) typename = spec['type'] declaration = self._fix_decl_name_type(declaration, typename) return c_ast.FuncDef(decl=declaration, param_decls=param_decls, body=body, coord=decl.coord)
def p_struct_declaration_1(self, p): """ struct_declaration : specifier_qualifier_list struct_declarator_list SEMI """ spec = p[1] decls = [] for struct_decl in p[2]: decl = c_ast.Decl(name=None, quals=spec['qual'], storage=spec['storage'], type=struct_decl['decl'], init=None, bitsize=struct_decl['bitsize'], coord=struct_decl['decl'].coord) typename = spec['type'] decls.append(self._fix_decl_name_type(decl, typename)) p[0] = decls
def p_decl_body(self, p): """ decl_body : declaration_specifiers init_declarator_list_opt """ spec = p[1] is_typedef = 'typedef' in spec['storage'] decls = [] # p[2] (init_declarator_list_opt) is either a list or None # if p[2] is None: # Then it's a declaration of a struct / enum tag, # without an actual declarator. # type = spec['type'] if len(type) > 1: coord = '?' for t in type: if hasattr(t, 'coord'): coord = t.coord break self._parse_error('Multiple type specifiers with a type tag', coord) decl = c_ast.Decl( name=None, quals=spec['qual'], storage=spec['storage'], type=type[0], init=None, bitsize=None, coord=type[0].coord) decls = [decl] else: for decl, init in p[2] or []: if is_typedef: decl = c_ast.Typedef( name=None, quals=spec['qual'], storage=spec['storage'], type=decl, coord=decl.coord) else: decl = c_ast.Decl( name=None, quals=spec['qual'], storage=spec['storage'], type=decl, init=init, bitsize=None, coord=decl.coord) typename = spec['type'] fixed_decl = self._fix_decl_name_type(decl, typename) # Add the type name defined by typedef to a # symbol table (for usage in the lexer) # if is_typedef: self._add_typedef_type(fixed_decl.name) decls.append(fixed_decl) p[0] = decls