Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_scalar_children(self):
b1 = c_ast.BinaryOp(
op='+',
left=c_ast.Constant(type='int', value='6'),
right=c_ast.ID(name='joe'))
cv = self.ConstantVisitor()
cv.visit(b1)
self.assertEqual(cv.values, ['6'])
b2 = c_ast.BinaryOp(
op='*',
left=c_ast.Constant(type='int', value='111'),
right=b1)
b3 = c_ast.BinaryOp(
op='^',
left=b2,
right=b1)
retvis = ReturnVisitor()
retvis.visit(prefix)
for b in prefix.block_items:
if isinstance(b, c_ast.Decl):
id = b.name
vid = id_map[id]
b.init = c_ast.ArrayRef(c_ast.ID('in_vars'),
c_ast.Constant('int', str(vid)))
decls.append(b)
for id in sorted(rev_id_map.keys()):
varname = rev_id_map[id]
arr = c_ast.ArrayRef(c_ast.ID('out_vars'),
c_ast.Constant('int', str(id)))
var = c_ast.ID(varname)
copy_out.append(c_ast.Assignment("=", arr, var))
prefix.block_items += copy_out
prefix.block_items.append(c_ast.Return(c_ast.Constant('int', str('1'))))
ofile.write("int prefix(word_t in_vars[%d], word_t out_vars[%d]) {\n" % (nids, nids))
ofile.write(cgen.visit(prefix))
ofile.write("}\n\n")
ofile.write("int guard(word_t in_vars[%d]) {\n" % nids)
guard_body = c_ast.Compound(copy.copy(decls))
guard_body.block_items.append(c_ast.Return(loop.cond))
init_exprs.append(
c_ast.NamedInitializer(
[c_ast.ID(init_name)],
c_ast.Constant('string', literal)
)
)
# assign types
type_annotations = {
name: t for name,t in self.values.iteritems()
if isinstance(t, Annotation)
}
types, arraydecl = self.struct.annotations.get_types(decl, type_annotations)
type_inits = [c_ast.NamedInitializer(
[c_ast.ID(ttype)],
c_ast.ID(t)
) for ttype, t in types.iteritems()]
fi = getframeinfo(currentframe())
init_exprs.append(c_ast.NamedInitializer(
[c_ast.ID('type')],
c_ast.InitList(type_inits, Coord(fi.filename, fi.lineno))
))
struct = annotated_struct.struct
# calculate struct offset
init_exprs.append(c_ast.NamedInitializer(
[c_ast.ID('offset')],
self.offsetof(struct.name, prop_name)
))
if 'nullable' in self.values and self.values['nullable']:
self.extra_decls[AnnotatedProperty.NULLABLE_NAME(decl.name)] = 'bool'
init_exprs.append(c_ast.NamedInitializer(
[c_ast.ID('null_offset')],
def p_postfix_expression_4(self, p):
""" postfix_expression : postfix_expression PERIOD ID
| postfix_expression PERIOD TYPEID
| postfix_expression ARROW ID
| postfix_expression ARROW TYPEID
"""
field = c_ast.ID(p[3], self._coord(p.lineno(3)))
p[0] = c_ast.StructRef(p[1], p[2], field, p[1].coord)
def is_lvalue(expr: Expression) -> bool:
if isinstance(expr, (ca.ID, ca.StructRef, ca.ArrayRef)):
return True
if isinstance(expr, ca.UnaryOp):
return expr.op == "*"
return False
def p_attribute_1(self, p):
""" attribute : CONST
"""
p[0] = c_ast.ID(name="const", coord=self._coord(p.lineno(1)))
def _is_simple_node(self, n):
""" Returns True for nodes that are "simple" - i.e. nodes that always
have higher precedence than operators.
"""
return isinstance(n,( c_ast.Constant, c_ast.ID, c_ast.ArrayRef,
c_ast.StructRef, c_ast.FuncCall))
def p_offsetof_member_designator(self, p):
""" offsetof_member_designator : identifier
| offsetof_member_designator PERIOD identifier
| offsetof_member_designator LBRACKET expression RBRACKET
"""
if len(p) == 2:
p[0] = p[1]
elif len(p) == 4:
field = c_ast.ID(p[3], self._token_coord(p, 3))
p[0] = c_ast.StructRef(p[1], p[2], field, p[1].coord)
elif len(p) == 5:
p[0] = c_ast.ArrayRef(p[1], p[3], p[1].coord)
else:
raise NotImplementedError("Unexpected parsing state. len(p): %u" % len(p))
assertion_expr = c_ast.Constant('int', '1')
for b in assertion.block_items:
if isinstance(b, c_ast.FuncCall) and b.name.name == "assert":
assertion_expr = b.args.children()[0][1]
assertion_block += [c_ast.Return(assertion_expr)]
assertion.block_items = assertion_block
f.write("int assertion(word_t in_vars[NARGS]) {\n")
f.write(assertion)
f.write("}\n\n")
for id in sorted(rev_id_map.keys()):
varname = rev_id_map[id]
arr = c_ast.ArrayRef(c_ast.ID('out_vars'),
c_ast.Constant('int', str(id)))
var = c_ast.ID(varname)
copy_out.append(c_ast.Assignment("=", arr, var))
copy_out.append(c_ast.Return(c_ast.Constant('int', str('1'))))
prefix.block_items += copy_out
f.write("int prefix(word_t in_vars[NARGS], word_t out_vars[NARGS]) {\n")
f.write(prefix)
f.write("}\n\n")
f.write("int guard(word_t in_vars[NARGS]) {\n")
guard_body = c_ast.Compound(copy.copy(decls))
guard_body.block_items.append(c_ast.Return(loop.cond))
) for ttype, t in types.iteritems()]
fi = getframeinfo(currentframe())
init_exprs.append(c_ast.NamedInitializer(
[c_ast.ID('type')],
c_ast.InitList(type_inits, Coord(fi.filename, fi.lineno))
))
struct = annotated_struct.struct
# calculate struct offset
init_exprs.append(c_ast.NamedInitializer(
[c_ast.ID('offset')],
self.offsetof(struct.name, prop_name)
))
if 'nullable' in self.values and self.values['nullable']:
self.extra_decls[AnnotatedProperty.NULLABLE_NAME(decl.name)] = 'bool'
init_exprs.append(c_ast.NamedInitializer(
[c_ast.ID('null_offset')],
self.offsetof(struct.name, AnnotatedProperty.NULLABLE_NAME(prop_name))
))
if arraydecl:
# static array
init_exprs.append(c_ast.NamedInitializer(
[c_ast.ID('length')],
arraydecl.dim
))
elif types['json'] == 'json_type_array':
# calculate length offset
len_prop = AnnotatedProperty.LENGTH_NAME(prop_name)
self.extra_decls[len_prop] = 'int'
init_exprs.append(c_ast.NamedInitializer(
[c_ast.ID('length_offset')],
self.offsetof(struct.name, len_prop)
))