Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lang] Add @pure annotation on no-side-effect actions.
see #191 Signed-off-by: Stéphane Galland <galland@arakhne.org>
- Loading branch information
1 parent
0224df4
commit 269d35a
Showing
7 changed files
with
283 additions
and
104 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
72 changes: 72 additions & 0 deletions
72
plugins/io.sarl.lang/src/io/sarl/lang/typing/ExtendedXExpressionHelper.java
This file contains 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,72 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2015 the original authors or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sarl.lang.typing; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Singleton; | ||
import org.eclipse.xtext.common.types.JvmOperation; | ||
import org.eclipse.xtext.xbase.XExpression; | ||
import org.eclipse.xtext.xbase.lib.Pure; | ||
import org.eclipse.xtext.xbase.util.XExpressionHelper; | ||
|
||
/** | ||
* Extended helper on expressions. | ||
* | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
* @see SARLXExpressionHelper | ||
*/ | ||
@Singleton | ||
public class ExtendedXExpressionHelper { | ||
|
||
@Inject | ||
private XExpressionHelper expressionHelper; | ||
|
||
private final Pattern pattern; | ||
|
||
/** Construct the helper. | ||
*/ | ||
public ExtendedXExpressionHelper() { | ||
this.pattern = Pattern.compile(XExpressionConstants.SPECIAL_PURE_FUNCTION_NAME_PATTERN); | ||
} | ||
|
||
/** Check if the given operation could be annoted with "@Pure". | ||
* | ||
* @param operation - the operation to test. | ||
* @param body - the body of the operation. | ||
* @return <code>true</code> if one of the components of the given expression has a side effect; | ||
* otherwise <code>false</code>. | ||
* @see Pure | ||
*/ | ||
public boolean isPureOperation(JvmOperation operation, XExpression body) { | ||
if (operation == null || operation.isAbstract() || body == null) { | ||
return false; | ||
} | ||
String name = operation.getSimpleName(); | ||
return (name != null && this.pattern.matcher(name).find()) || !this.expressionHelper.hasSideEffects(body); | ||
} | ||
|
||
} |
This file contains 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
47 changes: 47 additions & 0 deletions
47
plugins/io.sarl.lang/src/io/sarl/lang/typing/XExpressionConstants.java
This file contains 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,47 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2015 the original authors or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sarl.lang.typing; | ||
|
||
import com.google.inject.Singleton; | ||
|
||
/** | ||
* Constants used by the XExpression helpers. | ||
* | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
* @see SARLXExpressionHelper | ||
*/ | ||
@Singleton | ||
public final class XExpressionConstants { | ||
|
||
/** Regular expression pattern that matches the names of functions usually | ||
* considered as pure. | ||
*/ | ||
public static final String SPECIAL_PURE_FUNCTION_NAME_PATTERN = "^(((is)|(get)|(has))[A-Z].*)|(equals)|(hashCode)|(clone)|(toString)$"; //$NON-NLS-1$; | ||
|
||
private XExpressionConstants() { | ||
// | ||
} | ||
|
||
} |
Oops, something went wrong.