blob: ae2e487a964be4e2d0ff7657b478a87628bc015d [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
Elliott Hughes91250e02011-12-13 22:30:35 -080091 static const char* Descriptor(Type type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070092 switch (type) {
93 case kPrimBoolean:
Elliott Hughes91250e02011-12-13 22:30:35 -080094 return "Z";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070095 case kPrimByte:
Elliott Hughes91250e02011-12-13 22:30:35 -080096 return "B";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070097 case kPrimChar:
Elliott Hughes91250e02011-12-13 22:30:35 -080098 return "C";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070099 case kPrimShort:
Elliott Hughes91250e02011-12-13 22:30:35 -0800100 return "S";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700101 case kPrimInt:
Elliott Hughes91250e02011-12-13 22:30:35 -0800102 return "I";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700103 case kPrimFloat:
Elliott Hughes91250e02011-12-13 22:30:35 -0800104 return "F";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700105 case kPrimLong:
Elliott Hughes91250e02011-12-13 22:30:35 -0800106 return "J";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700107 case kPrimDouble:
Elliott Hughes91250e02011-12-13 22:30:35 -0800108 return "D";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800109 case kPrimVoid:
Elliott Hughes91250e02011-12-13 22:30:35 -0800110 return "V";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700111 default:
112 LOG(FATAL) << "Primitive char conversion on invalid type " << static_cast<int>(type);
Elliott Hughes91250e02011-12-13 22:30:35 -0800113 return NULL;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700114 }
115 }
116
117 private:
118 DISALLOW_IMPLICIT_CONSTRUCTORS(Primitive);
119};
120
Brian Carlstromae826982011-11-09 01:33:42 -0800121std::ostream& operator<<(std::ostream& os, const Primitive::Type& state);
122
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700123} // namespace art
124
125#endif // ART_SRC_PRIMITIVE_H_