blob: eaa04cd054731e2507db2798507f0d60779c3571 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080023#include "base/macros.h"
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070024
25namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026namespace mirror {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070027class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028} // namespace mirror
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070029
30class Primitive {
31 public:
32 enum Type {
33 kPrimNot = 0,
34 kPrimBoolean,
35 kPrimByte,
36 kPrimChar,
37 kPrimShort,
38 kPrimInt,
39 kPrimLong,
40 kPrimFloat,
41 kPrimDouble,
42 kPrimVoid,
43 };
44
45 static Type GetType(char type) {
46 switch (type) {
47 case 'B':
48 return kPrimByte;
49 case 'C':
50 return kPrimChar;
51 case 'D':
52 return kPrimDouble;
53 case 'F':
54 return kPrimFloat;
55 case 'I':
56 return kPrimInt;
57 case 'J':
58 return kPrimLong;
59 case 'S':
60 return kPrimShort;
61 case 'Z':
62 return kPrimBoolean;
63 case 'V':
64 return kPrimVoid;
65 default:
66 return kPrimNot;
67 }
68 }
69
70 static size_t ComponentSize(Type type) {
71 switch (type) {
Ian Rogers169c9a72011-11-13 20:13:17 -080072 case kPrimVoid: return 0;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070073 case kPrimBoolean:
74 case kPrimByte: return 1;
75 case kPrimChar:
76 case kPrimShort: return 2;
77 case kPrimInt:
78 case kPrimFloat: return 4;
79 case kPrimLong:
80 case kPrimDouble: return 8;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 case kPrimNot: return sizeof(mirror::Object*);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070082 default:
83 LOG(FATAL) << "Invalid type " << static_cast<int>(type);
84 return 0;
85 }
86 }
87
88 static size_t FieldSize(Type type) {
89 return ComponentSize(type) <= 4 ? 4 : 8;
90 }
91
Elliott Hughes91250e02011-12-13 22:30:35 -080092 static const char* Descriptor(Type type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070093 switch (type) {
94 case kPrimBoolean:
Elliott Hughes91250e02011-12-13 22:30:35 -080095 return "Z";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070096 case kPrimByte:
Elliott Hughes91250e02011-12-13 22:30:35 -080097 return "B";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070098 case kPrimChar:
Elliott Hughes91250e02011-12-13 22:30:35 -080099 return "C";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700100 case kPrimShort:
Elliott Hughes91250e02011-12-13 22:30:35 -0800101 return "S";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700102 case kPrimInt:
Elliott Hughes91250e02011-12-13 22:30:35 -0800103 return "I";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700104 case kPrimFloat:
Elliott Hughes91250e02011-12-13 22:30:35 -0800105 return "F";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700106 case kPrimLong:
Elliott Hughes91250e02011-12-13 22:30:35 -0800107 return "J";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700108 case kPrimDouble:
Elliott Hughes91250e02011-12-13 22:30:35 -0800109 return "D";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800110 case kPrimVoid:
Elliott Hughes91250e02011-12-13 22:30:35 -0800111 return "V";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700112 default:
113 LOG(FATAL) << "Primitive char conversion on invalid type " << static_cast<int>(type);
Elliott Hughes91250e02011-12-13 22:30:35 -0800114 return NULL;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700115 }
116 }
117
118 private:
119 DISALLOW_IMPLICIT_CONSTRUCTORS(Primitive);
120};
121
Brian Carlstromae826982011-11-09 01:33:42 -0800122std::ostream& operator<<(std::ostream& os, const Primitive::Type& state);
123
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700124} // namespace art
125
126#endif // ART_SRC_PRIMITIVE_H_