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