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