1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jdk.internal.clang; 25 26 import clang.Index_h.CXType; 27 import java.foreign.Scope; 28 import java.foreign.memory.Pointer; 29 30 public class Type { 31 private final CXType type; 32 Type(CXType type) { 33 this.type = type; 34 } 35 36 // Function Types 37 public boolean isVariadic() { 38 return LibClang.lib.clang_isFunctionTypeVariadic(type) != 0; 39 } 40 public Type resultType() { 41 return new Type(LibClang.lib.clang_getResultType(type)); 42 } 43 public int numberOfArgs() { 44 return LibClang.lib.clang_getNumArgTypes(type); 45 } 46 public Type argType(int idx) { 47 return new Type(LibClang.lib.clang_getArgType(type, idx)); 48 } 49 private int getCallingConvention0() { 50 return LibClang.lib.clang_getFunctionTypeCallingConv(type); 51 } 52 53 public CallingConvention getCallingConvention() { 54 int v = getCallingConvention0(); 55 return CallingConvention.valueOf(v); 56 } 57 58 // Pointer type 59 public Type getPointeeType() { 60 return new Type(LibClang.lib.clang_getPointeeType(type)); 61 } 62 63 // array/vector type 64 public Type getElementType() { 65 return new Type(LibClang.lib.clang_getElementType(type)); 66 } 67 public long getNumberOfElements() { 68 return LibClang.lib.clang_getNumElements(type); 69 } 70 71 // Struct/RecordType 72 private long getOffsetOf0(String fieldName) { 73 try (Scope s = Scope.globalScope().fork()) { 74 Pointer<Byte> cfname = s.allocateCString(fieldName); 75 return LibClang.lib.clang_Type_getOffsetOf(type, cfname); 76 } 77 } 78 79 public long getOffsetOf(String fieldName) { 80 long res = getOffsetOf0(fieldName); 81 if(TypeLayoutError.isError(res)) { 82 throw new TypeLayoutError(res, String.format("type: %s, fieldName: %s", this, fieldName)); 83 } 84 return res; 85 } 86 87 // Typedef 88 public Type canonicalType() { 89 return new Type(LibClang.lib.clang_getCanonicalType(type)); 90 } 91 92 public String spelling() { 93 return LibClang.CXStrToString(LibClang.lib.clang_getTypeSpelling(type)); 94 } 95 96 public int kind0() { 97 return type.kind$get(); 98 } 99 100 private long size0() { 101 return LibClang.lib.clang_Type_getSizeOf(type); 102 } 103 104 public long size() { 105 long res = size0(); 106 if(TypeLayoutError.isError(res)) { 107 throw new TypeLayoutError(res, String.format("type: %s", this)); 108 } 109 return res; 110 } 111 112 public TypeKind kind() { 113 int v = kind0(); 114 return TypeKind.valueOf(v); 115 } 116 117 public Cursor getDeclarationCursor() { 118 return new Cursor(LibClang.lib.clang_getTypeDeclaration(type)); 119 } 120 121 @Override 122 public boolean equals(Object other) { 123 if (this == other) { 124 return true; 125 } 126 if (!(other instanceof Type)) { 127 return false; 128 } 129 return LibClang.lib.clang_equalTypes(type, ((Type)other).type) != 0; 130 } 131 132 @Override 133 public int hashCode() { 134 return spelling().hashCode(); 135 } 136 }