private Map<String, StructDefinitionType> getStructFields(StructDefinitionType struct) { return struct.getFieldNames().stream() .filter(item -> struct.getFieldType(item) instanceof StructNameType) .collect(Collectors.toMap( item -> item, item -> tu.getStructDefinition((StructNameType) struct.getFieldType(item))) ); }
public InlineStructifiedFieldReductionOpportunity(StructDefinitionType outerStruct, StructDefinitionType innerStruct, String fieldToInline, TranslationUnit tu, VisitationDepth depth) { super(depth); this.outerStruct = outerStruct; this.innerStruct = innerStruct; this.fieldToInline = fieldToInline; this.tu = tu; assert outerStruct != innerStruct; assert fieldToInline.startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX); assert outerStruct.getFieldType(fieldToInline).getWithoutQualifiers() instanceof StructNameType : "Can only inline a struct field of a struct"; }
private boolean reachesOriginalVariable(StructDefinitionType structDefinitionType, String field) { if (!(field.startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX))) { return true; } if (!(structDefinitionType.getFieldType(field).getWithoutQualifiers() instanceof StructNameType)) { return false; } final StructNameType fieldType = (StructNameType) structDefinitionType.getFieldType(field).getWithoutQualifiers(); final StructDefinitionType nestedStruct = currentScope.lookupStructName(fieldType.getName()); return nestedStruct.getFieldNames().stream() .anyMatch(item -> reachesOriginalVariable(nestedStruct, item)); }
@Override public void visitStructDefinitionType(StructDefinitionType structDefinitionType) { out.append("struct "); if (structDefinitionType.hasStructNameType()) { visit(structDefinitionType.getStructNameType()); out.append(" "); } out.append("{" + newLine()); increaseIndent(); for (String name : structDefinitionType.getFieldNames()) { out.append(indent()); visit(structDefinitionType.getFieldType(name)); out.append(" " + name); processArrayInfo(structDefinitionType.getFieldType(name)); out.append(";" + newLine()); } decreaseIndent(); out.append("}"); }
private Expr makeInitializationExpr(StructDefinitionType structDefinitionType, Expr originalInitializer) { List<Expr> args = new ArrayList<>(); for (int i = 0; i < structDefinitionType.getNumFields(); i++) { if (structDefinitionType.getFieldName(i).startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX)) { final Type fieldType = structDefinitionType.getFieldType(i); args.add(fieldType instanceof StructNameType ? makeInitializationExpr(tu.getStructDefinition((StructNameType) fieldType), originalInitializer) : fieldType.getCanonicalConstant()); } else { args.add(originalInitializer); } } return new TypeConstructorExpr(structDefinitionType.getStructNameType().getName(), args); }
Type fieldTypeWithoutQualifiers = structDefinitionType.getFieldType(i) .getWithoutQualifiers(); if (fieldTypeWithoutQualifiers instanceof StructNameType) { structDefinitionType.getFieldType(i), componentInitializer));
public void findInliningOpportunities(StructNameType structType) { assert structType.getName().startsWith(Constants.STRUCTIFICATION_STRUCT_PREFIX); final StructDefinitionType structDefinitionType = currentScope.lookupStructName(structType.getName()); for (String f : structDefinitionType.getFieldNames()) { if (!f.startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX)) { continue; } if (structDefinitionType.getFieldType(f).getWithoutQualifiers() instanceof StructNameType) { final StructNameType innerStructType = (StructNameType) structDefinitionType.getFieldType(f).getWithoutQualifiers(); opportunities.add(new InlineStructifiedFieldReductionOpportunity( structDefinitionType, currentScope.lookupStructName(innerStructType.getName()), f, tu, getVistitationDepth())); findInliningOpportunities(innerStructType); } } }
@Override public void visitStructDefinitionType(StructDefinitionType structDefinitionType) { if (structDefinitionType.hasStructNameType()) { visitChildFromParent(structDefinitionType.getStructNameType(), structDefinitionType); } for (String name : structDefinitionType.getFieldNames()) { visitChildFromParent(structDefinitionType.getFieldType(name), structDefinitionType); } }
types.put(memberLookupExpr, structDeclarationMap.get(structureType.getWithoutQualifiers()) .getFieldType(memberLookupExpr.getMember())); types.put(memberLookupExpr, ((StructDefinitionType) structureType.getWithoutQualifiers()) .getFieldType(memberLookupExpr.getMember()));
: oldFieldName; oldToNewFieldNames.put(oldFieldName, newFieldName); outerStruct.insertField(indexOfInlinedField + i, newFieldName, innerStruct.getFieldType(i));
private void getOpportunitiesForStruct(StructNameType structType, VisitationDepth visitationDepth) { if (!(structType.getName().startsWith(Constants .STRUCTIFICATION_STRUCT_PREFIX))) { return; } final StructDefinitionType structDefinitionType = currentScope.lookupStructName(structType.getName()); for (String field : structDefinitionType.getFieldNames()) { if (!reachesOriginalVariable(structDefinitionType, field) && structDefinitionType.getNumFields() > 1) { final RemoveStructFieldReductionOpportunity op = new RemoveStructFieldReductionOpportunity( structDefinitionType, field, translationUnit, visitationDepth); if (op.preconditionHolds()) { opportunities.add(op); } } if (structDefinitionType.getFieldType(field).getWithoutQualifiers() instanceof StructNameType) { getOpportunitiesForStruct( (StructNameType) structDefinitionType.getFieldType(field).getWithoutQualifiers(), visitationDepth.deeper()); } } }