Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(326)

Side by Side Diff: frog/leg/universe.dart

Issue 9873021: Move frog/leg to lib/compiler/implementation. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « frog/leg/typechecker.dart ('k') | frog/leg/util/characters.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 class Universe {
6 Map<Element, String> generatedCode;
7 Map<Element, String> generatedBailoutCode;
8 final Set<ClassElement> instantiatedClasses;
9 final Set<SourceString> instantiatedClassInstanceFields;
10 final Set<FunctionElement> staticFunctionsNeedingGetter;
11 final Map<SourceString, Set<Selector>> invokedNames;
12 final Set<SourceString> invokedGetters;
13 final Set<SourceString> invokedSetters;
14 final Map<String, LibraryElement> libraries;
15 // TODO(ngeoffray): This should be a Set<Type>.
16 final Set<Element> isChecks;
17
18 Universe() : generatedCode = new Map<Element, String>(),
19 generatedBailoutCode = new Map<Element, String>(),
20 libraries = new Map<String, LibraryElement>(),
21 instantiatedClasses = new Set<ClassElement>(),
22 instantiatedClassInstanceFields = new Set<SourceString>(),
23 staticFunctionsNeedingGetter = new Set<FunctionElement>(),
24 invokedNames = new Map<SourceString, Set<Selector>>(),
25 invokedGetters = new Set<SourceString>(),
26 invokedSetters = new Set<SourceString>(),
27 isChecks = new Set<Element>();
28
29 void addGeneratedCode(WorkItem work, String code) {
30 generatedCode[work.element] = code;
31 }
32
33 void addBailoutCode(WorkItem work, String code) {
34 generatedBailoutCode[work.element] = code;
35 }
36 }
37
38 class SelectorKind {
39 final String name;
40 const SelectorKind(this.name);
41
42 static final SelectorKind GETTER = const SelectorKind('getter');
43 static final SelectorKind SETTER = const SelectorKind('setter');
44 static final SelectorKind INVOCATION = const SelectorKind('invocation');
45 static final SelectorKind OPERATOR = const SelectorKind('operator');
46 static final SelectorKind INDEX = const SelectorKind('index');
47
48 toString() => name;
49 }
50
51 class Selector implements Hashable {
52 // The numbers of arguments of the selector. Includes named
53 // arguments.
54 final int argumentCount;
55 final SelectorKind kind;
56 const Selector(this.kind, this.argumentCount);
57
58 int hashCode() => argumentCount + 1000 * namedArguments.length;
59 List<SourceString> get namedArguments() => const <SourceString>[];
60 int get namedArgumentCount() => 0;
61 int get positionalArgumentCount() => argumentCount;
62
63 static final Selector GETTER = const Selector(SelectorKind.GETTER, 0);
64 static final Selector SETTER = const Selector(SelectorKind.SETTER, 1);
65 static final Selector UNARY_OPERATOR =
66 const Selector(SelectorKind.OPERATOR, 0);
67 static final Selector BINARY_OPERATOR =
68 const Selector(SelectorKind.OPERATOR, 1);
69 static final Selector INDEX = const Selector(SelectorKind.INDEX, 1);
70 static final Selector INDEX_SET = const Selector(SelectorKind.INDEX, 2);
71 static final Selector INDEX_AND_INDEX_SET =
72 const Selector(SelectorKind.INDEX, 2);
73 static final Selector GETTER_AND_SETTER =
74 const Selector(SelectorKind.SETTER, 1);
75 static final Selector INVOCATION_0 =
76 const Selector(SelectorKind.INVOCATION, 0);
77 static final Selector INVOCATION_1 =
78 const Selector(SelectorKind.INVOCATION, 1);
79 static final Selector INVOCATION_2 =
80 const Selector(SelectorKind.INVOCATION, 2);
81
82 bool applies(FunctionParameters parameters) {
83 if (argumentCount > parameters.parameterCount) return false;
84 int requiredParameterCount = parameters.requiredParameterCount;
85 int optionalParameterCount = parameters.optionalParameterCount;
86
87 bool hasOptionalParameters = !parameters.optionalParameters.isEmpty();
88 if (namedArguments.isEmpty()) {
89 if (!hasOptionalParameters) {
90 return requiredParameterCount == argumentCount;
91 } else {
92 return argumentCount >= requiredParameterCount &&
93 argumentCount <= requiredParameterCount + optionalParameterCount;
94 }
95 } else {
96 if (!hasOptionalParameters) return false;
97 Link<Element> remainingNamedParameters = parameters.optionalParameters;
98 for (int i = requiredParameterCount; i < positionalArgumentCount; i++) {
99 remainingNamedParameters = remainingNamedParameters.tail;
100 }
101 Set<SourceString> nameSet = new Set<SourceString>();
102 for (;
103 !remainingNamedParameters.isEmpty();
104 remainingNamedParameters = remainingNamedParameters.tail) {
105 nameSet.add(remainingNamedParameters.head.name);
106 }
107
108 for (SourceString name in namedArguments) {
109 if (!nameSet.contains(name)) {
110 return false;
111 }
112 nameSet.remove(name);
113 }
114 return true;
115 }
116 }
117
118 /**
119 * Returns [:true:] if the selector and the [element] match; [:false:]
120 * otherwise.
121 */
122 bool addSendArgumentsToList(Send send,
123 List list,
124 FunctionParameters parameters,
125 compileArgument(Node argument),
126 compileConstant(Element element)) {
127 void addMatchingSendArgumentsToList(Link<Node> link) {
128 for (; !link.isEmpty(); link = link.tail) {
129 list.add(compileArgument(link.head));
130 }
131 }
132
133 if (!this.applies(parameters)) return false;
134 if (this.positionalArgumentCount == parameters.parameterCount) {
135 addMatchingSendArgumentsToList(send.arguments);
136 return true;
137 }
138
139 // If there are named arguments, provide them in the order
140 // expected by the called function, which is the source order.
141
142 // Visit positional arguments and add them to the list.
143 Link<Node> arguments = send.arguments;
144 int positionalArgumentCount = this.positionalArgumentCount;
145 for (int i = 0;
146 i < positionalArgumentCount;
147 arguments = arguments.tail, i++) {
148 list.add(compileArgument(arguments.head));
149 }
150
151 // Visit named arguments and add them into a temporary list.
152 List namedArguments = [];
153 for (; !arguments.isEmpty(); arguments = arguments.tail) {
154 NamedArgument namedArgument = arguments.head;
155 namedArguments.add(compileArgument(namedArgument.expression));
156 }
157
158 Link<Element> remainingNamedParameters = parameters.optionalParameters;
159 // Skip the optional parameters that have been given in the
160 // positional arguments.
161 for (int i = parameters.requiredParameterCount;
162 i < positionalArgumentCount;
163 i++) {
164 remainingNamedParameters = remainingNamedParameters.tail;
165 }
166
167 // Loop over the remaining named parameters, and try to find
168 // their values: either in the temporary list or using the
169 // default value.
170 for (;
171 !remainingNamedParameters.isEmpty();
172 remainingNamedParameters = remainingNamedParameters.tail) {
173 Element parameter = remainingNamedParameters.head;
174 int foundIndex = -1;
175 for (int i = 0; i < this.namedArguments.length; i++) {
176 SourceString name = this.namedArguments[i];
177 if (name == parameter.name) {
178 foundIndex = i;
179 break;
180 }
181 }
182 if (foundIndex != -1) {
183 list.add(namedArguments[foundIndex]);
184 } else {
185 list.add(compileConstant(parameter));
186 }
187 }
188 return true;
189 }
190
191 static bool sameNames(List<SourceString> first, List<SourceString> second) {
192 for (int i = 0; i < first.length; i++) {
193 if (first[i] != second[i]) return false;
194 }
195 return true;
196 }
197
198 bool operator ==(other) {
199 if (other is !Selector) return false;
200 return argumentCount == other.argumentCount
201 && namedArguments.length == other.namedArguments.length
202 && sameNames(namedArguments, other.namedArguments);
203 }
204
205 List<SourceString> getOrderedNamedArguments() => namedArguments;
206
207 toString() => '$kind $argumentCount';
208 }
209
210 class Invocation extends Selector {
211 final List<SourceString> namedArguments;
212 List<SourceString> orderedNamedArguments;
213 int get namedArgumentCount() => namedArguments.length;
214 int get positionalArgumentCount() => argumentCount - namedArgumentCount;
215
216 Invocation(int argumentCount,
217 [List<SourceString> names = const <SourceString>[]])
218 : super(SelectorKind.INVOCATION, argumentCount),
219 namedArguments = names,
220 orderedNamedArguments = const <SourceString>[];
221
222 List<SourceString> getOrderedNamedArguments() {
223 if (namedArguments.isEmpty()) return namedArguments;
224 // We use the empty const List as a sentinel.
225 if (!orderedNamedArguments.isEmpty()) return orderedNamedArguments;
226
227 List<SourceString> list = new List<SourceString>.from(namedArguments);
228 list.sort((SourceString first, SourceString second) {
229 return first.slowToString().compareTo(second.slowToString());
230 });
231 orderedNamedArguments = list;
232 return orderedNamedArguments;
233 }
234 }
OLDNEW
« no previous file with comments | « frog/leg/typechecker.dart ('k') | frog/leg/util/characters.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698