blob: 240892f2ddce547bf5a74a04b4aaddbddf34d75b [file] [log] [blame]
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001/*
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
25namespace art {
26
27class Object;
28
29class 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 Rogers169c9a72011-11-13 20:13:17 -080071 case kPrimVoid: return 0;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070072 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
91 static char DescriptorChar(Type type) {
92 switch (type) {
93 case kPrimBoolean:
94 return 'Z';
95 case kPrimByte:
96 return 'B';
97 case kPrimChar:
98 return 'C';
99 case kPrimShort:
100 return 'S';
101 case kPrimInt:
102 return 'I';
103 case kPrimFloat:
104 return 'J';
105 case kPrimLong:
106 return 'J';
107 case kPrimDouble:
108 return 'D';
109 default:
110 LOG(FATAL) << "Primitive char conversion on invalid type " << static_cast<int>(type);
111 return 0;
112 }
113 }
114
115 private:
116 DISALLOW_IMPLICIT_CONSTRUCTORS(Primitive);
117};
118
119} // namespace art
120
121#endif // ART_SRC_PRIMITIVE_H_