1/*
2 * Copyright (C) 2012, 2013, 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26
27#ifndef ParserModes_h
28#define ParserModes_h
29
30#include "Identifier.h"
31
32namespace JSC {
33
34enum class JSParserStrictMode { NotStrict, Strict };
35enum class JSParserBuiltinMode { NotBuiltin, Builtin };
36enum class JSParserCodeType { Program, Function, Module };
37
38enum class ConstructorKind { None, Base, Derived };
39enum class SuperBinding { Needed, NotNeeded };
40enum class ThisTDZMode { AlwaysCheck, CheckIfNeeded };
41
42enum ProfilerMode { ProfilerOff, ProfilerOn };
43enum DebuggerMode { DebuggerOff, DebuggerOn };
44
45enum FunctionMode { FunctionExpression, FunctionDeclaration };
46
47enum class SourceParseMode : uint8_t {
48 NormalFunctionMode,
49 GeneratorBodyMode,
50 GeneratorWrapperFunctionMode,
51 GetterMode,
52 SetterMode,
53 MethodMode,
54 ArrowFunctionMode,
55 ProgramMode,
56 ModuleAnalyzeMode,
57 ModuleEvaluateMode
58};
59
60inline bool isFunctionParseMode(SourceParseMode parseMode)
61{
62 switch (parseMode) {
63 case SourceParseMode::NormalFunctionMode:
64 case SourceParseMode::GeneratorBodyMode:
65 case SourceParseMode::GeneratorWrapperFunctionMode:
66 case SourceParseMode::GetterMode:
67 case SourceParseMode::SetterMode:
68 case SourceParseMode::MethodMode:
69 case SourceParseMode::ArrowFunctionMode:
70 return true;
71
72 case SourceParseMode::ProgramMode:
73 case SourceParseMode::ModuleAnalyzeMode:
74 case SourceParseMode::ModuleEvaluateMode:
75 return false;
76 }
77 RELEASE_ASSERT_NOT_REACHED();
78 return false;
79}
80
81inline bool isModuleParseMode(SourceParseMode parseMode)
82{
83 switch (parseMode) {
84 case SourceParseMode::ModuleAnalyzeMode:
85 case SourceParseMode::ModuleEvaluateMode:
86 return true;
87
88 case SourceParseMode::NormalFunctionMode:
89 case SourceParseMode::GeneratorBodyMode:
90 case SourceParseMode::GeneratorWrapperFunctionMode:
91 case SourceParseMode::GetterMode:
92 case SourceParseMode::SetterMode:
93 case SourceParseMode::MethodMode:
94 case SourceParseMode::ArrowFunctionMode:
95 case SourceParseMode::ProgramMode:
96 return false;
97 }
98 RELEASE_ASSERT_NOT_REACHED();
99 return false;
100}
101
102inline bool isProgramParseMode(SourceParseMode parseMode)
103{
104 switch (parseMode) {
105 case SourceParseMode::ProgramMode:
106 return true;
107
108 case SourceParseMode::NormalFunctionMode:
109 case SourceParseMode::GeneratorBodyMode:
110 case SourceParseMode::GeneratorWrapperFunctionMode:
111 case SourceParseMode::GetterMode:
112 case SourceParseMode::SetterMode:
113 case SourceParseMode::MethodMode:
114 case SourceParseMode::ArrowFunctionMode:
115 case SourceParseMode::ModuleAnalyzeMode:
116 case SourceParseMode::ModuleEvaluateMode:
117 return false;
118 }
119 RELEASE_ASSERT_NOT_REACHED();
120 return false;
121}
122
123inline bool functionNameIsInScope(const Identifier& name, FunctionMode functionMode)
124{
125 if (name.isNull())
126 return false;
127
128 if (functionMode != FunctionExpression)
129 return false;
130
131 return true;
132}
133
134inline bool functionNameScopeIsDynamic(bool usesEval, bool isStrictMode)
135{
136 // If non-strict eval is in play, a function gets a separate object in the scope chain for its name.
137 // This enables eval to declare and then delete a name that shadows the function's name.
138
139 if (!usesEval)
140 return false;
141
142 if (isStrictMode)
143 return false;
144
145 return true;
146}
147
148typedef uint16_t CodeFeatures;
149
150const CodeFeatures NoFeatures = 0;
151const CodeFeatures EvalFeature = 1 << 0;
152const CodeFeatures ArgumentsFeature = 1 << 1;
153const CodeFeatures WithFeature = 1 << 2;
154const CodeFeatures ThisFeature = 1 << 3;
155const CodeFeatures StrictModeFeature = 1 << 4;
156const CodeFeatures ShadowsArgumentsFeature = 1 << 5;
157const CodeFeatures ModifiedParameterFeature = 1 << 6;
158const CodeFeatures ModifiedArgumentsFeature = 1 << 7;
159const CodeFeatures ArrowFunctionFeature = 1 << 8;
160const CodeFeatures ArrowFunctionContextFeature = 1 << 9;
161
162const CodeFeatures AllFeatures = EvalFeature | ArgumentsFeature | WithFeature | ThisFeature | StrictModeFeature | ShadowsArgumentsFeature | ModifiedParameterFeature | ArrowFunctionFeature | ArrowFunctionContextFeature;
163
164} // namespace JSC
165
166#endif // ParserModes_h
167