def visit_Subscript(self, node): self.visit(node.value) # type of a[1:2, 3, 4:1] is the type of: declval(a)(slice, long, slice) if isextslice(node.slice): self.visit(node.slice) def f(t): def et(a, *b): return "{0}({1})".format(a, ", ".join(b)) dim_types = tuple(self.result[d] for d in node.slice.elts) return self.builder.ExpressionType(et, (t, ) + dim_types) elif isnum(node.slice) and node.slice.value >= 0: # type of a[2] is the type of an elements of a # this special case is to make type inference easier # for the back end compiler def f(t): return self.builder.ElementType(node.slice.value, t) else: # type of a[i] is the return type of the matching function self.visit(node.slice) def f(x): return self.builder.ExpressionType( "{0}[{1}]".format, (x, self.result[node.slice])) f and self.combine(node, node.value, unary_op=f)
def visit_AssignedSubscript(self, node): if isinstance(node.slice, ast.Slice): return False elif isextslice(node.slice): return False else: self.visit(node.slice) self.combine(node.value, node.slice, unary_op=self.builder.IndexableType, aliasing_type=True, register=True) return True
def visit_Subscript(self, node): value = self.visit(node.value) # we cannot overload the [] operator in that case if isstr(node.value): value = 'pythonic::types::str({})'.format(value) # positive static index case if (isnum(node.slice) and (node.slice.value >= 0) and isinstance(node.slice.value, int)): return "std::get<{0}>({1})".format(node.slice.value, value) # positive indexing case elif self.all_positive(node.slice): slice_ = self.visit(node.slice) return "{1}.fast({0})".format(slice_, value) # extended slice case elif isextslice(node.slice): slices = [self.visit(elt) for elt in node.slice.elts] return "{1}({0})".format(','.join(slices), value) # standard case else: slice_ = self.visit(node.slice) return "{1}[{0}]".format(slice_, value)