use of org.eclipse.titan.designer.AST.TTCN3.definitions.Definitions in project titan.EclipsePlug-ins by eclipse.
the class ComponentTypeBody method generateCode.
/**
* Add generated java code on this level.
*
* @param aData only used to update imports if needed
* @param source the source code generated
*/
public void generateCode(final JavaGenData aData, final StringBuilder source) {
final StringBuilder init_comp = aData.getInitComp();
init_comp.append("if(\"").append(identifier.getDisplayName()).append("\".equals(component_type)) {\n");
if (extendsReferences != null) {
boolean hasBaseComponents = false;
for (final ComponentTypeBody cb : compatibleBodies) {
if (cb.definitions.size() > 0) {
if (!hasBaseComponents) {
init_comp.append("if(init_base_comps) {\n");
hasBaseComponents = true;
}
// TODO get_scope_mod_gen
if (getModuleScope().equals(cb.getModuleScope())) {
init_comp.append("init_comp_type(\"");
init_comp.append(cb.getIdentifier().getDisplayName());
init_comp.append("\", false);\n");
} else {
init_comp.append("//TODO implement Module_List");
init_comp.append("Module_List::initialize_component(\"");
init_comp.append(cb.getModuleScope().getIdentifier().getDisplayName());
init_comp.append("\", \"");
init_comp.append(cb.getIdentifier().getDisplayName());
init_comp.append("\", false);\n");
}
}
}
if (hasBaseComponents) {
init_comp.append("}\n");
}
}
for (final Definition def : definitions) {
if (extendsGainedDefinitions.containsKey(def.getIdentifier().getName())) {
def.generateCodeInitComp(aData, init_comp, extendsGainedDefinitions.get(def.getIdentifier().getName()));
} else {
def.generateCode(aData, true);
}
}
init_comp.append("return true;\n");
init_comp.append("} else ");
}
use of org.eclipse.titan.designer.AST.TTCN3.definitions.Definitions in project titan.EclipsePlug-ins by eclipse.
the class ComponentTypeBody method addDefinitionsOfExtendsParents.
private void addDefinitionsOfExtendsParents(final CompilationTimeStamp timestamp) {
extendsGainedDefinitions.clear();
if (extendsReferences == null) {
return;
}
extendsReferences.check(timestamp);
final IReferenceChain referenceChain = ReferenceChain.getInstance(CIRCULAREXTENSIONCHAIN, true);
if (referenceChain.add(this)) {
extendsReferences.checkRecursion(referenceChain);
}
referenceChain.release();
initCompatibility(extendsReferences);
// collect definitions
final List<ComponentTypeBody> bodies = getExtendsInheritedComponentBodies();
for (final ComponentTypeBody body : bodies) {
final Map<String, Definition> subDefinitionMap = body.getDefinitionMap();
for (final Definition definition : subDefinitionMap.values()) {
final String name = definition.getIdentifier().getName();
if (definitions.hasDefinition(name)) {
final Definition localDefinition = definitions.getDefinition(name);
localDefinition.getIdentifier().getLocation().reportSemanticError(MessageFormat.format(LOCALINHERTANCECOLLISSION, definition.getIdentifier().getDisplayName(), definition.getMyScope().getFullName()));
} else if (extendsGainedDefinitions.containsKey(name)) {
final Definition previousDefinition = extendsGainedDefinitions.get(name);
if (!previousDefinition.equals(definition)) {
// it is not the same definition inherited on two paths
if (this.equals(previousDefinition.getMyScope())) {
previousDefinition.getLocation().reportSemanticError(MessageFormat.format(LOCALINHERTANCECOLLISSION, previousDefinition.getIdentifier().getDisplayName(), definition.getMyScope().getFullName()));
definition.getIdentifier().getLocation().reportSemanticWarning(MessageFormat.format(INHERITEDLOCATION, definition.getIdentifier().getDisplayName()));
} else if (identifier != null && identifier.getLocation() != null) {
identifier.getLocation().reportSemanticError(MessageFormat.format(INHERITANCECOLLISSION, definition.getIdentifier().getDisplayName(), definition.getMyScope().getFullName(), previousDefinition.getMyScope().getFullName()));
definition.getIdentifier().getLocation().reportSingularSemanticWarning(MessageFormat.format(INHERITEDDEFINITIONLOCATION, definition.getIdentifier().getDisplayName(), definition.getMyScope().getFullName()));
previousDefinition.getIdentifier().getLocation().reportSingularSemanticWarning(MessageFormat.format(INHERITEDDEFINITIONLOCATION, previousDefinition.getIdentifier().getDisplayName(), previousDefinition.getMyScope().getFullName()));
}
}
} else {
extendsGainedDefinitions.put(name, definition);
if (!definition.getMyScope().getModuleScope().equals(parentScope.getModuleScope())) {
if (parentScope.hasAssignmentWithId(timestamp, definition.getIdentifier())) {
if (identifier != null && identifier.getLocation() != null) {
identifier.getLocation().reportSemanticError(MessageFormat.format(HIDINGSCOPEELEMENT, definition.getIdentifier().getDisplayName()));
final List<ISubReference> subReferences = new ArrayList<ISubReference>();
subReferences.add(new FieldSubReference(definition.getIdentifier()));
final Reference reference = new Reference(null, subReferences);
final Assignment assignment = parentScope.getAssBySRef(timestamp, reference);
if (assignment != null && assignment.getLocation() != null) {
assignment.getLocation().reportSingularSemanticError(MessageFormat.format(HIDDENSCOPEELEMENT, definition.getIdentifier().getDisplayName()));
}
definition.getIdentifier().getLocation().reportSingularSemanticWarning(MessageFormat.format(INHERITEDDEFINITIONLOCATION, definition.getIdentifier().getDisplayName(), definition.getMyScope().getFullName()));
}
} else if (parentScope.isValidModuleId(definition.getIdentifier())) {
definition.getLocation().reportSingularSemanticWarning(MessageFormat.format(HIDINGMODULEIDENTIFIER, definition.getIdentifier().getDisplayName()));
}
}
}
}
}
}
use of org.eclipse.titan.designer.AST.TTCN3.definitions.Definitions in project titan.EclipsePlug-ins by eclipse.
the class DefinitionContainer method checkUniqueness.
public void checkUniqueness() {
definitionMap.clear();
for (final Definition definition : definitions) {
final String definitionName = definition.getIdentifier().getName();
if (definitionMap.containsKey(definitionName)) {
definitionMap.get(definitionName).getIdentifier().getLocation().reportSingularSemanticError(MessageFormat.format(CompFieldMap.DUPLICATEFIELDNAMEFIRST, definition.getIdentifier().getDisplayName()));
definition.getIdentifier().getLocation().reportSemanticError(MessageFormat.format(CompFieldMap.DUPLICATEFIELDNAMEREPEATED, definition.getIdentifier().getDisplayName()));
} else {
definitionMap.put(definitionName, definition);
}
}
}
use of org.eclipse.titan.designer.AST.TTCN3.definitions.Definitions in project titan.EclipsePlug-ins by eclipse.
the class ImportSelectionDialog method organizeImportsEdit.
/**
* Organize the imports according to the global preferences. If set,
* <ul>
* <li>Add imports necessary for missing references,</li>
* <li>Remove unused imports,</li>
* <li>Sort the import statements.</li>
* </ul>
* <p>
* These changes are not applied in the function, just collected in a
* <link>MultiTextEdit</link>, which is then returned.
* </p>
* TODO: notice and handle ambiguous references
*
* @param module
* The module which import statements are to organize.
* @param document
* The document that contains the module.
*
* @return The edit, which contains the proper changes.
*/
private static MultiTextEdit organizeImportsEdit(final TTCN3Module module, final IDocument document) throws BadLocationException {
final IProject prj = module.getProject();
final String doc = document.get();
final MultiTextEdit insertEdit = new MultiTextEdit();
final MultiTextEdit removeEdit = new MultiTextEdit();
final List<ImportText> newImports = new ArrayList<ImportText>();
final List<ImportText> importsKept = new ArrayList<ImportText>();
boolean needSorting = false;
if (addImports) {
// register the new needed imports
final Set<String> importNamesAdded = new HashSet<String>();
for (final Reference ref : module.getMissingReferences()) {
final Location missLoc = findReferenceInProject(ref, prj);
if (missLoc != null) {
final IFile file = (IFile) missLoc.getFile();
final ProjectSourceParser parser = GlobalParser.getProjectSourceParser(file.getProject());
final Module addMod = parser.containedModule(file);
final String importName = addMod.getIdentifier().getTtcnName();
if (!importNamesAdded.contains(importName)) {
final StringBuilder impText = new StringBuilder("import from ").append(importName).append(" all;");
if (importChangeMethod.equals(OrganizeImportPreferencePage.COMMENT_THEM)) {
impText.append(" // Added automatically to resolve ").append(ref.getDisplayName());
}
newImports.add(new ImportText(importName, impText.toString() + NEWLINE));
importNamesAdded.add(importName);
if (reportDebug) {
final StringBuilder sb = new StringBuilder("For ").append(ref.getDisplayName()).append(": ");
sb.append(impText.toString());
TITANDebugConsole.println(sb.toString());
}
}
}
}
if (sortImports && !newImports.isEmpty()) {
needSorting = true;
}
}
if (!needSorting && sortImports) {
// are the imports already sorted ?
final List<ImportModule> oldImports = module.getImports();
for (int size = oldImports.size(), i = 0; i < size - 1 && !needSorting; i++) {
if (oldImports.get(i).getName().compareTo(oldImports.get(i + 1).getName()) > 0) {
needSorting = true;
}
if (oldImports.get(i).getLocation().getOffset() > oldImports.get(i + 1).getLocation().getOffset()) {
needSorting = true;
}
}
if (!needSorting && oldImports.size() > 1) {
// are the import strictly before the definitions ?
final int lastImportOffset = oldImports.get(oldImports.size() - 1).getLocation().getOffset();
final Definitions defs = module.getAssignmentsScope();
if (defs.getNofAssignments() > 0 && !oldImports.isEmpty()) {
for (int i = 0, size = defs.getNofAssignments(); i < size; ++i) {
final int temp = defs.getAssignmentByIndex(i).getLocation().getOffset();
if (temp < lastImportOffset) {
needSorting = true;
}
}
}
}
}
if (needSorting || removeImports) {
// remove the imports not needed, or every if sorting is required
for (final ImportModule m : module.getImports()) {
final Location delImp = m.getLocation();
final IRegion startLineRegion = document.getLineInformationOfOffset(delImp.getOffset());
final IRegion endLineRegion = document.getLineInformationOfOffset(delImp.getEndOffset());
final String delimeter = document.getLineDelimiter(document.getLineOfOffset(delImp.getEndOffset()));
final int delLength = delimeter == null ? 0 : delimeter.length();
if (needSorting || (removeImports && !m.getUsedForImportation())) {
if (reportDebug) {
final MessageConsoleStream stream = TITANDebugConsole.getConsole().newMessageStream();
TITANDebugConsole.println("Removing " + "'" + doc.substring(startLineRegion.getOffset(), endLineRegion.getOffset() + endLineRegion.getLength() + delLength) + "'", stream);
TITANDebugConsole.println("From " + startLineRegion.getOffset() + " till " + ((endLineRegion.getOffset() - startLineRegion.getOffset()) + endLineRegion.getLength() + delLength), stream);
}
if (importChangeMethod.equals(OrganizeImportPreferencePage.COMMENT_THEM)) {
removeEdit.addChild(new InsertEdit(m.getLocation().getOffset(), "/*"));
// hack to handle the semicolon
removeEdit.addChild(new InsertEdit(m.getLocation().getEndOffset() + 1, "*/"));
} else {
removeEdit.addChild(new DeleteEdit(startLineRegion.getOffset(), (endLineRegion.getOffset() - startLineRegion.getOffset()) + endLineRegion.getLength() + delLength));
}
}
if (needSorting && (!removeImports || m.getUsedForImportation())) {
importsKept.add(new ImportText(m.getName(), doc.substring(startLineRegion.getOffset(), endLineRegion.getOffset() + endLineRegion.getLength() + delLength)));
}
}
}
if (!newImports.isEmpty() || (sortImports && needSorting)) {
// always insert at the beginning of the file
final int line = document.getLineOfOffset(module.getAssignmentsScope().getLocation().getOffset());
final IRegion lineRegion = document.getLineInformation(line);
final String delimeter = document.getLineDelimiter(line);
final int delimeterLength = delimeter == null ? 0 : delimeter.length();
final int startPos = lineRegion.getOffset() + lineRegion.getLength() + delimeterLength;
if (sortImports) {
if (needSorting || !newImports.isEmpty()) {
final List<ImportText> results = new ArrayList<ImportText>();
results.addAll(importsKept);
results.addAll(newImports);
Collections.sort(results);
for (final ImportText i : results) {
insertEdit.addChild(new InsertEdit(startPos, i.getText()));
}
}
} else {
Collections.sort(newImports);
for (final ImportText i : newImports) {
insertEdit.addChild(new InsertEdit(startPos, i.getText()));
}
}
}
final MultiTextEdit resultEdit = new MultiTextEdit();
resultEdit.addChild(insertEdit);
resultEdit.addChild(removeEdit);
return resultEdit;
}
Aggregations