use of com.oracle.truffle.dsl.processor.TruffleTypes in project graal by oracle.
the class FlatNodeGenFactory method createContextReferenceConstant.
public static CodeVariableElement createContextReferenceConstant(StaticConstants constants, TypeMirror languageType) {
TruffleTypes types = ProcessorContext.getInstance().getTypes();
String constantName = ElementUtils.createConstantName(ElementUtils.getSimpleName(languageType) + "Cref");
TypeElement contextReference = (TypeElement) types.TruffleLanguage_ContextReference.asElement();
DeclaredCodeTypeMirror constantType = new DeclaredCodeTypeMirror(contextReference, Arrays.asList(NodeParser.findContextTypeFromLanguage(languageType)));
return lookupConstant(constants.languageReferences, constantName, (name) -> {
CodeVariableElement newVar = new CodeVariableElement(modifiers(PRIVATE, STATIC, FINAL), constantType, name);
newVar.createInitBuilder().startStaticCall(contextReference.asType(), "create").typeLiteral(languageType).end();
return newVar;
});
}
use of com.oracle.truffle.dsl.processor.TruffleTypes in project graal by oracle.
the class NodeParser method computeSharing.
public static Map<CacheExpression, String> computeSharing(Element templateType, Collection<NodeData> nodes, boolean emitSharingWarnings) {
TruffleTypes types = ProcessorContext.getInstance().getTypes();
Map<SharableCache, Collection<CacheExpression>> groups = computeSharableCaches(nodes);
// compute unnecessary sharing.
Map<String, List<SharableCache>> declaredGroups = new HashMap<>();
for (NodeData node : nodes) {
for (SpecializationData specialization : node.getSpecializations()) {
for (CacheExpression cache : specialization.getCaches()) {
if (cache.isAlwaysInitialized()) {
continue;
}
String group = cache.getSharedGroup();
if (group != null) {
declaredGroups.computeIfAbsent(group, (v) -> new ArrayList<>()).add(new SharableCache(specialization, cache));
}
}
}
}
Map<CacheExpression, String> sharedExpressions = new LinkedHashMap<>();
for (NodeData node : nodes) {
for (SpecializationData specialization : node.getSpecializations()) {
for (CacheExpression cache : specialization.getCaches()) {
if (cache.isAlwaysInitialized()) {
continue;
}
Element declaringElement;
if (node.getTemplateType() instanceof GeneratedElement) {
// generated node
declaringElement = node.getTemplateType().getEnclosingElement();
if (!declaringElement.getKind().isClass() && !declaringElement.getKind().isInterface()) {
throw new AssertionError("Unexpected declared element for generated element: " + declaringElement.toString());
}
} else {
declaringElement = node.getTemplateType();
}
String group = cache.getSharedGroup();
SharableCache sharable = new SharableCache(specialization, cache);
Collection<CacheExpression> expressions = groups.get(sharable);
List<SharableCache> declaredSharing = declaredGroups.get(group);
if (group != null) {
if (declaredSharing.size() <= 1) {
if (!ElementUtils.elementEquals(templateType, declaringElement)) {
// modifiable.
continue;
}
}
if (declaredSharing.size() <= 1 && (expressions == null || expressions.size() <= 1)) {
cache.addError(cache.getSharedGroupMirror(), cache.getSharedGroupValue(), "Could not find any other cached parameter that this parameter could be shared. " + "Cached parameters are only sharable if they declare the same type and initializer expressions and if the specialization only has a single instance. " + "Remove the @%s annotation or make the parameter sharable to resolve this.", types.Cached_Shared.asElement().getSimpleName().toString());
} else {
if (declaredSharing.size() <= 1) {
String error = String.format("No other cached parameters are specified as shared with the group '%s'.", group);
Set<String> similarGroups = new LinkedHashSet<>(declaredGroups.keySet());
similarGroups.remove(group);
List<String> fuzzyMatches = ExportsParser.fuzzyMatch(similarGroups, group, 0.7f);
if (!fuzzyMatches.isEmpty()) {
StringBuilder appendix = new StringBuilder(" Did you mean ");
String sep = "";
for (String string : fuzzyMatches) {
appendix.append(sep);
appendix.append('\'').append(string).append('\'');
sep = ", ";
}
error += appendix.toString() + "?";
}
cache.addError(cache.getSharedGroupMirror(), cache.getSharedGroupValue(), error);
} else {
StringBuilder b = new StringBuilder();
for (SharableCache otherCache : declaredSharing) {
if (cache == otherCache.expression) {
continue;
}
String reason = sharable.equalsWithReason(otherCache);
if (reason == null) {
continue;
}
String signature = formatCacheExpression(otherCache.expression);
b.append(String.format(" - %s : %s%n", signature, reason));
}
if (b.length() != 0) {
cache.addError(cache.getSharedGroupMirror(), cache.getSharedGroupValue(), "Could not share some of the cached parameters in group '%s': %n%sRemove the @%s annotation or resolve the described issues to allow sharing.", group, b.toString(), types.Cached_Shared.asElement().getSimpleName().toString());
} else {
sharedExpressions.put(sharable.expression, group);
}
}
}
} else if (expressions != null && expressions.size() > 1) {
if (emitSharingWarnings) {
/*
* We only emit sharing warnings for the same declaring type, because
* otherwise sharing warnings might not be resolvable if the base type
* is not modifiable.
*/
List<CacheExpression> declaredInExpression = new ArrayList<>();
for (CacheExpression expression : expressions) {
if (ElementUtils.isDeclaredIn(expression.getParameter().getVariableElement(), declaringElement)) {
declaredInExpression.add(expression);
}
}
if (declaredInExpression.size() > 1 && findAnnotationMirror(cache.getParameter().getVariableElement(), types.Cached_Exclusive) == null) {
StringBuilder sharedCaches = new StringBuilder();
Set<String> recommendedGroups = new LinkedHashSet<>();
for (CacheExpression cacheExpression : declaredInExpression) {
if (cacheExpression != cache) {
String signature = formatCacheExpression(cacheExpression);
sharedCaches.append(String.format(" - %s%n", signature));
String otherGroup = cacheExpression.getSharedGroup();
if (otherGroup != null) {
recommendedGroups.add(otherGroup);
}
}
}
String recommendedGroup = recommendedGroups.size() == 1 ? recommendedGroups.iterator().next() : "group";
cache.addWarning("The cached parameter may be shared with: %n%s Annotate the parameter with @%s(\"%s\") or @%s to allow or deny sharing of the parameter.", sharedCaches, types.Cached_Shared.asElement().getSimpleName().toString(), recommendedGroup, types.Cached_Exclusive.asElement().getSimpleName().toString());
}
}
}
}
}
}
return sharedExpressions;
}
use of com.oracle.truffle.dsl.processor.TruffleTypes in project graal by oracle.
the class VerifyCompilationFinalProcessor method checkDimensions.
private boolean checkDimensions(final VariableElement field) {
TruffleTypes types = ProcessorContext.getInstance().getTypes();
final AnnotationMirror compFin = ElementUtils.findAnnotationMirror(field, types.CompilerDirectives_CompilationFinal);
if (compFin != null) {
final int compFinDimensions = ElementUtils.getAnnotationValue(Integer.class, compFin, "dimensions");
final int fieldDimensions = dimension(field.asType());
if (compFinDimensions < -1) {
emitError(field, "@CompilationFinal.dimensions cannot be negative.");
return false;
}
if (compFinDimensions == -1 && fieldDimensions > 0) {
final SuppressWarnings suppressWarnings = field.getAnnotation(SuppressWarnings.class);
if (suppressWarnings != null) {
for (String warning : suppressWarnings.value()) {
if ("VerifyCompilationFinal".equals(warning)) {
return true;
}
}
}
emitWarning(field, "@CompilationFinal.dimensions should be given for an array type.");
return false;
}
if (compFinDimensions > fieldDimensions) {
if (fieldDimensions == 0) {
emitError(field, String.format("Positive @CompilationFinal.dimensions (%d) not allowed for non array type.", compFinDimensions));
} else {
emitError(field, String.format("@CompilationFinal.dimensions (%d) cannot exceed the array's dimensions (%d).", compFinDimensions, fieldDimensions));
}
return false;
}
}
return true;
}
Aggregations