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

Side by Side Diff: pkg/kernel/lib/transformations/closure/mock.dart

Issue 2561723003: Merge kernel closure conversion into the Dart SDK (Closed)
Patch Set: Also restrict the test to linux for now Created 4 years 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
OLDNEW
(Empty)
1 // Copyright (c) 2016, 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 library kernel.transformations.closure.mock;
6
7 import '../../ast.dart' show
8 Arguments,
9 Block,
10 Class,
11 Constructor,
12 ConstructorInvocation,
13 DartType,
14 DynamicType,
15 EmptyStatement,
16 Expression,
17 ExpressionStatement,
18 Field,
19 FieldInitializer,
20 FunctionNode,
21 Initializer,
22 IntLiteral,
23 Library,
24 MethodInvocation,
25 Name,
26 Procedure,
27 ProcedureKind,
28 Program,
29 PropertyGet,
30 ReturnStatement,
31 Statement,
32 StaticInvocation,
33 Supertype,
34 VariableDeclaration,
35 VariableGet;
36
37 import '../../core_types.dart' show
38 CoreTypes;
39
40 import '../../frontend/accessors.dart' show
41 Accessor,
42 IndexAccessor,
43 PropertyAccessor,
44 ThisPropertyAccessor,
45 VariableAccessor;
46
47 /// Extend the program with this mock:
48 ///
49 /// class Context {
50 /// final List list;
51 /// var parent;
52 /// Context(int i) : list = new List(i);
53 /// operator[] (int i) => list[i];
54 /// operator[]= (int i, value) {
55 /// list[i] = value;
56 /// }
57 /// Context copy() {
58 /// Context c = new Context(list.length);
59 /// c.parent = parent;
60 /// c.list.setRange(0, list.length, list);
61 /// return c;
62 /// }
63 /// }
64 ///
65 /// Returns the mock.
66 Class mockUpContext(CoreTypes coreTypes, Program program) {
67 String fileUri = "dart:mock";
68 /// final List list;
69 Field listField = new Field(new Name("list"),
70 type: coreTypes.listClass.rawType, isFinal: true, fileUri: fileUri);
71 Accessor listFieldAccessor =
72 new ThisPropertyAccessor(listField.name, listField, null);
73
74 /// var parent;
75 Field parentField = new Field(new Name("parent"), fileUri: fileUri);
76 Accessor parentFieldAccessor =
77 new ThisPropertyAccessor(parentField.name, parentField, parentField);
78
79 List<Field> fields = <Field>[listField, parentField];
80
81 /// Context(int i) : list = new List(i);
82 VariableDeclaration iParameter = new VariableDeclaration(
83 "i", type: coreTypes.intClass.rawType, isFinal: true);
84 Constructor constructor = new Constructor(
85 new FunctionNode(new EmptyStatement(),
86 positionalParameters: <VariableDeclaration>[iParameter]),
87 name: new Name(""),
88 initializers: <Initializer>[
89 new FieldInitializer(
90 listField, new StaticInvocation(
91 coreTypes.listClass.procedures.first,
92 new Arguments(
93 <Expression>[
94 new VariableAccessor(iParameter)
95 .buildSimpleRead()],
96 types: <DartType>[const DynamicType()])))]);
97
98 /// operator[] (int i) => list[i];
99 iParameter = new VariableDeclaration(
100 "i", type: coreTypes.intClass.rawType, isFinal: true);
101 Accessor accessor = IndexAccessor.make(
102 listFieldAccessor.buildSimpleRead(),
103 new VariableAccessor(iParameter).buildSimpleRead(),
104 null, null);
105 Procedure indexGet = new Procedure(new Name("[]"), ProcedureKind.Operator,
106 new FunctionNode(new ReturnStatement(accessor.buildSimpleRead()),
107 positionalParameters: <VariableDeclaration>[iParameter]),
108 fileUri: fileUri);
109
110 /// operator[]= (int i, value) {
111 /// list[i] = value;
112 /// }
113 iParameter = new VariableDeclaration(
114 "i", type: coreTypes.intClass.rawType, isFinal: true);
115 VariableDeclaration valueParameter = new VariableDeclaration(
116 "value", isFinal: true);
117 accessor = IndexAccessor.make(
118 listFieldAccessor.buildSimpleRead(),
119 new VariableAccessor(iParameter).buildSimpleRead(), null, null);
120 Expression expression = accessor.buildAssignment(
121 new VariableAccessor(valueParameter).buildSimpleRead(),
122 voidContext: true);
123 Procedure indexSet = new Procedure(new Name("[]="), ProcedureKind.Operator,
124 new FunctionNode(new ExpressionStatement(expression),
125 positionalParameters: <VariableDeclaration>[
126 iParameter, valueParameter]), fileUri: fileUri);
127
128 /// Context copy() {
129 /// Context c = new Context(list.length);
130 /// c.parent = parent;
131 /// c.list.setRange(0, list.length, list);
132 /// return c;
133 /// }
134 VariableDeclaration c = new VariableDeclaration(
135 "c", initializer: new ConstructorInvocation(
136 constructor,
137 new Arguments(
138 <Expression>[
139 new PropertyGet(listFieldAccessor.buildSimpleRead(),
140 new Name("length"))])));
141 Accessor accessCParent = PropertyAccessor.make(
142 new VariableGet(c), parentField.name, parentField, parentField);
143 Accessor accessCList = PropertyAccessor.make(
144 new VariableGet(c), listField.name, listField, null);
145 List<Statement> statements = <Statement>[
146 c,
147 new ExpressionStatement(
148 accessCParent.buildAssignment(
149 parentFieldAccessor.buildSimpleRead(), voidContext: true)),
150 new ExpressionStatement(
151 new MethodInvocation(
152 accessCList.buildSimpleRead(),
153 new Name("setRange"),
154 new Arguments(
155 <Expression>[
156 new IntLiteral(0),
157 new PropertyGet(
158 listFieldAccessor.buildSimpleRead(),
159 new Name("length")),
160 listFieldAccessor.buildSimpleRead()]))),
161 new ReturnStatement(new VariableGet(c))];
162 Procedure copy = new Procedure(new Name("copy"), ProcedureKind.Method,
163 new FunctionNode(new Block(statements)), fileUri: fileUri);
164
165 List<Procedure> procedures = <Procedure>[indexGet, indexSet, copy];
166
167 Class contextClass = new Class(name: "Context",
168 supertype: new Supertype(coreTypes.objectClass, const <DartType>[]),
169 constructors: [constructor],
170 fields: fields, procedures: procedures, fileUri: fileUri);
171 Library mock = new Library(
172 Uri.parse(fileUri), name: "mock", classes: [contextClass])
173 ..fileUri = fileUri;
174 program.libraries.add(mock);
175 mock.parent = program;
176 program.uriToLineStarts[mock.fileUri] = <int>[0];
177 return contextClass;
178 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698