Search in sources :

Example 1 with ModuleImportation

use of org.eclipse.titan.designer.AST.ModuleImportation in project titan.EclipsePlug-ins by eclipse.

the class TTCN3Module method getAssBySRef.

@Override
public /**
 * {@inheritDoc}
 */
Assignment getAssBySRef(final CompilationTimeStamp timestamp, final Reference reference, final IReferenceChain refChain) {
    // if a moduleId is present, that import (or the actual module)
    // must be searched
    final Identifier moduleId = reference.getModuleIdentifier();
    final Location referenceLocation = reference.getLocation();
    final Identifier id = reference.getId();
    if (id == null) {
        return null;
    }
    Assignment temporalAssignment = null;
    if (moduleId == null) {
        // no module name is given in the reference
        if ("anytype".equals(id.getTtcnName())) {
            return anytypeDefinition;
        }
        Assignment tempResult = null;
        for (final ImportModule impMod : importedModules) {
            if (impMod.getReferredModule() != null) {
                final ModuleImportationChain referenceChain = new ModuleImportationChain(ModuleImportationChain.CIRCULARREFERENCE, false);
                tempResult = impMod.importAssignment(timestamp, referenceChain, identifier, reference, new ArrayList<ModuleImportation>());
                if (tempResult != null && !tempResult.getMyScope().getModuleScope().isVisible(timestamp, this.getIdentifier(), tempResult)) {
                    tempResult = null;
                }
                if (tempResult != null) {
                    if (temporalAssignment == null) {
                        temporalAssignment = tempResult;
                    } else if (temporalAssignment != tempResult) {
                        reference.getLocation().reportSemanticError("It is not possible to resolve this reference unambigously, as  it can be resolved to `" + temporalAssignment.getFullName() + "' and to `" + tempResult.getFullName() + "'");
                        return null;
                    }
                }
            }
        }
        if (temporalAssignment != null) {
            return temporalAssignment;
        }
        referenceLocation.reportSemanticError(MessageFormat.format(MISSINGREFERENCE, id.getDisplayName(), identifier.getDisplayName()));
        missingReferences.add(reference);
    } else if (moduleId.getName().equals(name)) {
        // the reference points to the own module
        if ("anytype".equals(id.getTtcnName())) {
            return anytypeDefinition;
        }
        temporalAssignment = definitions.getLocalAssignmentByID(timestamp, id);
        if (temporalAssignment == null) {
            referenceLocation.reportSemanticError(MessageFormat.format(MISSINGREFERENCE, id.getDisplayName(), identifier.getDisplayName()));
        }
    } else {
        // the reference points to another module
        for (final ImportModule impMod : importedModules) {
            if (moduleId.getName().equals(impMod.getName())) {
                if (impMod.getReferredModule() == null) {
                    return temporalAssignment;
                }
                final ModuleImportationChain referenceChain = new ModuleImportationChain(ModuleImportationChain.CIRCULARREFERENCE, false);
                temporalAssignment = impMod.importAssignment(timestamp, referenceChain, identifier, reference, new ArrayList<ModuleImportation>());
                if (!impMod.getReferredModule().isVisible(timestamp, this.getIdentifier(), temporalAssignment)) {
                    temporalAssignment = null;
                }
                if (temporalAssignment == null) {
                    referenceLocation.reportSemanticError(MessageFormat.format(MISSINGREFERENCE, id.getDisplayName(), impMod.getIdentifier().getDisplayName()));
                }
                return temporalAssignment;
            }
        }
        referenceLocation.reportConfigurableSemanticProblem(Platform.getPreferencesService().getString(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.REPORTMISSINGIMPORTEDMODULE, GeneralConstants.WARNING, null), MessageFormat.format(ImportModule.MISSINGMODULE, moduleId.getDisplayName()));
        missingReferences.add(reference);
    }
    return temporalAssignment;
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) Identifier(org.eclipse.titan.designer.AST.Identifier) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ModuleImportationChain(org.eclipse.titan.designer.AST.ModuleImportationChain) Location(org.eclipse.titan.designer.AST.Location) ModuleImportation(org.eclipse.titan.designer.AST.ModuleImportation)

Example 2 with ModuleImportation

use of org.eclipse.titan.designer.AST.ModuleImportation in project titan.EclipsePlug-ins by eclipse.

the class UnusedImportsProject method process.

