1 /* 2 * Copyright (C) 2011 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 #ifndef ART_RUNTIME_PRIMITIVE_H_ 18 #define ART_RUNTIME_PRIMITIVE_H_ 19 20 #include <sys/types.h> 21 22 #include "base/logging.h" 23 #include "base/macros.h" 24 25 namespace art { 26 namespace mirror { 27 class Object; 28 } // namespace mirror 29 30 class Primitive { 31 public: 32 enum Type { 33 kPrimNot = 0, 34 kPrimBoolean, 35 kPrimByte, 36 kPrimChar, 37 kPrimShort, 38 kPrimInt, 39 kPrimLong, 40 kPrimFloat, 41 kPrimDouble, 42 kPrimVoid, 43 }; 44 45 static Type GetType(char type) { 46 switch (type) { 47 case 'B': 48 return kPrimByte; 49 case 'C': 50 return kPrimChar; 51 case 'D': 52 return kPrimDouble; 53 case 'F': 54 return kPrimFloat; 55 case 'I': 56 return kPrimInt; 57 case 'J': 58 return kPrimLong; 59 case 'S': 60 return kPrimShort; 61 case 'Z': 62 return kPrimBoolean; 63 case 'V': 64 return kPrimVoid; 65 default: 66 return kPrimNot; 67 } 68 } 69 70 static size_t ComponentSize(Type type) { 71 switch (type) { 72 case kPrimVoid: return 0; 73 case kPrimBoolean: 74 case kPrimByte: return 1; 75 case kPrimChar: 76 case kPrimShort: return 2; 77 case kPrimInt: 78 case kPrimFloat: return 4; 79 case kPrimLong: 80 case kPrimDouble: return 8; 81 case kPrimNot: return sizeof(mirror::Object*); 82 default: 83 LOG(FATAL) << "Invalid type " << static_cast<int>(type); 84 return 0; 85 } 86 } 87 88 static size_t FieldSize(Type type) { 89 return ComponentSize(type) <= 4 ? 4 : 8; 90 } 91 92 static const char* Descriptor(Type type) { 93 switch (type) { 94 case kPrimBoolean: 95 return "Z"; 96 case kPrimByte: 97 return "B"; 98 case kPrimChar: 99 return "C"; 100 case kPrimShort: 101 return "S"; 102 case kPrimInt: 103 return "I"; 104 case kPrimFloat: 105 return "F"; 106 case kPrimLong: 107 return "J"; 108 case kPrimDouble: 109 return "D"; 110 case kPrimVoid: 111 return "V"; 112 default: 113 LOG(FATAL) << "Primitive char conversion on invalid type " << static_cast<int>(type); 114 return NULL; 115 } 116 } 117 118 private: 119 DISALLOW_IMPLICIT_CONSTRUCTORS(Primitive); 120 }; 121 122 std::ostream& operator<<(std::ostream& os, const Primitive::Type& state); 123 124 } // namespace art 125 126 #endif // ART_RUNTIME_PRIMITIVE_H_ 127