Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def bool_param(name):
return decl(name, node.TypeDecl(name, [], node.IdentifierType(["bool"])))
def p_type_specifier_no_typeid(self, p):
""" type_specifier_no_typeid : VOID
| _BOOL
| CHAR
| SHORT
| INT
| LONG
| FLOAT
| DOUBLE
| _COMPLEX
| SIGNED
| UNSIGNED
| __INT128
"""
p[0] = c_ast.IdentifierType([p[1]], coord=self._token_coord(p, 1))
def _get_type_and_quals(self, typenode, name=None, partial_length_ok=False):
# first, dereference typedefs, if we have it already parsed, we're good
if (isinstance(typenode, pycparser.c_ast.TypeDecl) and
isinstance(typenode.type, pycparser.c_ast.IdentifierType) and
len(typenode.type.names) == 1 and
('typedef ' + typenode.type.names[0]) in self._declarations):
tp, quals = self._declarations['typedef ' + typenode.type.names[0]]
quals |= self._extract_quals(typenode)
return tp, quals
#
if isinstance(typenode, pycparser.c_ast.ArrayDecl):
# array type
if typenode.dim is None:
length = None
else:
length = self._parse_constant(
typenode.dim, partial_length_ok=partial_length_ok)
tp, quals = self._get_type_and_quals(typenode.type,
partial_length_ok=partial_length_ok)
return model.ArrayType(tp, length), quals
return decl
if not typename:
# Functions default to returning int
#
if not isinstance(decl.type, c_ast.FuncDecl):
self._parse_error(
"Missing type in declaration", decl.coord)
type.type = c_ast.IdentifierType(
['int'],
coord=decl.coord)
else:
# At this point, we know that typename is a list of IdentifierType
# nodes. Concatenate all the names into a single list.
#
type.type = c_ast.IdentifierType(
[name for id in typename for name in id.names],
coord=typename[0].coord)
return decl
for decl in iterator:
current_decl = decl
if isinstance(decl, pycparser.c_ast.Decl):
self._parse_decl(decl)
elif isinstance(decl, pycparser.c_ast.Typedef):
if not decl.name:
raise CDefError("typedef does not declare any name",
decl)
quals = 0
if (isinstance(decl.type.type, pycparser.c_ast.IdentifierType) and
decl.type.type.names[-1].startswith('__dotdotdot')):
realtype = self._get_unknown_type(decl)
elif (isinstance(decl.type, pycparser.c_ast.PtrDecl) and
isinstance(decl.type.type, pycparser.c_ast.TypeDecl) and
isinstance(decl.type.type.type,
pycparser.c_ast.IdentifierType) and
decl.type.type.type.names[-1].startswith('__dotdotdot')):
realtype = self._get_unknown_ptr_type(decl)
else:
realtype, quals = self._get_type_and_quals(
decl.type, name=decl.name, partial_length_ok=True)
self._declare('typedef ' + decl.name, realtype, quals=quals)
elif decl.__class__.__name__ == 'Pragma':
pass # skip pragma, only in pycparser 2.15
else:
raise CDefError("unexpected <%s>: this construct is valid "
"C but not valid in cdef()" %
decl.__class__.__name__, decl)
except CDefError as e:
if len(e.args) == 1:
e.args = e.args + (current_decl,)
raise
def p_parameter_declaration_2(self, p):
""" parameter_declaration : declaration_specifiers abstract_declarator_opt
"""
spec = p[1]
if not spec['type']:
spec['type'] = [c_ast.IdentifierType(['int'],
coord=self._token_coord(p, 1))]
# Parameters can have the same names as typedefs. The trouble is that
# the parameter's name gets grouped into declaration_specifiers, making
# it look like an old-style declaration; compensate.
#
if len(spec['type']) > 1 and len(spec['type'][-1].names) == 1 and \
self._is_type_in_scope(spec['type'][-1].names[0]):
decl = self._build_declarations(
spec=spec,
decls=[dict(decl=p[2], init=None)])[0]
# This truly is an old-style parameter declaration
#
else:
decl = c_ast.Typename(
def reorganize_decl(n):
"""
Turns nested declarations into anonymous declarations.
"""
if isinstance(n, (c_ast.Union, c_ast.Struct)):
name = n.name
if not name:
name = anonymous(n)
if n.decls:
generate_decl(n, '', name)
return c_ast.IdentifierType(names=[ name ])
for name, child in n.children():
new_child = reorganize_decl(child)
if new_child is not child:
if "[" in name:
field, _, num = name[:-1].partition("[")
getattr(n, field)[int(num)] = new_child
else:
setattr(n, name, new_child)
return n
continue
if isinstance(type1, PtrDecl) and isinstance(type2, PtrDecl):
type1 = type1.type
type2 = type2.type
continue
if isinstance(type1, TypeDecl) and isinstance(type2, TypeDecl):
sub1 = type1.type
sub2 = type2.type
if isinstance(sub1, c_ast.Struct) and isinstance(sub2, c_ast.Struct):
return sub1.name == sub2.name
if isinstance(sub1, c_ast.Union) and isinstance(sub2, c_ast.Union):
return sub1.name == sub2.name
if (
allow_similar
and isinstance(sub1, (IdentifierType, c_ast.Enum))
and isinstance(sub2, (IdentifierType, c_ast.Enum))
):
# All int-ish types are similar (except void, but whatever)
return True
if isinstance(sub1, c_ast.Enum) and isinstance(sub2, c_ast.Enum):
return sub1.name == sub2.name
if isinstance(sub1, IdentifierType) and isinstance(sub2, IdentifierType):
return sorted(sub1.names) == sorted(sub2.names)
return False
def make_extra_decl(name, t):
idtype = c_ast.IdentifierType([t])
td = c_ast.TypeDecl(name, [], idtype)
return c_ast.Decl(
name,
[], # quals
[], # storage
[], # funcspec
td, # type
None, # init
None, # bitsize
)