OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino 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 old_servicec.parser; | |
6 | |
7 import 'primitives.dart' as primitives; | |
8 import 'struct_layout.dart'; | |
9 | |
10 abstract class Visitor { | |
11 visitUnit(Unit node); | |
12 visitService(Service service); | |
13 visitStruct(Struct struct); | |
14 visitUnion(Union union); | |
15 visitMethod(Method method); | |
16 visitFormal(Formal formal); | |
17 } | |
18 | |
19 enum InputKind { | |
20 PRIMITIVES, | |
21 STRUCT | |
22 } | |
23 | |
24 enum OutputKind { | |
25 PRIMITIVE, | |
26 STRUCT | |
27 } | |
28 | |
29 abstract class Node { | |
30 accept(Visitor visitor); | |
31 } | |
32 | |
33 class Unit extends Node { | |
34 final List<Service> services; | |
35 final List<Struct> structs; | |
36 Unit(this.services, this.structs); | |
37 accept(Visitor visitor) => visitor.visitUnit(this); | |
38 } | |
39 | |
40 class Service extends Node { | |
41 final String name; | |
42 final List<Method> methods; | |
43 Service(this.name, this.methods); | |
44 accept(Visitor visitor) => visitor.visitService(this); | |
45 } | |
46 | |
47 class Struct extends Node { | |
48 final String name; | |
49 final List<Formal> slots; | |
50 final List<Union> unions; | |
51 Struct(this.name, this.slots, this.unions); | |
52 | |
53 // Set by the resolver. | |
54 StructLayout layout; | |
55 | |
56 accept(Visitor visitor) => visitor.visitStruct(this); | |
57 } | |
58 | |
59 class Union extends Node { | |
60 final List<Formal> slots; | |
61 final Formal tag; | |
62 Union(this.slots) : tag = new Formal(new SimpleType("uint16", false), "tag"); | |
63 | |
64 // Set by the resolver. | |
65 Struct struct; | |
66 | |
67 accept(Visitor visitor) => visitor.visitUnion(this); | |
68 } | |
69 | |
70 class Formal extends Node { | |
71 final Type type; | |
72 final String name; | |
73 Formal(this.type, this.name); | |
74 | |
75 accept(Visitor visitor) => visitor.visitFormal(this); | |
76 } | |
77 | |
78 class Method extends Node { | |
79 final String name; | |
80 final List<Formal> arguments; | |
81 final Type returnType; | |
82 Method(this.name, this.arguments, this.returnType); | |
83 | |
84 // Set by the resolver. | |
85 OutputKind outputKind; | |
86 | |
87 InputKind inputKind; | |
88 StructLayout inputPrimitiveStructLayout; | |
89 | |
90 accept(Visitor visitor) => visitor.visitMethod(this); | |
91 } | |
92 | |
93 abstract class Type { | |
94 bool get isPointer; | |
95 bool get isList; | |
96 bool get isString; | |
97 | |
98 bool get isPrimitive => primitiveType != null; | |
99 | |
100 bool get isVoid => primitiveType == primitives.PrimitiveType.VOID; | |
101 bool get isBool => primitiveType == primitives.PrimitiveType.BOOL; | |
102 | |
103 // TODO(kasperl): Get rid of this. | |
104 String get identifier; | |
105 | |
106 // Set by the resolver. | |
107 Node resolved; | |
108 primitives.PrimitiveType primitiveType; | |
109 } | |
110 | |
111 class StringType extends Type { | |
112 final String identifier = "String"; | |
113 | |
114 int get hashCode { | |
115 int hash = identifier.hashCode; | |
116 return hash; | |
117 } | |
118 | |
119 bool operator==(Object other) { | |
120 return other is StringType; | |
121 } | |
122 | |
123 bool get isPointer => false; | |
124 bool get isList => false; | |
125 bool get isString => true; | |
126 } | |
127 | |
128 class SimpleType extends Type { | |
129 final String identifier; | |
130 final bool isPointer; | |
131 SimpleType(this.identifier, this.isPointer); | |
132 | |
133 int get hashCode { | |
134 int hash = identifier.hashCode; | |
135 if (isPointer) { | |
136 hash = hash ^ ((hash >> 16) | (hash << 16)); | |
137 } | |
138 return hash; | |
139 } | |
140 | |
141 bool operator==(Object other) { | |
142 return other is SimpleType | |
143 && identifier == other.identifier | |
144 && isPointer == other.isPointer; | |
145 } | |
146 | |
147 bool get isList => false; | |
148 bool get isString => false; | |
149 } | |
150 | |
151 class ListType extends Type { | |
152 final SimpleType elementType; | |
153 ListType(this.elementType); | |
154 | |
155 int get hashCode { | |
156 int hash = elementType.hashCode; | |
157 return ((hash >> 16) | (hash << 16)); | |
158 } | |
159 | |
160 bool operator==(Object other) { | |
161 return other is ListType && elementType == other.elementType; | |
162 } | |
163 | |
164 bool get isPointer => false; | |
165 bool get isList => true; | |
166 bool get isString => false; | |
167 | |
168 String get identifier => elementType.identifier; | |
169 } | |
OLD | NEW |