-
Notifications
You must be signed in to change notification settings - Fork 26
Add symtype relations of iterables, fix for-each is valid #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+676
−75
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
monticore-grammar/src/main/java/de/monticore/types3/SymTypeRelationsOfIterables.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // (c) https://github.com/MontiCore/monticore | ||
| package de.monticore.types3; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import de.monticore.types.check.SymTypeExpression; | ||
| import de.monticore.types.check.SymTypeOfGenerics; | ||
| import de.se_rwth.commons.logging.Log; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Relations of SymTypeExpressions that are iterable (e.g., arrays, List, Iterable) | ||
| */ | ||
| public class SymTypeRelationsOfIterables { | ||
|
|
||
| protected static SymTypeRelationsOfIterables delegate; | ||
|
|
||
| /** | ||
| * Whether the type is an array (e.g., int[]) | ||
| * | ||
| * @see SymTypeExpression#isArrayType() | ||
| */ | ||
| public static boolean isArrayType(SymTypeExpression type) { | ||
| return getDelegate()._isArrayType(type); | ||
| } | ||
|
|
||
| protected boolean _isArrayType(SymTypeExpression type) { | ||
| return type.isArrayType(); | ||
| } | ||
|
|
||
| /** | ||
| * Whether the type is of the raw type Iterable (e.g., java.lang.Iterable) | ||
| * | ||
| * @see SymTypeExpression#isArrayType() | ||
| */ | ||
| protected static boolean isOfTypeIterable(SymTypeExpression type) { | ||
schmalzing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return getDelegate()._isOfTypeIterable(type); | ||
| } | ||
|
|
||
| protected boolean _isOfTypeIterable(SymTypeExpression type) { | ||
| if (!type.isGenericType()) { | ||
| return false; | ||
| } | ||
| SymTypeOfGenerics generic = type.asGenericType(); | ||
| String name = generic.getTypeConstructorFullName(); | ||
| if (!name.equals("Iterable") && !name.equals("java.lang.Iterable")) { | ||
| return false; | ||
| } | ||
| return generic.sizeArguments() == 1; | ||
| } | ||
|
|
||
| /** | ||
| * Whether the type if of or a subtype of the raw type Iterable (e.g., java.lang.Iterable) | ||
| * | ||
| * @see SymTypeExpression#isArrayType() | ||
| */ | ||
| protected static boolean isOfTypeIterableOrSubType(SymTypeExpression type) { | ||
| return getDelegate()._isOfTypeIterable(type); | ||
| } | ||
|
|
||
| protected boolean _isOfTypeIterableOrSubType(SymTypeExpression type) { | ||
| if (_isOfTypeIterable(type)) { | ||
| return true; | ||
| } else { | ||
| for (SymTypeExpression superType : SymTypeRelations.getNominalSuperTypes(type)) { | ||
| if (_isOfTypeIterableOrSubType(superType)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * @return an {@code Optional} of the Element type of the iterable | ||
| * or an {@link Optional#empty()} if the type is not iterable | ||
| */ | ||
| public static Optional<SymTypeExpression> getIterationType(SymTypeExpression type) { | ||
| return getDelegate()._getIterationType(type); | ||
| } | ||
|
|
||
| protected Optional<SymTypeExpression> _getIterationType(SymTypeExpression type) { | ||
| Optional<SymTypeExpression> iterationType = _getIterationTypeOfArray(type); | ||
|
|
||
| if (iterationType.isPresent()) { | ||
| return iterationType; | ||
| } | ||
|
|
||
| return _getIterationTypeOfTypeIterable(type); | ||
| } | ||
|
|
||
| protected Optional<SymTypeExpression> _getIterationTypeOfArray(SymTypeExpression type) { | ||
| if (_isArrayType(type)) { | ||
| return Optional.of(type.asArrayType().cloneWithLessDim(1)); | ||
| } else { | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
|
|
||
| protected Optional<SymTypeExpression> _getIterationTypeOfTypeIterable(SymTypeExpression type) { | ||
| if (_isOfTypeIterable(type)) { | ||
| return Optional.of(type.asGenericType().getArgument(0)); | ||
| } | ||
| for (SymTypeExpression superType : SymTypeRelations.getNominalSuperTypes(type)) { | ||
| Optional<SymTypeExpression> iterationType = _getIterationTypeOfTypeIterable(superType); | ||
| if (iterationType.isPresent()) { | ||
| return iterationType; | ||
| } | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| // static delegate | ||
|
|
||
| public static void init() { | ||
| Log.trace("init default IterableSymTypeRelations", "TypeCheck setup"); | ||
| setDelegate(new SymTypeRelationsOfIterables()); | ||
| } | ||
|
|
||
| public static void reset() { | ||
| SymTypeRelationsOfIterables.delegate = null; | ||
| } | ||
|
|
||
| protected static void setDelegate(SymTypeRelationsOfIterables newDelegate) { | ||
| SymTypeRelationsOfIterables.delegate = Preconditions.checkNotNull(newDelegate); | ||
| } | ||
|
|
||
| protected static SymTypeRelationsOfIterables getDelegate() { | ||
| if (SymTypeRelationsOfIterables.delegate == null) { | ||
| init(); | ||
| } | ||
| return SymTypeRelationsOfIterables.delegate; | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.