| 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 servicec.node; | |
| 6 | |
| 7 import 'package:compiler/src/tokens/token.dart' show | |
| 8 Token; | |
| 9 | |
| 10 import 'errors.dart' show | |
| 11 ErrorNode, | |
| 12 InternalCompilerError; | |
| 13 | |
| 14 import 'types.dart' show | |
| 15 TypeKind, | |
| 16 lookupType, | |
| 17 primitiveTypes; | |
| 18 | |
| 19 // Highest-level node. | |
| 20 class CompilationUnitNode extends Node { | |
| 21 List<TopLevelNode> topLevels; | |
| 22 | |
| 23 CompilationUnitNode(this.topLevels); | |
| 24 | |
| 25 void accept(NodeVisitor visitor) { | |
| 26 visitor.visitCompilationUnit(this); | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 // Top-level nodes. | |
| 31 abstract class TopLevelNode extends Node { | |
| 32 IdentifierNode identifier; | |
| 33 | |
| 34 TopLevelNode(this.identifier); | |
| 35 } | |
| 36 | |
| 37 class ServiceNode extends TopLevelNode { | |
| 38 List<FunctionNode> functions; | |
| 39 | |
| 40 ServiceNode(IdentifierNode identifier, this.functions) | |
| 41 : super(identifier); | |
| 42 | |
| 43 void accept(NodeVisitor visitor) { | |
| 44 visitor.visitService(this); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 class StructNode extends TopLevelNode { | |
| 49 List<MemberNode> members; | |
| 50 | |
| 51 StructNode(IdentifierNode identifier, this.members) | |
| 52 : super(identifier); | |
| 53 | |
| 54 void accept(NodeVisitor visitor) { | |
| 55 visitor.visitStruct(this); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 // Definition level nodes. | |
| 60 class FunctionNode extends Node { | |
| 61 TypeNode returnType; | |
| 62 IdentifierNode identifier; | |
| 63 List<FormalNode> formals; | |
| 64 | |
| 65 FunctionNode(this.returnType, this.identifier, this.formals); | |
| 66 | |
| 67 void accept(NodeVisitor visitor) { | |
| 68 visitor.visitFunction(this); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 class FormalNode extends Node { | |
| 73 TypeNode type; | |
| 74 IdentifierNode identifier; | |
| 75 | |
| 76 FormalNode(this.type, this.identifier); | |
| 77 | |
| 78 void accept(NodeVisitor visitor) { | |
| 79 visitor.visitFormal(this); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 class UnionNode extends MemberNode { | |
| 84 List<FieldNode> fields; | |
| 85 | |
| 86 UnionNode(this.fields); | |
| 87 | |
| 88 void accept(NodeVisitor visitor) { | |
| 89 visitor.visitUnion(this); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 class FieldNode extends MemberNode { | |
| 94 TypeNode type; | |
| 95 IdentifierNode identifier; | |
| 96 | |
| 97 FieldNode(this.type, this.identifier); | |
| 98 | |
| 99 void accept(NodeVisitor visitor) { | |
| 100 visitor.visitField(this); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 abstract class TypeNode extends Node { | |
| 105 IdentifierNode identifier; | |
| 106 | |
| 107 TypeNode(this.identifier); | |
| 108 | |
| 109 bool isList() => false; | |
| 110 bool isPrimitive() => false; | |
| 111 bool isString() => false; | |
| 112 bool isStruct() => false; | |
| 113 bool isPointer() => false; | |
| 114 | |
| 115 void resolve(Map<IdentifierNode, StructNode> structs); | |
| 116 } | |
| 117 | |
| 118 // A node that can be the member of a struct. | |
| 119 abstract class MemberNode extends Node { | |
| 120 } | |
| 121 | |
| 122 | |
| 123 class SimpleType extends TypeNode { | |
| 124 TypeKind _type; | |
| 125 StructNode _resolved; | |
| 126 StructNode get resolved => _resolved; | |
| 127 | |
| 128 SimpleType(IdentifierNode identifier) | |
| 129 : super(identifier); | |
| 130 | |
| 131 void accept(NodeVisitor visitor) { | |
| 132 visitor.visitSimpleType(this); | |
| 133 } | |
| 134 | |
| 135 bool isPrimitive() => primitiveTypes.contains(_type); | |
| 136 bool isString() => TypeKind.STRING == _type; | |
| 137 bool isStruct() => TypeKind.STRUCT == _type; | |
| 138 | |
| 139 void resolve(Map<IdentifierNode, StructNode> structs) { | |
| 140 _type = lookupType(identifier.value); | |
| 141 if (!isPrimitive() && !isString()) { | |
| 142 _resolved = structs[identifier]; | |
| 143 if (null != _resolved) { | |
| 144 _type = TypeKind.STRUCT; | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 class PointerType extends TypeNode { | |
| 151 final TypeKind _type = TypeKind.POINTER; | |
| 152 TypeNode pointee; | |
| 153 | |
| 154 PointerType(TypeNode pointee) | |
| 155 : super(new IdentifierNode(pointee.identifier.token)) { | |
| 156 // TODO(stanm): generate a token with a pointer name | |
| 157 this.pointee = pointee; | |
| 158 } | |
| 159 | |
| 160 bool isPointer() => true; | |
| 161 bool pointeeResolves() => pointee.isStruct(); | |
| 162 | |
| 163 void accept(NodeVisitor visitor) { | |
| 164 visitor.visitPointerType(this); | |
| 165 } | |
| 166 | |
| 167 void resolve(Map<IdentifierNode, StructNode> structs) { | |
| 168 pointee.resolve(structs); | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 class ListType extends TypeNode { | |
| 173 TypeKind _type; | |
| 174 TypeNode typeParameter; | |
| 175 | |
| 176 ListType(IdentifierNode identifier, this.typeParameter) | |
| 177 : super(identifier); | |
| 178 | |
| 179 void accept(NodeVisitor visitor) { | |
| 180 visitor.visitListType(this); | |
| 181 } | |
| 182 | |
| 183 bool isList() => TypeKind.LIST == _type; | |
| 184 | |
| 185 void resolve(Map<IdentifierNode, StructNode> structs) { | |
| 186 _type = lookupType(identifier.value); | |
| 187 typeParameter.resolve(structs); | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 class IdentifierNode extends Node { | |
| 192 Token token; | |
| 193 String get value => token.value; | |
| 194 | |
| 195 IdentifierNode(this.token); | |
| 196 | |
| 197 int get hashCode => value.hashCode; | |
| 198 bool operator ==(IdentifierNode other) => value == other.value; | |
| 199 | |
| 200 String toString() => "Identifier[$value]"; | |
| 201 | |
| 202 void accept(NodeVisitor visitor) { | |
| 203 visitor.visitIdentifier(this); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 // Marker nodes. | |
| 208 abstract class MarkerNode extends Node { | |
| 209 void accept(NodeVisitor visitor) { | |
| 210 throw new InternalCompilerError("MarkerNode visited"); | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 /// Marks a point on the stack where type parsing was started. | |
| 215 class BeginTypeMarker extends MarkerNode { | |
| 216 } | |
| 217 | |
| 218 abstract class Node { | |
| 219 void accept(NodeVisitor visitor); | |
| 220 } | |
| 221 | |
| 222 // Visitor class | |
| 223 abstract class NodeVisitor { | |
| 224 void visitCompilationUnit(CompilationUnitNode compilationUnit); | |
| 225 void visitService(ServiceNode service); | |
| 226 void visitStruct(StructNode struct); | |
| 227 void visitFunction(FunctionNode function); | |
| 228 void visitUnion(UnionNode union); | |
| 229 void visitField(FieldNode field); | |
| 230 | |
| 231 // Structural/syntactic classification of types. | |
| 232 void visitSimpleType(SimpleType type); | |
| 233 void visitPointerType(PointerType type); | |
| 234 void visitListType(ListType type); | |
| 235 void visitFormal(FormalNode formal); | |
| 236 | |
| 237 // Functional/semantic classification of types. | |
| 238 void visitTypeParameter(TypeNode type); | |
| 239 void visitReturnType(TypeNode type); | |
| 240 | |
| 241 void visitIdentifier(IdentifierNode identifier); | |
| 242 | |
| 243 void visitError(ErrorNode error); | |
| 244 } | |
| 245 | |
| 246 abstract class RecursiveVisitor extends NodeVisitor { | |
| 247 void visitCompilationUnit(CompilationUnitNode compilationUnit) { | |
| 248 for (TopLevelNode topLevel in compilationUnit.topLevels) { | |
| 249 topLevel.accept(this); | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 void visitService(ServiceNode service) { | |
| 254 if (service.identifier != null) service.identifier.accept(this); | |
| 255 for (FunctionNode function in service.functions) { | |
| 256 function.accept(this); | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 void visitStruct(StructNode struct) { | |
| 261 if (struct.identifier != null) struct.identifier.accept(this); | |
| 262 for (MemberNode member in struct.members) { | |
| 263 member.accept(this); | |
| 264 } | |
| 265 } | |
| 266 | |
| 267 void visitFunction(FunctionNode function) { | |
| 268 visitReturnType(function.returnType); | |
| 269 if (function.identifier != null) function.identifier.accept(this); | |
| 270 for (FormalNode formal in function.formals) { | |
| 271 formal.accept(this); | |
| 272 } | |
| 273 } | |
| 274 | |
| 275 void visitFormal(FormalNode formal) { | |
| 276 if (formal.type != null) formal.type.accept(this); | |
| 277 if (formal.identifier != null) formal.identifier.accept(this); | |
| 278 } | |
| 279 | |
| 280 void visitUnion(UnionNode union) { | |
| 281 for (FieldNode field in union.fields) { | |
| 282 field.accept(this); | |
| 283 } | |
| 284 } | |
| 285 | |
| 286 void visitField(FieldNode field) { | |
| 287 if (field.type != null) field.type.accept(this); | |
| 288 if (field.identifier != null) field.identifier.accept(this); | |
| 289 } | |
| 290 | |
| 291 void visitReturnType(TypeNode type) { | |
| 292 // No op. | |
| 293 } | |
| 294 | |
| 295 void visitTypeParameter(TypeNode type) { | |
| 296 // No op. | |
| 297 } | |
| 298 | |
| 299 void visitSimpleType(SimpleType type) { | |
| 300 // No op. | |
| 301 } | |
| 302 | |
| 303 void visitPointerType(PointerType pointer) { | |
| 304 // No op. | |
| 305 } | |
| 306 | |
| 307 void visitListType(ListType list) { | |
| 308 visitTypeParameter(list.typeParameter); | |
| 309 } | |
| 310 | |
| 311 void visitIdentifier(IdentifierNode identifier) { | |
| 312 // No op. | |
| 313 } | |
| 314 | |
| 315 void visitError(ErrorNode error) { | |
| 316 // No op. | |
| 317 } | |
| 318 } | |
| OLD | NEW |