1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import java.util.HashMap; 18 19 public class JType { 20 21 String baseType; 22 boolean isArray; 23 boolean isClass; 24 boolean isString; 25 26 static HashMap<CType,JType> typeMapping = new HashMap<CType,JType>(); 27 static HashMap<CType,JType> arrayTypeMapping = new HashMap<CType,JType>(); 28 29 static { 30 // Primitive types typeMapping.put(new CType("GLbitfield"), new JType("int"))31 typeMapping.put(new CType("GLbitfield"), new JType("int")); typeMapping.put(new CType("GLboolean"), new JType("boolean"))32 typeMapping.put(new CType("GLboolean"), new JType("boolean")); typeMapping.put(new CType("GLclampf"), new JType("float"))33 typeMapping.put(new CType("GLclampf"), new JType("float")); typeMapping.put(new CType("GLclampx"), new JType("int"))34 typeMapping.put(new CType("GLclampx"), new JType("int")); typeMapping.put(new CType("GLenum"), new JType("int"))35 typeMapping.put(new CType("GLenum"), new JType("int")); typeMapping.put(new CType("GLfloat"), new JType("float"))36 typeMapping.put(new CType("GLfloat"), new JType("float")); typeMapping.put(new CType("GLfixed"), new JType("int"))37 typeMapping.put(new CType("GLfixed"), new JType("int")); typeMapping.put(new CType("GLint"), new JType("int"))38 typeMapping.put(new CType("GLint"), new JType("int")); typeMapping.put(new CType("GLintptr"), new JType("int"))39 typeMapping.put(new CType("GLintptr"), new JType("int")); typeMapping.put(new CType("GLshort"), new JType("short"))40 typeMapping.put(new CType("GLshort"), new JType("short")); typeMapping.put(new CType("GLsizei"), new JType("int"))41 typeMapping.put(new CType("GLsizei"), new JType("int")); typeMapping.put(new CType("GLsizeiptr"), new JType("int"))42 typeMapping.put(new CType("GLsizeiptr"), new JType("int")); typeMapping.put(new CType("GLubyte"), new JType("byte"))43 typeMapping.put(new CType("GLubyte"), new JType("byte")); typeMapping.put(new CType("GLuint"), new JType("int"))44 typeMapping.put(new CType("GLuint"), new JType("int")); typeMapping.put(new CType("void"), new JType("void"))45 typeMapping.put(new CType("void"), new JType("void")); typeMapping.put(new CType("GLubyte", true, true), new JType("String", false, false))46 typeMapping.put(new CType("GLubyte", true, true), new JType("String", false, false)); typeMapping.put(new CType("char", false, true), new JType("byte"))47 typeMapping.put(new CType("char", false, true), new JType("byte")); typeMapping.put(new CType("char", true, true), new JType("String", false, false))48 typeMapping.put(new CType("char", true, true), new JType("String", false, false)); typeMapping.put(new CType("int"), new JType("int"))49 typeMapping.put(new CType("int"), new JType("int")); 50 51 // Untyped pointers map to untyped Buffers typeMapping.put(new CType("GLvoid", true, true), new JType("java.nio.Buffer", true, false))52 typeMapping.put(new CType("GLvoid", true, true), 53 new JType("java.nio.Buffer", true, false)); typeMapping.put(new CType("GLvoid", false, true), new JType("java.nio.Buffer", true, false))54 typeMapping.put(new CType("GLvoid", false, true), 55 new JType("java.nio.Buffer", true, false)); typeMapping.put(new CType("void", false, true), new JType("java.nio.Buffer", true, false))56 typeMapping.put(new CType("void", false, true), 57 new JType("java.nio.Buffer", true, false)); typeMapping.put(new CType("GLeglImageOES", false, false), new JType("java.nio.Buffer", true, false))58 typeMapping.put(new CType("GLeglImageOES", false, false), 59 new JType("java.nio.Buffer", true, false)); 60 61 // Typed pointers map to typed Buffers typeMapping.put(new CType("GLboolean", false, true), new JType("java.nio.IntBuffer", true, false))62 typeMapping.put(new CType("GLboolean", false, true), 63 new JType("java.nio.IntBuffer", true, false)); typeMapping.put(new CType("GLenum", false, true), new JType("java.nio.IntBuffer", true, false))64 typeMapping.put(new CType("GLenum", false, true), 65 new JType("java.nio.IntBuffer", true, false)); typeMapping.put(new CType("GLfixed", false, true), new JType("java.nio.IntBuffer", true, false))66 typeMapping.put(new CType("GLfixed", false, true), 67 new JType("java.nio.IntBuffer", true, false)); typeMapping.put(new CType("GLfixed", true, true), new JType("java.nio.IntBuffer", true, false))68 typeMapping.put(new CType("GLfixed", true, true), 69 new JType("java.nio.IntBuffer", true, false)); typeMapping.put(new CType("GLfloat", false, true), new JType("java.nio.FloatBuffer", true, false))70 typeMapping.put(new CType("GLfloat", false, true), 71 new JType("java.nio.FloatBuffer", true, false)); typeMapping.put(new CType("GLfloat", true, true), new JType("java.nio.FloatBuffer", true, false))72 typeMapping.put(new CType("GLfloat", true, true), 73 new JType("java.nio.FloatBuffer", true, false)); typeMapping.put(new CType("GLint", false, true), new JType("java.nio.IntBuffer", true, false))74 typeMapping.put(new CType("GLint", false, true), 75 new JType("java.nio.IntBuffer", true, false)); typeMapping.put(new CType("GLint", true, true), new JType("java.nio.IntBuffer", true, false))76 typeMapping.put(new CType("GLint", true, true), 77 new JType("java.nio.IntBuffer", true, false)); typeMapping.put(new CType("GLsizei", false, true), new JType("java.nio.IntBuffer", true, false))78 typeMapping.put(new CType("GLsizei", false, true), 79 new JType("java.nio.IntBuffer", true, false)); typeMapping.put(new CType("GLuint", false, true), new JType("java.nio.IntBuffer", true, false))80 typeMapping.put(new CType("GLuint", false, true), 81 new JType("java.nio.IntBuffer", true, false)); typeMapping.put(new CType("GLuint", true, true), new JType("java.nio.IntBuffer", true, false))82 typeMapping.put(new CType("GLuint", true, true), 83 new JType("java.nio.IntBuffer", true, false)); typeMapping.put(new CType("GLshort", true, true), new JType("java.nio.ShortBuffer", true, false))84 typeMapping.put(new CType("GLshort", true, true), 85 new JType("java.nio.ShortBuffer", true, false)); 86 87 // Typed pointers map to arrays + offsets arrayTypeMapping.put(new CType("char", false, true), new JType("byte", false, true))88 arrayTypeMapping.put(new CType("char", false, true), 89 new JType("byte", false, true)); arrayTypeMapping.put(new CType("GLboolean", false, true), new JType("boolean", false, true))90 arrayTypeMapping.put(new CType("GLboolean", false, true), 91 new JType("boolean", false, true)); arrayTypeMapping.put(new CType("GLenum", false, true), new JType("int", false, true))92 arrayTypeMapping.put(new CType("GLenum", false, true), new JType("int", false, true)); arrayTypeMapping.put(new CType("GLfixed", true, true), new JType("int", false, true))93 arrayTypeMapping.put(new CType("GLfixed", true, true), new JType("int", false, true)); arrayTypeMapping.put(new CType("GLfixed", false, true), new JType("int", false, true))94 arrayTypeMapping.put(new CType("GLfixed", false, true), new JType("int", false, true)); arrayTypeMapping.put(new CType("GLfloat", false, true), new JType("float", false, true))95 arrayTypeMapping.put(new CType("GLfloat", false, true), new JType("float", false, true)); arrayTypeMapping.put(new CType("GLfloat", true, true), new JType("float", false, true))96 arrayTypeMapping.put(new CType("GLfloat", true, true), new JType("float", false, true)); arrayTypeMapping.put(new CType("GLint", false, true), new JType("int", false, true))97 arrayTypeMapping.put(new CType("GLint", false, true), new JType("int", false, true)); arrayTypeMapping.put(new CType("GLint", true, true), new JType("int", false, true))98 arrayTypeMapping.put(new CType("GLint", true, true), new JType("int", false, true)); arrayTypeMapping.put(new CType("GLshort", true, true), new JType("short", false, true))99 arrayTypeMapping.put(new CType("GLshort", true, true), new JType("short", false, true)); arrayTypeMapping.put(new CType("GLsizei", false, true), new JType("int", false, true))100 arrayTypeMapping.put(new CType("GLsizei", false, true), new JType("int", false, true)); arrayTypeMapping.put(new CType("GLsizei", true, true), new JType("int", false, true))101 arrayTypeMapping.put(new CType("GLsizei", true, true), new JType("int", false, true)); arrayTypeMapping.put(new CType("GLuint", false, true), new JType("int", false, true))102 arrayTypeMapping.put(new CType("GLuint", false, true), new JType("int", false, true)); arrayTypeMapping.put(new CType("GLuint", true, true), new JType("int", false, true))103 arrayTypeMapping.put(new CType("GLuint", true, true), new JType("int", false, true)); arrayTypeMapping.put(new CType("GLintptr"), new JType("int", false, true))104 arrayTypeMapping.put(new CType("GLintptr"), new JType("int", false, true)); arrayTypeMapping.put(new CType("GLsizeiptr"), new JType("int", false, true))105 arrayTypeMapping.put(new CType("GLsizeiptr"), new JType("int", false, true)); 106 } 107 JType()108 public JType() { 109 } 110 JType(String primitiveTypeName)111 public JType(String primitiveTypeName) { 112 this.baseType = primitiveTypeName; 113 this.isClass = false; 114 this.isArray = false; 115 } 116 JType(String primitiveTypeName, boolean isClass, boolean isArray)117 public JType(String primitiveTypeName, boolean isClass, boolean isArray) { 118 this.baseType = primitiveTypeName; 119 this.isClass = isClass; 120 this.isArray = isArray; 121 } 122 getBaseType()123 public String getBaseType() { 124 return baseType; 125 } 126 127 @Override toString()128 public String toString() { 129 return baseType + (isArray ? "[]" : ""); 130 } 131 isArray()132 public boolean isArray() { 133 return isArray; 134 } 135 isClass()136 public boolean isClass() { 137 return isClass; 138 } 139 isString()140 public boolean isString() { 141 return baseType.equals("String"); 142 } 143 isPrimitive()144 public boolean isPrimitive() { 145 return !isClass() && !isArray(); 146 } 147 isVoid()148 public boolean isVoid() { 149 return baseType.equals("void"); 150 } 151 isBuffer()152 public boolean isBuffer() { 153 return baseType.indexOf("Buffer") != -1; 154 } 155 isTypedBuffer()156 public boolean isTypedBuffer() { 157 return !baseType.equals("java.nio.Buffer") && 158 (baseType.indexOf("Buffer") != -1); 159 } 160 convert(CType ctype, boolean useArray)161 public static JType convert(CType ctype, boolean useArray) { 162 JType javaType = null; 163 if (useArray) { 164 javaType = arrayTypeMapping.get(ctype); 165 } 166 if (javaType == null) { 167 javaType = typeMapping.get(ctype); 168 } 169 if (javaType == null) { 170 throw new RuntimeException("Unsupported C type: " + ctype); 171 } 172 return javaType; 173 } 174 } 175