def _resolve(self, visitor): name = self.name try: # is it a type name? return cl.type_names[name].cl_type except KeyError: try: # is it a built-in? return cl.builtins[name].cl_type except KeyError: try: # is it a (non-inlined) constant? return cl.infer_cl_type(visitor.constants[name]) except KeyError: try: # is it an argument? return visitor.explicit_arg_map[name] except KeyError: # it must be a local variable try: value = visitor.local_variables[name] except KeyError: raise InvalidTypeError("Invalid name %s." % name) # let downstream multiple assignment types know we're # resolving this name so it can maintain a stack prev_resolving_name = visitor._resolving_name visitor._resolving_name = name cl_type = visitor._resolve_type(value) visitor._resolving_name = prev_resolving_name return cl_type
def _resolve(self, visitor): value = self.value unresolved_type = value.unresolved_type try: value_type = visitor._resolve_type(unresolved_type) except Error: value_type = None # protocol for attribute lookup try: # if _resolve_Attr is defined, call that _resolve_Attr = value_type._resolve_Attr except AttributeError: try: # otherwise, try to resolve the attribute itself and # get its cl_type _resolve_value = unresolved_type._resolve_value except AttributeError: pass else: value = _resolve_value(visitor) attr = self.attr try: attr_value = getattr(value, attr) except AttributeError: pass else: try: return cl.infer_cl_type(attr_value) except cl.Error: pass else: cl_type = _resolve_Attr(visitor, value, self.attr) if cl_type is not None: return cl_type if value_type is None: raise InvalidTypeError("Attribute '%s' could not be found." % self.attr) else: raise InvalidTypeError( "Attribute '%s' is invalid for object of type %s." % (self.attr, value_type.name))
def _apply_default_args_and_types(self, args): # yields a full sequence of arg, arg_type pairs given args # does not filter out lifted constants or include implicit arguments # provided args for arg in args: arg_type = cl.infer_cl_type(arg) yield arg, arg_type # defaults n_provided = len(args) n_args = len(self.explicit_arg_names) # constants were filtered out in this step n_defaults = n_args - n_provided if n_defaults < 0: raise Error("Too many arguments were specified for %s." % self.name) elif n_defaults > 0: defaults = self.defaults default_types = self.default_types for i in xrange(n_defaults): yield defaults[i], default_types[i]
def _apply_default_args_and_types(self, args): # yields a full sequence of arg, arg_type pairs given args # does not filter out lifted constants or include implicit arguments # provided args for arg in args: arg_type = cl.infer_cl_type(arg) yield arg, arg_type # defaults n_provided = len(args) n_args = len(self.explicit_arg_names ) # constants were filtered out in this step n_defaults = n_args - n_provided if n_defaults < 0: raise Error("Too many arguments were specified for %s." % self.name) elif n_defaults > 0: defaults = self.defaults default_types = self.default_types for i in xrange(n_defaults): yield defaults[i], default_types[i]
def visit_Name(self, n): self._process_expr(n) id = n.id if id in cl.builtins: return id try: constant = self.constants[id] except KeyError: if id in self.all_variables: return id else: if isinstance(n.ctx, _ast.Store): raise CompileTimeError("Cannot assign to constant %s." % id, n) # inline constants if isinstance(constant, basestring): return cl.to_cl_string_literal(constant) cl_type = cl.infer_cl_type(constant) if isinstance(cl_type, cl.PtrType): return "__implicit__" + str(self._add_implicit(constant)) else: return cl_type.make_literal(constant)
def visit_Num(self, n): num = n.n return astx.copy_node(n, unresolved_type=InlineConstantType( num, cl.infer_cl_type(num)))
def implicit_arg_types(self): return tuple(cl.infer_cl_type(value) for value in self.implicit_args)
def visit_Num(self, n): num = n.n return astx.copy_node(n, unresolved_type=InlineConstantType(num, cl.infer_cl_type(num)))
def _get_concrete_fn_for(self, args): arg_types = (cl.infer_cl_type(arg) for arg in args) arg_types = self._apply_default_types(arg_types) return self._get_concrete_fn_final(arg_types)
def default_types(self): """A tuple containing the types of the defaults.""" return tuple(cl.infer_cl_type(default) for default in self.defaults)