def update_scalar_type_initializer(desig_expr, default_values): assert len(default_values) == 1 expr, desig = exp(desig_expr), designation(desig_expr, 0) if desig != 0: logger.warning('Excess Elements in initializer ...') else: default_values[0] = cast(expr, c_type(default_values[0]))
def update_composite_type_initializer(desig_expr, default_values): desig = designation(desig_expr) if desig > default_values: logger.warning('Excess elements in initializer ...') else: assert len(default_values[desig]) == 1 default_values[desig][0] = cast(exp(desig_expr), c_type(default_values[desig][0]))
def parse_identifier_designated_expr(desig_expr, default_values): return parse_designated_expr( OffsetDesignatedExpression( offset(c_type(default_values), designation(desig_expr)), # get offset ... exp(desig_expr), loc(desig_expr)), default_values)
def parse_range_designated_expr(desig_expr, default_values): first, last = designation(desig_expr) exhaust( imap( parse_designated_expr, imap(OffsetDesignatedExpression, xrange(first, last + 1), repeat(exp(desig_expr)), repeat(loc(desig_expr))), repeat(default_values)))
def parse_range_designated_expr(desig_expr, default_values): first, last = designation(desig_expr) exhaust( imap( parse_designated_expr, imap(OffsetDesignatedExpression, xrange(first, last + 1), repeat(exp(desig_expr)), repeat(loc(desig_expr))), repeat(default_values) ) )
def parse_identifier_designated_expr(desig_expr, default_values): return parse_designated_expr( OffsetDesignatedExpression( offset(c_type(default_values), designation(desig_expr)), # get offset ... exp(desig_expr), loc(desig_expr) ), default_values )
def parse_designated_union_expr(desig_expr, default_values): # care must be taken when dealing with union initializer, they may only set a single expression ... ctype, desig, expr = c_type(default_values), designation(desig_expr), exp(desig_expr) if isinstance(expr, DesignatedExpression): parse_designated_expr(expr, default_values) elif isinstance(expr, Initializer): set_default_initializer(expr, default_values) else: default_values[0] = expr
def parse_default_offset_designated_expr(desig_expr, default_values): ctype, desig, expr = c_type(default_values), designation(desig_expr), exp(desig_expr) if isinstance(expr, DesignatedExpression) and error_if_not_type(ctype, (StructType, ArrayType)): parse_designated_expr(expr, default_values[desig]) elif isinstance(expr, Initializer): default_values = default_values[desig] if isinstance(ctype, (StructType, ArrayType)) else default_values set_default_initializer(expr, default_values) else: update_default_value(desig_expr, default_values)
def parse_designated_union_expr(desig_expr, default_values): # care must be taken when dealing with union initializer, they may only set a single expression ... ctype, desig, expr = c_type(default_values), designation(desig_expr), exp( desig_expr) if isinstance(expr, DesignatedExpression): parse_designated_expr(expr, default_values) elif isinstance(expr, Initializer): set_default_initializer(expr, default_values) else: default_values[0] = expr
def parse_default_offset_designated_expr(desig_expr, default_values): ctype, desig, expr = c_type(default_values), designation(desig_expr), exp( desig_expr) if isinstance(expr, DesignatedExpression) and error_if_not_type( ctype, (StructType, ArrayType)): parse_designated_expr(expr, default_values[desig]) elif isinstance(expr, Initializer): default_values = default_values[desig] if isinstance( ctype, (StructType, ArrayType)) else default_values set_default_initializer(expr, default_values) else: update_default_value(desig_expr, default_values)
def update_default_value(desig_expr, default_values): ctype, desig, expr = c_type(default_values), designation(desig_expr), exp(desig_expr) if desig >= len(default_values): logger.warning( '{0} Excess element {1} {2} in initializer, it will be ignored ... '.format( loc(desig_expr), desig, expr )) else: _ = (not safe_type_coercion(c_type(expr), c_type(default_values[desig]))) and raise_error( '{l} Unable to coerce from {f} to {t}'.format(l=loc(expr), f=c_type(expr), t=c_type(default_values[desig])) ) update_func = update_composite_type_initializer \ if isinstance(ctype, (StructType, ArrayType)) else update_scalar_type_initializer update_func(desig_expr, default_values)
def update_default_value(desig_expr, default_values): ctype, desig, expr = c_type(default_values), designation(desig_expr), exp( desig_expr) if desig >= len(default_values): logger.warning( '{0} Excess element {1} {2} in initializer, it will be ignored ... ' .format(loc(desig_expr), desig, expr)) else: _ = (not safe_type_coercion(c_type(expr), c_type( default_values[desig]))) and raise_error( '{l} Unable to coerce from {f} to {t}'.format( l=loc(expr), f=c_type(expr), t=c_type(default_values[desig]))) update_func = update_composite_type_initializer \ if isinstance(ctype, (StructType, ArrayType)) else update_scalar_type_initializer update_func(desig_expr, default_values)
def initializer_desig_exprs(initializer, default_values): previous_desig_offset_mag = -1 for expr_or_desig_expr in initializer.itervalues(): if not isinstance(expr_or_desig_expr, DesignatedExpression): # assign default designation if none present ... expr_or_desig_expr = DefaultOffsetDesignationExpression( previous_desig_offset_mag + 1, # use previous designation ... expr_or_desig_expr, loc(expr_or_desig_expr) ) elif isinstance(expr_or_desig_expr, IdentifierDesignatedExpression): # if designation is an identifier expr_or_desig_expr = OffsetDesignatedExpression( offset(c_type(default_values), designation(expr_or_desig_expr)), # get offset ... exp(expr_or_desig_expr), loc(expr_or_desig_expr) ) expand_defaults(expr_or_desig_expr, default_values) # expand defaults for incomplete array types yield expr_or_desig_expr previous_desig_offset_mag = max_designation_mag(expr_or_desig_expr) # record designation in case we need new 1
def initializer_desig_exprs(initializer, default_values): previous_desig_offset_mag = -1 for expr_or_desig_expr in initializer.itervalues(): if not isinstance(expr_or_desig_expr, DesignatedExpression ): # assign default designation if none present ... expr_or_desig_expr = DefaultOffsetDesignationExpression( previous_desig_offset_mag + 1, # use previous designation ... expr_or_desig_expr, loc(expr_or_desig_expr)) elif isinstance(expr_or_desig_expr, IdentifierDesignatedExpression ): # if designation is an identifier expr_or_desig_expr = OffsetDesignatedExpression( offset(c_type(default_values), designation(expr_or_desig_expr)), # get offset ... exp(expr_or_desig_expr), loc(expr_or_desig_expr)) expand_defaults( expr_or_desig_expr, default_values) # expand defaults for incomplete array types yield expr_or_desig_expr previous_desig_offset_mag = max_designation_mag( expr_or_desig_expr) # record designation in case we need new 1
def max_designation_mag(desig_expr): return rules(max_designation_mag)[type(desig_expr)]( designation(desig_expr))
def max_designation_mag(desig_expr): return rules(max_designation_mag)[type(desig_expr)](designation(desig_expr))