@Override
protected void process(final IProject project, final Problems problems) {
    TITANDebugConsole.println("Unused import");
    final ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(project);
    final Set<String> knownModuleNames = projectSourceParser.getKnownModuleNames();
    final List<Module> modules = new ArrayList<Module>();
    for (final String moduleName : new TreeSet<String>(knownModuleNames)) {
        Module module = projectSourceParser.getModuleByName(moduleName);
        modules.add(module);
    }
    final Set<Module> setOfImportedModules = new HashSet<Module>();
    for (Module module : modules) {
        setOfImportedModules.clear();
        setOfImportedModules.addAll(module.getImportedModules());
        ImportsCheck check = new ImportsCheck();
        module.accept(check);
        setOfImportedModules.removeAll(check.getModules());
        if (module instanceof TTCN3Module) {
            for (ImportModule mod : ((TTCN3Module) module).getImports()) {
                for (Module m : setOfImportedModules) {
                    if (m.getIdentifier().equals(mod.getIdentifier())) {
                        problems.report(mod.getIdentifier().getLocation(), "Possibly unused importation");
                    }
                }
            }
        } else {
            ModuleImportsCheck importsCheck = new ModuleImportsCheck();
            module.accept(importsCheck);
            for (ModuleImportation im : importsCheck.getImports()) {
                for (Module m : setOfImportedModules) {
                    if (m.getIdentifier().equals(im.getIdentifier())) {
                        problems.report(im.getIdentifier().getLocation(), "Possibly unused importation");
                    }
                }
            }
        }
    }
}
Also used : TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module) ArrayList(java.util.ArrayList) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) ImportModule(org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule) TreeSet(java.util.TreeSet) ImportModule(org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule) Module(org.eclipse.titan.designer.AST.Module) TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module) HashSet(java.util.HashSet) ModuleImportation(org.eclipse.titan.designer.AST.ModuleImportation)

Example 3 with ModuleImportation

use of org.eclipse.titan.designer.AST.ModuleImportation in project titan.EclipsePlug-ins by eclipse.

the class ImportModule method importAssignment.

@Override
public /**
 * {@inheritDoc}
 */
Assignment importAssignment(final CompilationTimeStamp timestamp, final ModuleImportationChain referenceChain, final Identifier moduleId, final Reference reference, final List<ModuleImportation> usedImports) {
    if (referenceChain.contains(this)) {
        return null;
    }
    if (referredModule == null) {
        return null;
    }
    Assignment result = null;
    if (hasImportOfImport) {
        if (referredModule instanceof TTCN3Module) {
            final TTCN3Module ttcnmodule = (TTCN3Module) referredModule;
            Assignment tempResult = null;
            final List<ImportModule> imports = ttcnmodule.getImports();
            for (final ImportModule importation : imports) {
                final List<ModuleImportation> tempUsedImports = new ArrayList<ModuleImportation>();
                // check if it could be reached if
                // visibility is out of question
                referenceChain.markState();
                if (importation.getVisibilityModifier() == VisibilityModifier.Public) {
                    tempResult = importation.importAssignment(timestamp, referenceChain, moduleId, reference, tempUsedImports);
                } else if (importation.getVisibilityModifier() == VisibilityModifier.Friend) {
                    tempResult = importation.importAssignment(timestamp, referenceChain, moduleId, reference, tempUsedImports);
                    if (tempResult != null) {
                        tempUsedImports.add(importation);
                    }
                }
                referenceChain.previousState();
                if (tempResult != null) {
                    // if found something check if
                    // all imports used to find it
                    // are visible in the actual
                    // module
                    boolean visible = true;
                    for (final ModuleImportation usedImportation : tempUsedImports) {
                        if (usedImportation instanceof ImportModule) {
                            final ImportModule ttcnImport = (ImportModule) usedImportation;
                            if (!ttcnImport.getMyModule().isVisible(timestamp, myModuleIdentifier, ttcnImport)) {
                                visible = false;
                            }
                        }
                    }
                    if (visible) {
                        usedImports.addAll(tempUsedImports);
                        if (result == null) {
                            result = tempResult;
                        } else if (result != tempResult) {
                            // the reference could point to two locations
                            reference.getLocation().reportSemanticError("It is not possible to resolve this reference unambigously, as  it can be resolved to `" + result.getFullName() + "' and to `" + tempResult.getFullName() + "'");
                            return null;
                        }
                    }
                    tempResult = null;
                }
            }
        }
    }
    if (hasNormalImport) {
        result = referredModule.importAssignment(timestamp, moduleId, reference);
    }
    if (result != null) {
        usedImports.add(this);
        setUsedForImportation();
    }
    return result;
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) ArrayList(java.util.ArrayList) ModuleImportation(org.eclipse.titan.designer.AST.ModuleImportation)

Aggregations

ArrayList (java.util.ArrayList)3 ModuleImportation (org.eclipse.titan.designer.AST.ModuleImportation)3 Assignment (org.eclipse.titan.designer.AST.Assignment)2 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Identifier (org.eclipse.titan.designer.AST.Identifier)1 Location (org.eclipse.titan.designer.AST.Location)1 Module (org.eclipse.titan.designer.AST.Module)1 ModuleImportationChain (org.eclipse.titan.designer.AST.ModuleImportationChain)1 ImportModule (org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule)1 TTCN3Module (org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module)1 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)1