OLD | NEW |
(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 ExtensionGenerator implements ProtobufContainer { |
| 6 |
| 7 // Whitespace |
| 8 static final String sp = MessageGenerator.sp; |
| 9 |
| 10 // List of all ExtensionGenerators encountered during a compilation run |
| 11 static List<ExtensionGenerator> allExtensions; |
| 12 |
| 13 ExtensionGenerator(GoogleProtobuf_FieldDescriptorProto descriptor, |
| 14 ProtobufContainer parent, GenerationContext context) |
| 15 : this._descriptor = descriptor, |
| 16 this._parent = parent, |
| 17 this._context = context, |
| 18 fqname = parent.fqname == "." ? |
| 19 ".${descriptor.name}" : |
| 20 "${parent.fqname}.${descriptor.name}" { |
| 21 if (allExtensions == null) { |
| 22 allExtensions = new List<ExtensionGenerator>(); |
| 23 } |
| 24 allExtensions.add(this); |
| 25 } |
| 26 |
| 27 String get classname() { |
| 28 if (_classname == null) { |
| 29 String name = new ProtobufField(_descriptor, _context).externalFieldName; |
| 30 if (_parent is MessageGenerator) { |
| 31 MessageGenerator mg = _parent; |
| 32 _classname = "${mg._classname}.$name"; |
| 33 } else { |
| 34 _classname = name; |
| 35 } |
| 36 } |
| 37 return _classname; |
| 38 } |
| 39 |
| 40 void generate(IndentingWriter out) { |
| 41 ProtobufField field = new ProtobufField(_descriptor, _context); |
| 42 String baseType = field.baseType; |
| 43 |
| 44 String name = field.externalFieldName; |
| 45 String type = field.shortTypeName; |
| 46 |
| 47 String typeName = ""; |
| 48 ProtobufContainer typeNameContainer = _context[_descriptor.typeName]; |
| 49 if (typeNameContainer != null) { |
| 50 typeName = typeNameContainer.classname; |
| 51 } |
| 52 |
| 53 String extendee = ""; |
| 54 ProtobufContainer extendeeContainer = _context[_descriptor.extendee]; |
| 55 if (extendeeContainer != null) { |
| 56 extendee = extendeeContainer.classname; |
| 57 } |
| 58 |
| 59 out.println("static Extension _$name${sp}=${sp}null;"); |
| 60 out.println("static Extension get $name()${sp}{"); |
| 61 out.println("${sp}${sp}if${sp}(null${sp}==${sp}_$name${sp}) {"); |
| 62 |
| 63 String initializer = ""; |
| 64 String builder = ""; |
| 65 String valueOf = ""; |
| 66 |
| 67 if (_descriptor.type == GoogleProtobuf_FieldDescriptorProto_Type.TYPE_MESSAG
E || |
| 68 _descriptor.type == GoogleProtobuf_FieldDescriptorProto_Type.TYPE_GROUP)
{ |
| 69 if (_descriptor.label == |
| 70 GoogleProtobuf_FieldDescriptorProto_Label.LABEL_REPEATED) { |
| 71 initializer = ",${sp}()${sp}=>${sp}new PbList<${typeName}>(null)"; |
| 72 builder = ",${sp}()${sp}=>${sp}new ${typeName}_Builder()"; |
| 73 } else { |
| 74 initializer = ",${sp}()${sp}=>${sp}${typeName}.defaultInstance"; |
| 75 builder = ",${sp}()${sp}=>${sp}new ${typeName}_Builder()"; |
| 76 } |
| 77 } else { |
| 78 if (_descriptor.label == |
| 79 GoogleProtobuf_FieldDescriptorProto_Label.LABEL_REPEATED) { |
| 80 initializer = ",${sp}()${sp}=>${sp}new PbList<${baseType}>(null)"; |
| 81 } else if (field.hasInitialization) { |
| 82 initializer = ",${sp}${field.initialization}"; |
| 83 } |
| 84 } |
| 85 |
| 86 if (field.enum) { |
| 87 if (initializer.isEmpty()) { |
| 88 initializer = ",${sp}null"; |
| 89 } |
| 90 if (builder.isEmpty()) { |
| 91 builder = ",${sp}null"; |
| 92 } |
| 93 valueOf = ",${sp}(var v)${sp}=>${sp}${field.baseType}.valueOf(v)"; |
| 94 } |
| 95 |
| 96 out.println("${sp}${sp}${sp}${sp}_$name${sp}=" |
| 97 "${sp}new Extension(\"$extendee\",${sp}\"$name\",${sp}" |
| 98 "${_descriptor.number},${sp}Builder.$type" |
| 99 "${initializer}${builder}${valueOf}" |
| 100 ");"); |
| 101 |
| 102 out.println("${sp}${sp}}"); |
| 103 out.println("${sp}${sp}return _$name;"); |
| 104 out.println("}"); |
| 105 } |
| 106 |
| 107 String get name() => classname; |
| 108 GoogleProtobuf_FileOptions_OptimizeMode get optimizeFor() => _parent.optimizeF
or; |
| 109 |
| 110 final String fqname; |
| 111 String _classname; |
| 112 GoogleProtobuf_FieldDescriptorProto _descriptor; |
| 113 ProtobufContainer _parent; |
| 114 GenerationContext _context; |
| 115 } |
OLD | NEW |