2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 friend struct OperandTypes;
35 static const Type TypeReusable = 1;
36 static const Type TypeInt32 = 2;
38 static const Type TypeMaybeNumber = 0x04;
39 static const Type TypeMaybeString = 0x08;
40 static const Type TypeMaybeNull = 0x10;
41 static const Type TypeMaybeBool = 0x20;
42 static const Type TypeMaybeOther = 0x40;
44 static const Type TypeBits = TypeMaybeNumber | TypeMaybeString | TypeMaybeNull | TypeMaybeBool | TypeMaybeOther;
46 explicit ResultType(Type type)
53 return m_type & TypeReusable;
58 return m_type & TypeInt32;
61 bool definitelyIsNumber()
63 return (m_type & TypeBits) == TypeMaybeNumber;
66 bool definitelyIsString()
68 return (m_type & TypeBits) == TypeMaybeString;
73 return m_type & TypeMaybeNumber;
78 return !mightBeNumber();
81 static ResultType nullType()
83 return ResultType(TypeMaybeNull);
86 static ResultType booleanType()
88 return ResultType(TypeMaybeBool);
91 static ResultType numberType()
93 return ResultType(TypeMaybeNumber);
96 static ResultType numberTypeCanReuse()
98 return ResultType(TypeReusable | TypeMaybeNumber);
101 static ResultType numberTypeCanReuseIsInt32()
103 return ResultType(TypeReusable | TypeInt32 | TypeMaybeNumber);
106 static ResultType stringOrNumberTypeCanReuse()
108 return ResultType(TypeReusable | TypeMaybeNumber | TypeMaybeString);
111 static ResultType stringType()
113 return ResultType(TypeMaybeString);
116 static ResultType unknownType()
118 return ResultType(TypeBits);
121 static ResultType forAdd(ResultType op1, ResultType op2)
123 if (op1.definitelyIsNumber() && op2.definitelyIsNumber())
124 return numberTypeCanReuse();
125 if (op1.definitelyIsString() || op2.definitelyIsString())
127 return stringOrNumberTypeCanReuse();
130 static ResultType forBitOp()
132 return numberTypeCanReuseIsInt32();
141 OperandTypes(ResultType first = ResultType::unknownType(), ResultType second = ResultType::unknownType())
143 // We have to initialize one of the int to ensure that
144 // the entire struct is initialized.
146 m_u.rds.first = first.m_type;
147 m_u.rds.second = second.m_type;
152 ResultType::Type first;
153 ResultType::Type second;
160 return ResultType(m_u.rds.first);
165 return ResultType(m_u.rds.second);
172 static OperandTypes fromInt(int value)
182 #endif // ResultType_h