Search in sources :

Example 6 with CompilationUnit

use of abs.frontend.ast.CompilationUnit in project abstools by abstools.

the class FileAddLocationHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveMenuSelection(event);
    Object firstElement = selection.getFirstElement();
    if (firstElement instanceof IFile) {
        IFile file = (IFile) firstElement;
        IProject project = file.getProject();
        AbsNature nature = UtilityFunctions.getAbsNature(project);
        if (nature == null)
            return null;
        IPersistentPreferenceStore projectStore = nature.getProjectPreferenceStore();
        boolean locationTypecheckingEnabled = projectStore.getBoolean(Constants.LOCATION_TYPECHECK);
        if (!locationTypecheckingEnabled) {
            MessageDialog.openInformation(Display.getDefault().getActiveShell(), "Locationtypechecking", "Location type checking is disabled. Please enable for this function to work!");
            return null;
        }
        UtilityFunctions.saveEditor(file, false);
        Map<LocationTypeVariable, LocationType> locationTypeInferrerResult = nature.getLocationTypeInferrerResult();
        if (locationTypeInferrerResult != null) {
            Map<LocationTypeVariable, LocationType> filteredResults = new HashMap<LocationTypeVariable, LocationType>();
            ASTNode<?> node;
            CompilationUnit cu;
            for (Entry<LocationTypeVariable, LocationType> entry : locationTypeInferrerResult.entrySet()) {
                node = entry.getKey().getNode();
                if (node == null)
                    continue;
                cu = node.getCompilationUnit();
                if (cu == null)
                    continue;
                if (file.getLocation().toFile().getAbsolutePath().equals(cu.getFileName())) {
                    filteredResults.put(entry.getKey(), entry.getValue());
                }
            }
            InferMain inferMain = new InferMain();
            String commandId = event.getCommand().getId();
            if ("org.abs-models.abs.plugin.fileaddalllocations".equals(commandId)) {
                inferMain.setConfig(InferMain.Config.values());
            } else if ("org.abs-models.abs.plugin.fileaddclasslocations".equals(commandId)) {
                inferMain.setConfig(InferMain.Config.CLASSES);
            } else if ("org.abs-models.abs.plugin.fileaddfieldlocations".equals(commandId)) {
                inferMain.setConfig(InferMain.Config.FIELDS);
            } else if ("org.abs-models.abs.plugin.fileaddfunctionlocations".equals(commandId)) {
                inferMain.setConfig(InferMain.Config.FUNCTIONS);
            } else if ("org.abs-models.abs.plugin.fileaddinterfacelocations".equals(commandId)) {
                inferMain.setConfig(InferMain.Config.INTERFACES);
            }
            try {
                inferMain.writeInferenceResultsBack(filteredResults);
                try {
                    file.refreshLocal(IResource.DEPTH_INFINITE, null);
                } catch (CoreException e) {
                    Activator.logException(e);
                }
            } catch (IOException e) {
                MessageDialog.openError(Display.getDefault().getActiveShell(), "Error while inserting locations", "An error occurred while inserting locations!\n" + e.getLocalizedMessage());
            }
        }
    }
    return null;
}
Also used : CompilationUnit(abs.frontend.ast.CompilationUnit) IFile(org.eclipse.core.resources.IFile) InferMain(abs.frontend.typechecker.locationtypes.infer.InferMain) HashMap(java.util.HashMap) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) IPersistentPreferenceStore(org.eclipse.jface.preference.IPersistentPreferenceStore) LocationTypeVariable(abs.frontend.typechecker.locationtypes.infer.LocationTypeVariable) CoreException(org.eclipse.core.runtime.CoreException) LocationType(abs.frontend.typechecker.locationtypes.LocationType) AbsNature(org.absmodels.abs.plugin.builder.AbsNature)

Example 7 with CompilationUnit

use of abs.frontend.ast.CompilationUnit in project abstools by abstools.

the class AbsNature method addPackagesForTypeChecking.

/**
 * Add ABS package dependencies to {@link AbsNature#modelbuilder} for type checking
 * @throws TypecheckInternalException
 */
