blob: b436bd2165604f77c02cd2fb176d0771422fa3d2 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_PRIMITIVE_H_
18#define ART_RUNTIME_PRIMITIVE_H_
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070019
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"
Ian Rogersef7d42f2014-01-06 12:55:46 -080024#include "mirror/object_reference.h"
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070025
26namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027namespace mirror {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070028class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029} // namespace mirror
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070030
31class 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
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
71 static size_t ComponentSize(Type type) {
72 switch (type) {
Ian Rogers169c9a72011-11-13 20:13:17 -080073 case kPrimVoid: return 0;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070074 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;
Ian Rogersef7d42f2014-01-06 12:55:46 -080082 case kPrimNot: return sizeof(mirror::HeapReference<mirror::Object>);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070083 default:
84 LOG(FATAL) << "Invalid type " << static_cast<int>(type);
85 return 0;
86 }
87 }
88
89 static size_t FieldSize(Type type) {
90 return ComponentSize(type) <= 4 ? 4 : 8;
91 }
92
Elliott Hughes91250e02011-12-13 22:30:35 -080093 static const char* Descriptor(Type type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070094 switch (type) {
95 case kPrimBoolean:
Elliott Hughes91250e02011-12-13 22:30:35 -080096 return "Z";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070097 case kPrimByte:
Elliott Hughes91250e02011-12-13 22:30:35 -080098 return "B";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070099 case kPrimChar:
Elliott Hughes91250e02011-12-13 22:30:35 -0800100 return "C";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700101 case kPrimShort:
Elliott Hughes91250e02011-12-13 22:30:35 -0800102 return "S";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700103 case kPrimInt:
Elliott Hughes91250e02011-12-13 22:30:35 -0800104 return "I";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700105 case kPrimFloat:
Elliott Hughes91250e02011-12-13 22:30:35 -0800106 return "F";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700107 case kPrimLong:
Elliott Hughes91250e02011-12-13 22:30:35 -0800108 return "J";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700109 case kPrimDouble:
Elliott Hughes91250e02011-12-13 22:30:35 -0800110 return "D";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800111 case kPrimVoid:
Elliott Hughes91250e02011-12-13 22:30:35 -0800112 return "V";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700113 default:
114 LOG(FATAL) << "Primitive char conversion on invalid type " << static_cast<int>(type);
Elliott Hughes91250e02011-12-13 22:30:35 -0800115 return NULL;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700116 }
117 }
118
119 private:
120 DISALLOW_IMPLICIT_CONSTRUCTORS(Primitive);
121};
122
Brian Carlstromae826982011-11-09 01:33:42 -0800123std::ostream& operator<<(std::ostream& os, const Primitive::Type& state);
124
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700125} // namespace art
126
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700127#endif // ART_RUNTIME_PRIMITIVE_H_