private Type stripQualifiers(Type type) { // TODO: when implementing this the only compound type I considered was ArrayType. if (type instanceof QualifiedType) { return stripQualifiers(type.getWithoutQualifiers()); } if (type instanceof ArrayType) { return new ArrayType(stripQualifiers(((ArrayType) type).getBaseType()), ((ArrayType) type).getArrayInfo()); } return type; }
@Override public ArrayConstructorExpr clone() { List<Expr> newArgs = args.stream().map(Expr::clone).collect(Collectors.toList()); return new ArrayConstructorExpr(arrayType.clone(), newArgs); }
private void processArrayInfo(Type type) { if (!(type.getWithoutQualifiers() instanceof ArrayType)) { return; } ArrayType arrayType = (ArrayType) type.getWithoutQualifiers(); while (true) { out.append("[" + (arrayType.getArrayInfo().hasSize() ? arrayType.getArrayInfo().getSize() : "") + "]"); if (!(arrayType.getBaseType().getWithoutQualifiers() instanceof ArrayType)) { break; } arrayType = (ArrayType) arrayType.getBaseType().getWithoutQualifiers(); } }
private Type getType(Type baseType, ArrayInfo arrayInfo) { if (arrayInfo == null) { return baseType; } return new ArrayType(baseType, arrayInfo); }
@Override public void visitArrayType(ArrayType arrayType) { // Do not generate array info, as this has to come after the associated variable name visit(arrayType.getBaseType()); }
@Override public void visitArrayConstructorExpr(ArrayConstructorExpr arrayConstructorExpr) { visit(arrayConstructorExpr.getArrayType()); out.append("[" + arrayConstructorExpr.getArrayType().getArrayInfo().getSize() + "]("); boolean first = true; for (Expr e : arrayConstructorExpr.getArgs()) { if (!first) { out.append(", "); } first = false; visit(e); } out.append(")"); }
@Override public ArrayType clone() { return new ArrayType(baseType.clone(), arrayInfo.clone()); }
ArrayType arrayType = (ArrayType) targetType; List<Expr> args = new ArrayList<>(); for (int i = 0; i < arrayType.getArrayInfo().getSize(); i++) { args.add(makeExpr(arrayType.getBaseType(), isLValue, constContext, depth + 1));
@Override public void visitArrayType(ArrayType arrayType) { visitChildFromParent(arrayType.getBaseType(), arrayType); }
private static Integer getSize(Type type) { assert isArrayVectorOrMatrix(type); if (type instanceof ArrayType) { return ((ArrayType) type).getArrayInfo().getSize(); } if (BasicType.allVectorTypes().contains(type)) { return ((BasicType) type).getNumElements(); } return ((BasicType) type).getNumColumns(); }
Type dropQualifiersThatCannotBeUsedForLocalVariable(Type type) { if (type instanceof ArrayType) { return new ArrayType( dropQualifiersThatCannotBeUsedForLocalVariable(((ArrayType) type).getBaseType()), ((ArrayType) type).getArrayInfo()); } if (!(type instanceof QualifiedType)) { return type; } List<TypeQualifier> newQualifiers = new ArrayList<>(); for (TypeQualifier q : ((QualifiedType) type).getQualifiers()) { if (Arrays.asList( TypeQualifier.IN_PARAM, TypeQualifier.INOUT_PARAM, TypeQualifier.OUT_PARAM, TypeQualifier.UNIFORM, TypeQualifier.SHADER_INPUT, TypeQualifier.SHADER_OUTPUT).contains(q)) { continue; } // Many of the other qualifiers are probably not allowed; move them up as we discover this newQualifiers.add(q); } return new QualifiedType(((QualifiedType) type).getTargetType(), newQualifiers); }
private static Initializer makeInitializer(BasicType baseType, ArrayInfo arrayInfo, List<Number> args) { if (arrayInfo != null) { assert arrayInfo.getSize() * baseType.getNumElements() == args.size(); List<Expr> argExprs = new ArrayList<>(); for (int index = 0; index < arrayInfo.getSize(); index++) { argExprs.add(getBasicTypeLiteralExpr(baseType, args.subList(index * baseType.getNumElements(), (index + 1) * baseType.getNumElements()))); } return new ScalarInitializer(new ArrayConstructorExpr( new ArrayType(baseType.getWithoutQualifiers(), arrayInfo.clone()), argExprs)); } return new ScalarInitializer(getBasicTypeLiteralExpr(baseType, args)); }
boolean typeRefersToUniform(Type type) { if (type.hasQualifier(TypeQualifier.UNIFORM)) { return true; } if (!(type.getWithoutQualifiers() instanceof ArrayType)) { return false; } return typeRefersToUniform(((ArrayType) type.getWithoutQualifiers()).getBaseType()); }
@Override public void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration) { final Type baseType = variablesDeclaration.getBaseType(); visit(baseType); out.append(" "); boolean first = true; for (VariableDeclInfo vdi : variablesDeclaration.getDeclInfos()) { if (!first) { out.append(", "); } first = false; out.append(vdi.getName()); if (vdi.hasArrayInfo()) { out.append("[" + vdi.getArrayInfo().getSize() + "]"); assert !(baseType instanceof ArrayType); } else if (baseType instanceof ArrayType) { out.append("[" + ((ArrayType) baseType).getArrayInfo().getSize() + "]"); } if (vdi.hasInitializer()) { out.append(" = "); visit(vdi.getInitializer()); } } }
@Override public void visitFunctionPrototype(FunctionPrototype functionPrototype) { encounteredFunctionPrototypes.add(functionPrototype); for (ParameterDecl p : functionPrototype.getParameters()) { visitParameterDecl(p); if (addEncounteredParametersToScope) { if (p.getName() == null) { continue; } currentScope.add(p.getName(), p.getArrayInfo() == null ? p.getType() : new ArrayType(p.getType(), p.getArrayInfo()), Optional.of(p)); } } }
@Override public void visitArrayIndexExpr(ArrayIndexExpr arrayIndexExpr) { super.visitArrayIndexExpr(arrayIndexExpr); Type arrayType = lookupType(arrayIndexExpr.getArray()); if (arrayType == null) { return; } arrayType = arrayType.getWithoutQualifiers(); Type elementType; if (BasicType.allVectorTypes().contains(arrayType)) { elementType = ((BasicType) arrayType).getElementType(); } else if (BasicType.allMatrixTypes().contains(arrayType)) { elementType = ((BasicType) arrayType).getColumnType(); } else { assert arrayType instanceof ArrayType; elementType = ((ArrayType) arrayType).getBaseType(); } types.put(arrayIndexExpr, elementType); }
@Override public void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration) { visit(variablesDeclaration.getBaseType()); List<VariableDeclInfo> children = new ArrayList<>(); children.addAll(variablesDeclaration.getDeclInfos()); for (VariableDeclInfo declInfo : children) { visitVariableDeclInfo(declInfo); currentScope.add(declInfo.getName(), declInfo.getArrayInfo() == null ? variablesDeclaration.getBaseType() : new ArrayType(variablesDeclaration.getBaseType(), declInfo.getArrayInfo()), Optional.empty(), declInfo, variablesDeclaration); visitVariableDeclInfoAfterAddedToScope(declInfo); } }
private Pair<List<String>, List<Type>> getMembers(Member_listContext memberListContext) { final LinkedList<String> fieldNames = new LinkedList<>(); final LinkedList<Type> fieldTypes = new LinkedList<>(); for (Member_listContext ctx = memberListContext; ctx != null; ctx = ctx.member_list()) { final Type baseType = visitFully_specified_type(ctx.member_declaration().fully_specified_type()); for (Struct_declarator_listContext declarators = ctx.member_declaration() .struct_declarator_list(); declarators != null; declarators = declarators.struct_declarator_list()) { fieldNames.addFirst(declarators.struct_declarator().IDENTIFIER().getText()); if (declarators.struct_declarator().array_specifier() == null) { fieldTypes.addFirst(baseType); } else { fieldTypes.addFirst(new ArrayType(baseType, getArrayInfo(declarators.struct_declarator().array_specifier()))); } } } return new Pair<>(fieldNames, fieldTypes); }
if (header.array_specifier() != null) { return new ArrayConstructorExpr( new ArrayType( getBuiltinType(header.function_identifier().builtin_type_specifier_nonarray()), getArrayInfo(header.array_specifier())), params); header.function_identifier().variable_identifier().getText()); return new ArrayConstructorExpr( new ArrayType(structType, getArrayInfo(header.array_specifier())), params);