| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library fasta.kernel_procedure_builder; | 5 library fasta.kernel_procedure_builder; |
| 6 | 6 |
| 7 import 'package:kernel/ast.dart' | 7 import 'package:kernel/ast.dart' |
| 8 show | 8 show |
| 9 Arguments, | 9 Arguments, |
| 10 AsyncMarker, | 10 AsyncMarker, |
| 11 Constructor, | 11 Constructor, |
| 12 ConstructorInvocation, | 12 ConstructorInvocation, |
| 13 DartType, | 13 DartType, |
| 14 DynamicType, | 14 DynamicType, |
| 15 EmptyStatement, | 15 EmptyStatement, |
| 16 Expression, | 16 Expression, |
| 17 FunctionNode, | 17 FunctionNode, |
| 18 Initializer, | 18 Initializer, |
| 19 Library, | |
| 20 LocalInitializer, | 19 LocalInitializer, |
| 21 Member, | 20 Member, |
| 22 Name, | 21 Name, |
| 23 NamedExpression, | 22 NamedExpression, |
| 24 Procedure, | 23 Procedure, |
| 25 ProcedureKind, | 24 ProcedureKind, |
| 26 RedirectingInitializer, | 25 RedirectingInitializer, |
| 27 Statement, | 26 Statement, |
| 28 StaticInvocation, | 27 StaticInvocation, |
| 29 StringLiteral, | 28 StringLiteral, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 45 import 'kernel_builder.dart' | 44 import 'kernel_builder.dart' |
| 46 show | 45 show |
| 47 Builder, | 46 Builder, |
| 48 ClassBuilder, | 47 ClassBuilder, |
| 49 ConstructorReferenceBuilder, | 48 ConstructorReferenceBuilder, |
| 50 FormalParameterBuilder, | 49 FormalParameterBuilder, |
| 51 KernelFormalParameterBuilder, | 50 KernelFormalParameterBuilder, |
| 52 KernelLibraryBuilder, | 51 KernelLibraryBuilder, |
| 53 KernelTypeBuilder, | 52 KernelTypeBuilder, |
| 54 KernelTypeVariableBuilder, | 53 KernelTypeVariableBuilder, |
| 54 LibraryBuilder, |
| 55 MetadataBuilder, | 55 MetadataBuilder, |
| 56 ProcedureBuilder, | 56 ProcedureBuilder, |
| 57 TypeVariableBuilder, | 57 TypeVariableBuilder, |
| 58 memberError; | 58 memberError; |
| 59 | 59 |
| 60 abstract class KernelFunctionBuilder | 60 abstract class KernelFunctionBuilder |
| 61 extends ProcedureBuilder<KernelTypeBuilder> { | 61 extends ProcedureBuilder<KernelTypeBuilder> { |
| 62 final String nativeMethodName; | 62 final String nativeMethodName; |
| 63 | 63 |
| 64 FunctionNode function; | 64 FunctionNode function; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 86 if (function != null) { | 86 if (function != null) { |
| 87 function.body = newBody; | 87 function.body = newBody; |
| 88 newBody?.parent = function; | 88 newBody?.parent = function; |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 | 91 |
| 92 Statement get body => actualBody ??= new EmptyStatement(); | 92 Statement get body => actualBody ??= new EmptyStatement(); |
| 93 | 93 |
| 94 bool get isNative => nativeMethodName != null; | 94 bool get isNative => nativeMethodName != null; |
| 95 | 95 |
| 96 FunctionNode buildFunction() { | 96 FunctionNode buildFunction(LibraryBuilder library) { |
| 97 assert(function == null); | 97 assert(function == null); |
| 98 FunctionNode result = new FunctionNode(body, asyncMarker: asyncModifier); | 98 FunctionNode result = new FunctionNode(body, asyncMarker: asyncModifier); |
| 99 if (typeVariables != null) { | 99 if (typeVariables != null) { |
| 100 for (KernelTypeVariableBuilder t in typeVariables) { | 100 for (KernelTypeVariableBuilder t in typeVariables) { |
| 101 result.typeParameters.add(t.parameter); | 101 result.typeParameters.add(t.parameter); |
| 102 } | 102 } |
| 103 setParents(result.typeParameters, result); | 103 setParents(result.typeParameters, result); |
| 104 } | 104 } |
| 105 if (formals != null) { | 105 if (formals != null) { |
| 106 for (KernelFormalParameterBuilder formal in formals) { | 106 for (KernelFormalParameterBuilder formal in formals) { |
| 107 VariableDeclaration parameter = formal.build(); | 107 VariableDeclaration parameter = formal.build(library); |
| 108 if (formal.isNamed) { | 108 if (formal.isNamed) { |
| 109 result.namedParameters.add(parameter); | 109 result.namedParameters.add(parameter); |
| 110 } else { | 110 } else { |
| 111 result.positionalParameters.add(parameter); | 111 result.positionalParameters.add(parameter); |
| 112 } | 112 } |
| 113 parameter.parent = result; | 113 parameter.parent = result; |
| 114 if (formal.isRequired) { | 114 if (formal.isRequired) { |
| 115 result.requiredParameterCount++; | 115 result.requiredParameterCount++; |
| 116 } | 116 } |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 if (returnType != null) { | 119 if (returnType != null) { |
| 120 result.returnType = returnType.build(); | 120 result.returnType = returnType.build(library); |
| 121 } | 121 } |
| 122 if (!isConstructor && !isInstanceMember && parent is ClassBuilder) { | 122 if (!isConstructor && !isInstanceMember && parent is ClassBuilder) { |
| 123 List<TypeParameter> typeParameters = parent.target.typeParameters; | 123 List<TypeParameter> typeParameters = parent.target.typeParameters; |
| 124 if (typeParameters.isNotEmpty) { | 124 if (typeParameters.isNotEmpty) { |
| 125 Map<TypeParameter, DartType> substitution; | 125 Map<TypeParameter, DartType> substitution; |
| 126 DartType removeTypeVariables(DartType type) { | 126 DartType removeTypeVariables(DartType type) { |
| 127 if (substitution == null) { | 127 if (substitution == null) { |
| 128 substitution = <TypeParameter, DartType>{}; | 128 substitution = <TypeParameter, DartType>{}; |
| 129 for (TypeParameter parameter in typeParameters) { | 129 for (TypeParameter parameter in typeParameters) { |
| 130 substitution[parameter] = const DynamicType(); | 130 substitution[parameter] = const DynamicType(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 if (containsTypeVariable(result.returnType, set)) { | 149 if (containsTypeVariable(result.returnType, set)) { |
| 150 result.returnType = removeTypeVariables(result.returnType); | 150 result.returnType = removeTypeVariables(result.returnType); |
| 151 } | 151 } |
| 152 } | 152 } |
| 153 } | 153 } |
| 154 return function = result; | 154 return function = result; |
| 155 } | 155 } |
| 156 | 156 |
| 157 Member build(Library library); | 157 Member build(LibraryBuilder library); |
| 158 | 158 |
| 159 void becomeNative(Loader loader) { | 159 void becomeNative(Loader loader) { |
| 160 target.isExternal = true; | 160 target.isExternal = true; |
| 161 Builder constructor = loader.getNativeAnnotation(); | 161 Builder constructor = loader.getNativeAnnotation(); |
| 162 Arguments arguments = | 162 Arguments arguments = |
| 163 new Arguments(<Expression>[new StringLiteral(nativeMethodName)]); | 163 new Arguments(<Expression>[new StringLiteral(nativeMethodName)]); |
| 164 Expression annotation; | 164 Expression annotation; |
| 165 if (constructor.isConstructor) { | 165 if (constructor.isConstructor) { |
| 166 annotation = new ConstructorInvocation(constructor.target, arguments) | 166 annotation = new ConstructorInvocation(constructor.target, arguments) |
| 167 ..isConst = true; | 167 ..isConst = true; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 } | 213 } |
| 214 | 214 |
| 215 void set asyncModifier(AsyncMarker newModifier) { | 215 void set asyncModifier(AsyncMarker newModifier) { |
| 216 actualAsyncModifier = newModifier; | 216 actualAsyncModifier = newModifier; |
| 217 if (function != null) { | 217 if (function != null) { |
| 218 // No parent, it's an enum. | 218 // No parent, it's an enum. |
| 219 function.asyncMarker = actualAsyncModifier; | 219 function.asyncMarker = actualAsyncModifier; |
| 220 } | 220 } |
| 221 } | 221 } |
| 222 | 222 |
| 223 Procedure build(Library library) { | 223 Procedure build(LibraryBuilder library) { |
| 224 // TODO(ahe): I think we may call this twice on parts. Investigate. | 224 // TODO(ahe): I think we may call this twice on parts. Investigate. |
| 225 if (procedure.name == null) { | 225 if (procedure.name == null) { |
| 226 procedure.function = buildFunction(); | 226 procedure.function = buildFunction(library); |
| 227 procedure.function.parent = procedure; | 227 procedure.function.parent = procedure; |
| 228 procedure.isAbstract = isAbstract; | 228 procedure.isAbstract = isAbstract; |
| 229 procedure.isStatic = isStatic; | 229 procedure.isStatic = isStatic; |
| 230 procedure.isExternal = isExternal; | 230 procedure.isExternal = isExternal; |
| 231 procedure.isConst = isConst; | 231 procedure.isConst = isConst; |
| 232 procedure.name = new Name(name, library); | 232 procedure.name = new Name(name, library.target); |
| 233 } | 233 } |
| 234 return procedure; | 234 return procedure; |
| 235 } | 235 } |
| 236 | 236 |
| 237 Procedure get target => procedure; | 237 Procedure get target => procedure; |
| 238 } | 238 } |
| 239 | 239 |
| 240 // TODO(ahe): Move this to own file? | 240 // TODO(ahe): Move this to own file? |
| 241 class KernelConstructorBuilder extends KernelFunctionBuilder { | 241 class KernelConstructorBuilder extends KernelFunctionBuilder { |
| 242 final Constructor constructor = new Constructor(null); | 242 final Constructor constructor = new Constructor(null); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 261 compilationUnit, charOffset, nativeMethodName); | 261 compilationUnit, charOffset, nativeMethodName); |
| 262 | 262 |
| 263 bool get isInstanceMember => false; | 263 bool get isInstanceMember => false; |
| 264 | 264 |
| 265 bool get isConstructor => true; | 265 bool get isConstructor => true; |
| 266 | 266 |
| 267 AsyncMarker get asyncModifier => AsyncMarker.Sync; | 267 AsyncMarker get asyncModifier => AsyncMarker.Sync; |
| 268 | 268 |
| 269 ProcedureKind get kind => null; | 269 ProcedureKind get kind => null; |
| 270 | 270 |
| 271 Constructor build(Library library) { | 271 Constructor build(LibraryBuilder library) { |
| 272 if (constructor.name == null) { | 272 if (constructor.name == null) { |
| 273 constructor.function = buildFunction(); | 273 constructor.function = buildFunction(library); |
| 274 constructor.function.parent = constructor; | 274 constructor.function.parent = constructor; |
| 275 constructor.isConst = isConst; | 275 constructor.isConst = isConst; |
| 276 constructor.isExternal = isExternal; | 276 constructor.isExternal = isExternal; |
| 277 constructor.name = new Name(name, library); | 277 constructor.name = new Name(name, library.target); |
| 278 } | 278 } |
| 279 return constructor; | 279 return constructor; |
| 280 } | 280 } |
| 281 | 281 |
| 282 FunctionNode buildFunction() { | 282 FunctionNode buildFunction(LibraryBuilder library) { |
| 283 // TODO(ahe): Should complain if another type is explicitly set. | 283 // TODO(ahe): Should complain if another type is explicitly set. |
| 284 return super.buildFunction()..returnType = const VoidType(); | 284 return super.buildFunction(library)..returnType = const VoidType(); |
| 285 } | 285 } |
| 286 | 286 |
| 287 Constructor get target => constructor; | 287 Constructor get target => constructor; |
| 288 | 288 |
| 289 void checkSuperOrThisInitializer(Initializer initializer) { | 289 void checkSuperOrThisInitializer(Initializer initializer) { |
| 290 if (superInitializer != null || redirectingInitializer != null) { | 290 if (superInitializer != null || redirectingInitializer != null) { |
| 291 memberError( | 291 memberError( |
| 292 target, | 292 target, |
| 293 "Can't have more than one 'super' or 'this' initializer.", | 293 "Can't have more than one 'super' or 'this' initializer.", |
| 294 initializer.fileOffset); | 294 initializer.fileOffset); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 } | 341 } |
| 342 } | 342 } |
| 343 initializers.add(initializer..parent = constructor); | 343 initializers.add(initializer..parent = constructor); |
| 344 initializers.add(superInitializer); | 344 initializers.add(superInitializer); |
| 345 return; | 345 return; |
| 346 } | 346 } |
| 347 initializers.add(initializer); | 347 initializers.add(initializer); |
| 348 initializer.parent = constructor; | 348 initializer.parent = constructor; |
| 349 } | 349 } |
| 350 } | 350 } |
| OLD | NEW |