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))) ); }
@Override public void visitStructDefinitionType(StructDefinitionType structDefinitionType) { super.visitStructDefinitionType(structDefinitionType); // TODO: right now we do not obfuscate fields of anonymous structs. if (structDefinitionType.hasStructNameType()) { final String oldName = structDefinitionType.getStructNameType().getName(); final String newName = renameStruct(oldName); namedStructRenaming.put(oldName, newName); structDefinitionTypes.add(structDefinitionType); final Map<String, String> fieldRenaming = new HashMap<>(); for (String fieldName : structDefinitionType.getFieldNames()) { fieldRenaming.put(fieldName, renameStructField(fieldName)); } structFieldRenaming.put(structDefinitionType.getStructNameType(), fieldRenaming); } }
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("}"); }
@Override public void visitStructDefinitionType(StructDefinitionType structDefinitionType) { if (structDefinitionType.hasStructNameType()) { visitChildFromParent(structDefinitionType.getStructNameType(), structDefinitionType); } for (String name : structDefinitionType.getFieldNames()) { visitChildFromParent(structDefinitionType.getFieldType(name), structDefinitionType); } }
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); } } }
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()); } } }