private void addPackagesForTypeChecking() throws TypecheckInternalException {
    try {
        Main m = new Main();
        m.setWithStdLib(true);
        List<CompilationUnit> units = new ArrayList<CompilationUnit>();
        for (PackageEntry entry : packageContainer.getPackages()) {
            File file = new File(entry.getPath());
            if (isABSPackage(file)) {
                units.addAll(m.parseABSPackageFile(file));
            }
        }
        modelbuilder.addCompilationUnits(units);
    } catch (IOException e) {
        throw new TypecheckInternalException(e);
    } catch (NoModelException e) {
    // ignore
    }
}
Also used : CompilationUnit(abs.frontend.ast.CompilationUnit) NoModelException(org.absmodels.abs.plugin.internal.NoModelException) PackageEntry(org.absmodels.abs.plugin.editor.outline.PackageEntry) TypecheckInternalException(org.absmodels.abs.plugin.internal.TypecheckInternalException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Main(abs.frontend.parser.Main) PackageAbsFile(org.absmodels.abs.plugin.editor.outline.PackageAbsFile) File(java.io.File)

Example 8 with CompilationUnit

use of abs.frontend.ast.CompilationUnit in project abstools by abstools.

the class AbsNature method parseABSFile.

/**
 * @deprecated unused
 */
public void parseABSFile(PackageAbsFile file, boolean withincomplete, Object monitor) {
    Main m = new Main();
    m.setWithStdLib(true);
    List<CompilationUnit> units = new ArrayList<CompilationUnit>();
    try {
        final File f = new File(file.getAbsoluteFilePath());
        assert f.exists();
        units.addAll(m.parseABSPackageFile(f));
        modelbuilder.addCompilationUnits(units);
    } catch (IOException e) {
        Activator.logException(e);
    } catch (NoModelException e) {
    }
}
Also used : CompilationUnit(abs.frontend.ast.CompilationUnit) NoModelException(org.absmodels.abs.plugin.internal.NoModelException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Main(abs.frontend.parser.Main) PackageAbsFile(org.absmodels.abs.plugin.editor.outline.PackageAbsFile) File(java.io.File)

Example 9 with CompilationUnit

use of abs.frontend.ast.CompilationUnit in project abstools by abstools.

the class InferMain method writeInferenceResultsBack.

public void writeInferenceResultsBack(Map<LocationTypeVariable, LocationType> results) throws IOException {
    Map<CompilationUnit, List<LocationTypeVariable>> m = clusterByCompilationUnit(results);
    for (Entry<CompilationUnit, List<LocationTypeVariable>> e : m.entrySet()) {
        CompilationUnit cu = e.getKey();
        List<LocationTypeVariable> l = getSortedList(e);
        File file = new File(cu.getFileName());
        StringBuilder sb = FileUtils.fileToStringBuilder(file);
        int offset = 0;
        for (LocationTypeVariable ltv : l) {
            if (shouldBeConsidered(ltv)) {
                int diff = ltv.getTypeNode().getAbsolutePosition();
                int pos = offset + diff;
                String s = results.get(ltv).toAnnoString();
                sb.insert(pos, s);
                offset += s.length();
            }
        }
        FileUtils.writeStringBuilderToFile(sb, file);
    }
}
Also used : CompilationUnit(abs.frontend.ast.CompilationUnit) File(java.io.File)

Example 10 with CompilationUnit

use of abs.frontend.ast.CompilationUnit in project abstools by abstools.

the class InferMain method clusterByCompilationUnit.

private Map<CompilationUnit, List<LocationTypeVariable>> clusterByCompilationUnit(Map<LocationTypeVariable, LocationType> results) {
    Map<CompilationUnit, List<LocationTypeVariable>> m = new HashMap<>();
    for (LocationTypeVariable ltv : results.keySet()) {
        ASTNode<?> node = ltv.getNode();
        if (node == null)
            continue;
        CompilationUnit cu = node.getCompilationUnit();
        if (node.getModuleDecl().getName().startsWith("ABS."))
            continue;
        List<LocationTypeVariable> list = m.get(cu);
        if (list == null) {
            list = new ArrayList<>();
            m.put(cu, list);
        }
        list.add(ltv);
    }
    return m;
}
Also used : CompilationUnit(abs.frontend.ast.CompilationUnit)

Aggregations

CompilationUnit (abs.frontend.ast.CompilationUnit)27 Model (abs.frontend.ast.Model)7 ModuleDecl (abs.frontend.ast.ModuleDecl)6 File (java.io.File)5 Test (org.junit.Test)5 ClassDecl (abs.frontend.ast.ClassDecl)4 DeltaDecl (abs.frontend.ast.DeltaDecl)4 MethodSig (abs.frontend.ast.MethodSig)4 Main (abs.frontend.parser.Main)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 AbsNature (org.absmodels.abs.plugin.builder.AbsNature)4 DeltaAccess (abs.frontend.ast.DeltaAccess)3 List (abs.frontend.ast.List)3 ModifyClassModifier (abs.frontend.ast.ModifyClassModifier)3 SemanticConditionList (abs.frontend.analyser.SemanticConditionList)2 Block (abs.frontend.ast.Block)2 MethodImpl (abs.frontend.ast.MethodImpl)2 SkipStmt (abs.frontend.ast.SkipStmt)2 StringReader (java.io.StringReader)2