diff graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/Utils.java @ 9220:97ad6d3e7557

Codegen API changes. Executed child nodes are now defined using @NodeChildren instead of fields.
author Christian Humer <christian.humer@gmail.com>
date Sat, 20 Apr 2013 12:16:22 +0200
parents bd067a48a9c2
children 6d92fdf1c999
line wrap: on
line diff
--- a/graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/Utils.java	Tue Apr 16 11:03:40 2013 +0200
+++ b/graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/Utils.java	Sat Apr 20 12:16:22 2013 +0200
@@ -32,6 +32,7 @@
 import javax.lang.model.util.*;
 
 import com.oracle.truffle.codegen.processor.ast.*;
+import com.oracle.truffle.codegen.processor.ast.CodeTypeMirror.DeclaredCodeTypeMirror;
 import com.oracle.truffle.codegen.processor.compiler.*;
 
 /**
@@ -63,17 +64,20 @@
         return types;
     }
 
+    public static DeclaredType getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs) {
+        return new DeclaredCodeTypeMirror(typeElem, Arrays.asList(typeArgs));
+    }
+
     public static List<AnnotationMirror> collectAnnotations(ProcessorContext context, AnnotationMirror markerAnnotation, String elementName, Element element,
                     Class<? extends Annotation> annotationClass) {
-        List<AnnotationMirror> result = Utils.getAnnotationValueList(AnnotationMirror.class, markerAnnotation, elementName);
+        List<AnnotationMirror> result = new ArrayList<>();
+        if (markerAnnotation != null) {
+            result.addAll(Utils.getAnnotationValueList(AnnotationMirror.class, markerAnnotation, elementName));
+        }
         AnnotationMirror explicit = Utils.findAnnotationMirror(context.getEnvironment(), element, annotationClass);
         if (explicit != null) {
             result.add(explicit);
         }
-
-        for (AnnotationMirror mirror : result) {
-            assert Utils.typeEquals(mirror.getAnnotationType(), context.getType(annotationClass));
-        }
         return result;
     }
 
@@ -319,6 +323,8 @@
                 return getSimpleName(mirror);
             case ERROR:
                 throw new CompileErrorException("Type error " + mirror);
+            case NONE:
+                return "$none";
             default:
                 throw new RuntimeException("Unknown type specified " + mirror + " mirror: " + mirror);
         }
@@ -371,7 +377,7 @@
         return null;
     }
 
-    private static List<Element> getElementHierarchy(Element e) {
+    public static List<Element> getElementHierarchy(Element e) {
         List<Element> elements = new ArrayList<>();
         elements.add(e);
 
@@ -589,7 +595,7 @@
 
         @Override
         public Object visitEnumConstant(VariableElement c, Void p) {
-            return c.getConstantValue();
+            return c;
         }
 
         @Override
@@ -772,11 +778,7 @@
 
     public static Modifier getVisibility(Set<Modifier> modifier) {
         for (Modifier mod : modifier) {
-            if (mod == Modifier.PUBLIC) {
-                return mod;
-            } else if (mod == Modifier.PRIVATE) {
-                return mod;
-            } else if (mod == Modifier.PROTECTED) {
+            if (mod == Modifier.PUBLIC || mod == Modifier.PRIVATE || mod == Modifier.PROTECTED) {
                 return mod;
             }
         }
@@ -813,4 +815,20 @@
         return getQualifiedName(actualType).equals("java.lang.Object");
     }
 
+    public static boolean isFieldAccessible(Element element, VariableElement variable) {
+        TypeElement type = Utils.findNearestEnclosingType(element);
+        TypeElement varType = Utils.findNearestEnclosingType(variable);
+
+        while (type != null) {
+            if (typeEquals(type.asType(), varType.asType())) {
+                return true;
+            }
+            if (type.getSuperclass() != null) {
+                type = Utils.fromTypeMirror(type.getSuperclass());
+            } else {
+                type = null;
+            }
+        }
+        return false;
+    }
 }