Skip to content

Commit

Permalink
Merge branch 'refactorSymtypeRelations' into 'dev'
Browse files Browse the repository at this point in the history
better messages in TypeCheck

See merge request monticore/monticore!859
  • Loading branch information
SE-FDr committed Aug 2, 2023
2 parents 5c9c566 + 4067bb5 commit 2371162
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.collect.Lists;
import de.monticore.symbols.basicsymbols._symboltable.IBasicSymbolsGlobalScope;
import de.monticore.symbols.basicsymbols._symboltable.TypeSymbol;
import de.monticore.symboltable.modifiers.AccessModifier;

import java.util.List;
import java.util.Collections;
Expand Down Expand Up @@ -61,6 +62,7 @@ protected TypeSymbol createPrimitive(String name){
.setEnclosingScope(globalScope())
.setFullName(name)
.setSpannedScope(scope())
.setAccessModifier(AccessModifier.ALL_INCLUSION)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ public SymTypeOfFunction getFunctionType() {
public AccessModifier getAccessModifier() {
// supporting legacy source code...
if(accessModifier == null) {
Log.info("AccessModifier of function '"
Log.trace("AccessModifier of function '"
+ getFullName() + "' was not set (null)",
"BasicSymbols");
return AccessModifier.ALL_INCLUSION;
accessModifier = AccessModifier.ALL_INCLUSION;
}
return accessModifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ public SymTypeExpression getSuperClass() {
public AccessModifier getAccessModifier() {
// supporting legacy source code...
if(accessModifier == null) {
Log.info("AccessModifier of type '"
Log.trace("AccessModifier of type '"
+ getFullName() + "' was not set (null)",
"BasicSymbols");
return AccessModifier.ALL_INCLUSION;
accessModifier = AccessModifier.ALL_INCLUSION;
}
return accessModifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public boolean deepEquals(TypeVarSymbol other) {
public AccessModifier getAccessModifier() {
// supporting legacy source code...
if(accessModifier == null) {
Log.info("AccessModifier of type variable '"
Log.trace("AccessModifier of type variable '"
+ getFullName() + "' was not set (null)",
"BasicSymbols");
return AccessModifier.ALL_INCLUSION;
accessModifier = AccessModifier.ALL_INCLUSION;
}
return accessModifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public void replaceTypeVariables(Map<TypeVarSymbol, SymTypeExpression> replaceMa
public AccessModifier getAccessModifier() {
// supporting legacy source code...
if(accessModifier == null) {
Log.info("AccessModifier of variable '"
Log.trace("AccessModifier of variable '"
+ getFullName() + "' was not set (null)",
"BasicSymbols");
return AccessModifier.ALL_INCLUSION;
accessModifier = AccessModifier.ALL_INCLUSION;
}
return accessModifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,8 @@ protected Optional<VariableSymbol> resolveVariableLocally(
Optional<VariableSymbol> resolved = scope.resolveVariable(
name,
accessModifier,
predicate.and(v -> v.getEnclosingScope() == scope)
predicate.and(getIsLocalSymbolPredicate(scope))
);
resolved = filterNonLocalSymbols(resolved, scope);
return resolved;
}

Expand All @@ -283,9 +282,8 @@ protected List<FunctionSymbol> resolveFunctionLocally(
false,
name,
accessModifier,
predicate.and(f -> f.getEnclosingScope() == scope)
predicate.and(getIsLocalSymbolPredicate(scope))
);
resolved = filterNonLocalSymbols(resolved, scope);
return resolved;
}

Expand All @@ -302,9 +300,8 @@ protected Optional<TypeSymbol> resolveTypeLocally(
false,
name,
accessModifier,
predicate.and(t -> t.getEnclosingScope() == scope)
predicate.and(getIsLocalSymbolPredicate(scope))
);
resolved = filterNonLocalSymbols(resolved, scope);
// prefer variables to concrete types in the same scope,
// e.g., class A<B>{class B{} B b = new B();} is not valid Java
if (resolved.stream().anyMatch(getTypeDispatcher()::isTypeVar)) {
Expand All @@ -323,44 +320,20 @@ protected Optional<TypeSymbol> resolveTypeLocally(
* Even more legacy code workarounds:
* filter out anything that is not in the exact scope
* due to questionable resolve strategy overrides in Scope classes...
* Changes the original list!!!
*/
protected <E extends ISymbol> List<E> filterNonLocalSymbols(
List<E> symbols,
IScope scope
) {
Iterator<E> sItr = symbols.iterator();
while (sItr.hasNext()) {
ISymbol symbol = sItr.next();
if (symbol.getEnclosingScope() != scope) {
Log.info("filtered symbol '"
+ symbol.getFullName() + "' as it was resolved "
protected Predicate<ISymbol> getIsLocalSymbolPredicate(IScope localScope) {
return s -> {
if (s.getEnclosingScope() != localScope) {
Log.trace("filtered symbol '"
+ s.getFullName() + "' as it was resolved "
+ "in a different scope, even though "
+ "\"resolve[...]Locally[...]\" was used",
LOG_NAME
);
sItr.remove();
return false;
}
}
return symbols;
}

protected <E extends ISymbol> Optional<E> filterNonLocalSymbols(
Optional<E> symbolOpt,
IScope scope
) {
if (symbolOpt.isPresent()) {
if (symbolOpt.get().getEnclosingScope() != scope) {
Log.info("filtered symbol '"
+ symbolOpt.get().getFullName() + "' as it was resolved "
+ "in a different scope, even though "
+ "\"resolve[...]Locally[...]\" was used",
LOG_NAME
);
symbolOpt = Optional.empty();
}
}
return symbolOpt;
return true;
};
}

protected BasicSymbolsTypeDispatcher getTypeDispatcher() {
Expand Down

0 comments on commit 2371162

Please sign in to comment.