| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014, 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 codegen.dart; |
| 6 |
| 7 import 'api.dart'; |
| 8 |
| 9 /** |
| 10 * Visitor specialized for generating Dart code. |
| 11 */ |
| 12 class DartCodegenVisitor extends HierarchicalApiVisitor { |
| 13 /** |
| 14 * Type references in the spec that are named something else in Dart. |
| 15 */ |
| 16 static const Map<String, String> _typeRenames = const { |
| 17 'long': 'int', |
| 18 'object': 'Map', |
| 19 }; |
| 20 |
| 21 DartCodegenVisitor(Api api) : super(api); |
| 22 |
| 23 /** |
| 24 * Convert the given [TypeDecl] to a Dart type. |
| 25 */ |
| 26 String dartType(TypeDecl type) { |
| 27 if (type is TypeReference) { |
| 28 String typeName = type.typeName; |
| 29 TypeDefinition referencedDefinition = api.types[typeName]; |
| 30 if (_typeRenames.containsKey(typeName)) { |
| 31 return _typeRenames[typeName]; |
| 32 } |
| 33 if (referencedDefinition == null) { |
| 34 return typeName; |
| 35 } |
| 36 TypeDecl referencedType = referencedDefinition.type; |
| 37 if (referencedType is TypeObject || referencedType is TypeEnum) { |
| 38 return typeName; |
| 39 } |
| 40 return dartType(referencedType); |
| 41 } else if (type is TypeList) { |
| 42 return 'List<${dartType(type.itemType)}>'; |
| 43 } else if (type is TypeMap) { |
| 44 return 'Map<${dartType(type.keyType)}, ${dartType(type.valueType)}>'; |
| 45 } else if (type is TypeUnion) { |
| 46 return 'dynamic'; |
| 47 } else { |
| 48 throw new Exception("Can't convert to a dart type"); |
| 49 } |
| 50 } |
| 51 } |
| OLD | NEW |