@Override public void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration) { assert enclosingVariablesDeclarationType == null; enclosingVariablesDeclarationType = variablesDeclaration.getBaseType(); super.visitVariablesDeclaration(variablesDeclaration); enclosingVariablesDeclarationType = null; }
public List<VariablesDeclaration> getUniformDecls() { return getGlobalVariablesDeclarations() .stream() .filter(item -> item.getBaseType().hasQualifier(TypeQualifier.UNIFORM)) .collect(Collectors.toList()); }
@Override public void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration) { if (variablesDeclaration.getBaseType().getWithoutQualifiers() instanceof StructDefinitionType) { final StructDefinitionType sdt = (StructDefinitionType) variablesDeclaration.getBaseType().getWithoutQualifiers(); if (sdt.hasStructNameType()) { // Initially, assume it is unused unusedStructs.add(sdt); } } super.visitVariablesDeclaration(variablesDeclaration); }
@Override public void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration) { if (variablesDeclaration.getBaseType().hasQualifier(TypeQualifier.UNIFORM)) { inUniformDecl = true; } super.visitVariablesDeclaration(variablesDeclaration); inUniformDecl = false; }
@Override public void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration) { visitChildFromParent(variablesDeclaration.getBaseType(), variablesDeclaration); List<VariableDeclInfo> children = new ArrayList<>(); children.addAll(variablesDeclaration.getDeclInfos()); for (VariableDeclInfo vd : children) { visitChildFromParent(vd, variablesDeclaration); } }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); if (!currentVariablesDeclaration.isPresent()) { return; } if (!nonConst(variableIdentifierExpr)) { return; } if (currentVariablesDeclaration.get().getBaseType().hasQualifier(TypeQualifier.CONST)) { ((QualifiedType) currentVariablesDeclaration.get().getBaseType()) .removeQualifier(TypeQualifier.CONST); if (atGlobalScope) { assert !globalsToBeReInitialized.contains(currentVariablesDeclaration.get()); globalsToBeReInitialized.add(currentVariablesDeclaration.get()); } } else if (shadingLanguageVersion.globalVariableInitializersMustBeConst() && atGlobalScope) { if (!globalsToBeReInitialized.contains(currentVariablesDeclaration.get())) { globalsToBeReInitialized.add(currentVariablesDeclaration.get()); } } }
private boolean incompatibleComponentVariableIsDeclaredInBlock() { final BasicType expectedType = getComponentType(); for (VariablesDeclaration variablesDeclaration : block.getStmts() .stream() .filter(item -> item instanceof DeclarationStmt) .map(item -> (DeclarationStmt) item) .map(item -> item.getVariablesDeclaration()) .collect(Collectors.toList())) { final Type actualType = variablesDeclaration.getBaseType().getWithoutQualifiers(); for (VariableDeclInfo vdi : variablesDeclaration.getDeclInfos() .stream() .filter(item -> item.getName().equals(getComponentName())) .collect(Collectors.toList())) { if (actualType != expectedType || vdi.hasArrayInfo()) { return true; } } } return false; }
@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); } }
@Override public void visitDeclarationStmt(DeclarationStmt declarationStmt) { super.visitDeclarationStmt(declarationStmt); if (!(declarationStmt.getVariablesDeclaration().getBaseType() .getWithoutQualifiers() instanceof StructNameType)) { return; } getOpportunitiesForStruct((StructNameType) declarationStmt.getVariablesDeclaration() .getBaseType().getWithoutQualifiers(), getVistitationDepth()); }
private StructifiedVariableInfo findOriginalVariableInfo() { return findOriginalVariableInfo( (StructNameType) declaration.getVariablesDeclaration().getBaseType() .getWithoutQualifiers(), Optional.ofNullable((ScalarInitializer) declaration.getVariablesDeclaration() .getDeclInfo(0).getInitializer())) .get(); }
.add(new VariablesDeclaration(((VariablesDeclaration) d).getBaseType(), declInfo));
/** * Yields the basic type that the component we are pulling out should have. * @return The basic type of the component. */ public BasicType getComponentType() { return BasicType.makeVectorType( ((BasicType) vectorVariablesDeclaration.getBaseType().getWithoutQualifiers()) .getElementType(), componentData.getWidth()); }
private void sweep(TranslationUnit tu) { final List<Declaration> oldTopLevelDecls = new ArrayList<>(); oldTopLevelDecls.addAll(tu.getTopLevelDeclarations()); for (Declaration decl : oldTopLevelDecls) { if (!(decl instanceof VariablesDeclaration)) { continue; } final VariablesDeclaration variablesDeclaration = (VariablesDeclaration) decl; int index = 0; while (index < variablesDeclaration.getNumDecls()) { if (unusedGlobals.contains(variablesDeclaration.getDeclInfo(index))) { variablesDeclaration.removeDeclInfo(index); } else { index++; } } if (variablesDeclaration.getNumDecls() == 0 && !isUsedStructType(variablesDeclaration.getBaseType())) { tu.removeTopLevelDeclaration(variablesDeclaration); } } }
private boolean nonConst(VariableIdentifierExpr variableIdentifierExpr) { final ScopeEntry se = currentScope.lookupScopeEntry(variableIdentifierExpr.getName()); return se != null && se.hasVariablesDeclaration() && !se.getVariablesDeclaration().getBaseType() .hasQualifier(TypeQualifier.CONST); }
@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 visitDeclarationStmt(DeclarationStmt declarationStmt) { super.visitDeclarationStmt(declarationStmt); if (!Util.isStructifiedDeclaration(declarationStmt)) { return; } final StructNameType structType = (StructNameType) declarationStmt.getVariablesDeclaration().getBaseType() .getWithoutQualifiers(); findInliningOpportunities(structType); }
private Expr insertFieldIntoStruct(String enclosingStructName, StructDefinitionType enclosingStruct, IRandom generator) { Expr result = new VariableIdentifierExpr(enclosingStructName); StructDefinitionType currentStruct = enclosingStruct; while (true) { Map<String, StructDefinitionType> structFields = getStructFields(currentStruct); if (!structFields.keySet().isEmpty() && generator.nextBoolean()) { String fieldName = structFields.keySet().stream().collect(Collectors.toList()) .get(generator.nextInt(structFields.size())); result = new MemberLookupExpr(result, fieldName); currentStruct = structFields.get(fieldName); } else { // Choose random position at which to insert the field. currentStruct.insertField(generator.nextInt(currentStruct.getNumFields() + 1), declToTransform.getVariablesDeclaration().getDeclInfo(0).getName(), declToTransform.getVariablesDeclaration().getBaseType()); result = new MemberLookupExpr(result, declToTransform.getVariablesDeclaration() .getDeclInfo(0).getName()); return result; } } }
@Override public void visitDeclarationStmt(DeclarationStmt declarationStmt) { super.visitDeclarationStmt(declarationStmt); // Currently we only structify solo declarations if (declarationStmt.getVariablesDeclaration().getNumDecls() != 1) { return; } // We don't currently structify arrays if (declarationStmt.getVariablesDeclaration().getDeclInfo(0).hasArrayInfo()) { return; } final Type baseType = declarationStmt.getVariablesDeclaration().getBaseType(); if (hasQualifiers(baseType)) { return; } // TODO: For simplicity, at present we do not structify non-basic types. The issue is that // if a struct S is to be structified, we need to declare the structs that enclose S *after* // S is declared, which is a bit fiddly (currently they all go at the top of the translation // unit). if (!(baseType.getWithoutQualifiers() instanceof BasicType)) { return; } opportunities.add(new StructificationOpportunity(declarationStmt, currentBlock(), tu, shadingLanguageVersion)); }
@Override public void visitFunctionPrototype(FunctionPrototype functionPrototype) { super.visitFunctionPrototype(functionPrototype); for (int i = 0; i < functionPrototype.getNumParameters(); i++) { if (functionPrototype.getParameter(i).getType().getWithoutQualifiers() .equals(declaration.getVariablesDeclaration().getBaseType().getWithoutQualifiers())) { assert declaration.getVariablesDeclaration().getDeclInfo(0).getName() .equals(functionPrototype.getParameter(i).getName()); functionPrototype.getParameter(i).setName(originalVariableInfo.getName()); functionPrototype.getParameter(i).setType(originalVariableInfo.getType()); } } }
@Override public void visitMemberLookupExpr(MemberLookupExpr memberLookupExpr) { super.visitMemberLookupExpr(memberLookupExpr); if (!(memberLookupExpr.getStructure() instanceof VariableIdentifierExpr)) { return; } VariableIdentifierExpr structVariable = ((VariableIdentifierExpr) memberLookupExpr .getStructure()); ScopeEntry se = currentScope.lookupScopeEntry(structVariable.getName()); if (se == null) { return; } if (se.getType().getWithoutQualifiers().equals(declaration.getVariablesDeclaration() .getBaseType().getWithoutQualifiers())) { // We've found the variable reference, but now we might have a chain, like: // s._f0._f2._f1._f0.v // We need to find the member expression that has .v and replace that with v MemberLookupExpr current = memberLookupExpr; while (current.getMember().startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX)) { current = (MemberLookupExpr) parentMap.getParent(current); } parentMap.getParent(current).replaceChild( current, new VariableIdentifierExpr(originalVariableInfo.getName())); } } }.visit(tu);