blob: 259378df959ad2b1f5250420421af2ecc23c7295 [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) {
71 case kPrimBoolean:
72 case kPrimByte: return 1;
73 case kPrimChar:
74 case kPrimShort: return 2;
75 case kPrimInt:
76 case kPrimFloat: return 4;
77 case kPrimLong:
78 case kPrimDouble: return 8;
79 case kPrimNot: return sizeof(Object*);
80 default:
81 LOG(FATAL) << "Invalid type " << static_cast<int>(type);
82 return 0;
83 }
84 }
85
86 static size_t FieldSize(Type type) {
87 return ComponentSize(type) <= 4 ? 4 : 8;
88 }
89
90 static char DescriptorChar(Type type) {
91 switch (type) {
92 case kPrimBoolean:
93 return 'Z';
94 case kPrimByte:
95 return 'B';
96 case kPrimChar:
97 return 'C';
98 case kPrimShort:
99 return 'S';
100 case kPrimInt:
101 return 'I';
102 case kPrimFloat:
103 return 'J';
104 case kPrimLong:
105 return 'J';
106 case kPrimDouble:
107 return 'D';
108 default:
109 LOG(FATAL) << "Primitive char conversion on invalid type " << static_cast<int>(type);
110 return 0;
111 }
112 }
113
114 private:
115 DISALLOW_IMPLICIT_CONSTRUCTORS(Primitive);
116};
117
118} // namespace art
119
120#endif // ART_SRC_PRIMITIVE_H_