| 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.resolver; | |
| 6 | |
| 7 import 'dart:core' hide Type; | |
| 8 | |
| 9 import 'parser.dart'; | |
| 10 import 'primitives.dart' as primitives; | |
| 11 import 'struct_layout.dart'; | |
| 12 | |
| 13 void resolve(Unit unit) { | |
| 14 Definer definer = new Definer(); | |
| 15 definer.visit(unit); | |
| 16 Resolver resolver = new Resolver(definer.definitions); | |
| 17 resolver.visit(unit); | |
| 18 resolver.resolveAllStructs(); | |
| 19 } | |
| 20 | |
| 21 class Definer extends ResolutionVisitor { | |
| 22 final Map<String, Node> definitions = <String, Node>{}; | |
| 23 | |
| 24 visitService(Service node) { | |
| 25 define(node.name, node); | |
| 26 super.visitService(node); | |
| 27 } | |
| 28 | |
| 29 visitStruct(Struct node) { | |
| 30 define(node.name, node); | |
| 31 super.visitStruct(node); | |
| 32 } | |
| 33 | |
| 34 void define(String name, Node node) { | |
| 35 if (definitions.containsKey(name)) { | |
| 36 throw "Multiple definitions for $name"; | |
| 37 } | |
| 38 definitions[name] = node; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 class Resolver extends ResolutionVisitor { | |
| 43 final Map<String, Node> definitions; | |
| 44 final Map<Struct, Set<Struct>> dependencyMap = <Struct, Set<Struct>>{}; | |
| 45 Resolver(this.definitions); | |
| 46 | |
| 47 void resolveAllStructs() { | |
| 48 for (Struct struct in dependencyMap.keys) { | |
| 49 resolveStruct(struct, new Set<Struct>()); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 void resolveStruct(Struct struct, Set<Struct> visited) { | |
| 54 if (visited.contains(struct)) { | |
| 55 if (struct.layout != null) return; | |
| 56 throw new UnsupportedError("Cyclic struct graph at ${struct.name}."); | |
| 57 } | |
| 58 | |
| 59 visited.add(struct); | |
| 60 Set<Struct> dependencies = dependencyMap[struct]; | |
| 61 dependencies.forEach((Struct each) { resolveStruct(each, visited); }); | |
| 62 struct.layout = new StructLayout(struct); | |
| 63 } | |
| 64 | |
| 65 visitMethod(Method node) { | |
| 66 super.visitMethod(node); | |
| 67 | |
| 68 if (node.returnType.isPrimitive) { | |
| 69 node.outputKind = OutputKind.PRIMITIVE; | |
| 70 } else { | |
| 71 node.outputKind = OutputKind.STRUCT; | |
| 72 if (!node.returnType.isPointer) { | |
| 73 throw new UnsupportedError("Cannot return structs by value."); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 List<Formal> arguments = node.arguments; | |
| 78 if (arguments.any((e) => !e.type.isPrimitive && !e.type.isPointer)) { | |
| 79 throw new UnsupportedError("Cannot pass structs by value as arguments."); | |
| 80 } | |
| 81 | |
| 82 if (arguments.any((e) => e.type.isVoid)) { | |
| 83 throw new UnsupportedError("Cannot pass void as argument."); | |
| 84 } | |
| 85 | |
| 86 if (arguments.length == 1) { | |
| 87 node.inputKind = arguments[0].type.isPrimitive | |
| 88 ? InputKind.PRIMITIVES | |
| 89 : InputKind.STRUCT; | |
| 90 } else if (arguments.any((Formal each) => !each.type.isPrimitive)) { | |
| 91 throw new UnsupportedError("Methods accepting multiple arguments can " | |
| 92 "only take primitive values."); | |
| 93 } else { | |
| 94 node.inputKind = InputKind.PRIMITIVES; | |
| 95 } | |
| 96 | |
| 97 if (node.inputKind == InputKind.PRIMITIVES) { | |
| 98 StructLayout layout = new StructLayout.forArguments(arguments); | |
| 99 node.inputPrimitiveStructLayout = layout; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 visitStruct(Struct node) { | |
| 104 super.visitStruct(node); | |
| 105 | |
| 106 Iterable<Struct> computeDependencies(Iterable<Formal> slots) { | |
| 107 return slots | |
| 108 .where((Formal slot) => !slot.type.isPointer) | |
| 109 .where((Formal slot) => !slot.type.isPrimitive && | |
| 110 !slot.type.isList && | |
| 111 !slot.type.isString) | |
| 112 .map((Formal slot) => definitions[slot.type.identifier]); | |
| 113 } | |
| 114 | |
| 115 Set<Struct> dependencies = dependencyMap[node] = | |
| 116 computeDependencies(node.slots).toSet(); | |
| 117 | |
| 118 if (node.slots.any((Formal slot) => slot.type.isVoid)) { | |
| 119 throw new UnsupportedError("Cannot have void slots in struct"); | |
| 120 } | |
| 121 | |
| 122 if (node.unions.isNotEmpty) { | |
| 123 if (node.unions.length != 1) { | |
| 124 throw new UnsupportedError("Structs can have at most one union"); | |
| 125 } | |
| 126 Union union = node.unions.single; | |
| 127 union.struct = node; | |
| 128 node.slots.add(union.tag); | |
| 129 dependencies.addAll(computeDependencies(union.slots)); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 void resolveType(Type node) { | |
| 134 if (node.isString) return; | |
| 135 primitives.PrimitiveType primitiveType = primitives.lookup(node.identifier); | |
| 136 if (primitiveType != null) { | |
| 137 node.primitiveType = primitiveType; | |
| 138 } else { | |
| 139 String type = node.identifier; | |
| 140 if (definitions.containsKey(type)) { | |
| 141 node.resolved = definitions[type]; | |
| 142 } else { | |
| 143 throw new UnsupportedError("Cannot deal with type $type"); | |
| 144 } | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 visitFormal(Formal node) { | |
| 149 super.visitFormal(node); | |
| 150 if (node.type.isList) { | |
| 151 ListType listType = node.type; | |
| 152 if (listType.elementType.isPointer) { | |
| 153 throw new UnsupportedError("Cannot handle lists of pointers"); | |
| 154 } | |
| 155 } else if (node.type.isPointer) { | |
| 156 if (node.type.isPrimitive) { | |
| 157 throw new UnsupportedError("Cannot handle pointers to primitive types"); | |
| 158 } | |
| 159 } | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 class ResolutionVisitor extends Visitor { | |
| 164 visit(Node node) => node.accept(this); | |
| 165 | |
| 166 visitUnit(Unit node) { | |
| 167 node.services.forEach(visit); | |
| 168 node.structs.forEach(visit); | |
| 169 } | |
| 170 | |
| 171 visitService(Service node) { | |
| 172 node.methods.forEach(visit); | |
| 173 } | |
| 174 | |
| 175 visitStruct(Struct node) { | |
| 176 node.slots.forEach(visit); | |
| 177 node.unions.forEach(visit); | |
| 178 } | |
| 179 | |
| 180 visitUnion(Union node) { | |
| 181 visit(node.tag); | |
| 182 node.slots.forEach(visit); | |
| 183 } | |
| 184 | |
| 185 visitMethod(Method node) { | |
| 186 node.arguments.forEach(visit); | |
| 187 resolveType(node.returnType); | |
| 188 } | |
| 189 | |
| 190 visitFormal(Formal node) { | |
| 191 resolveType(node.type); | |
| 192 } | |
| 193 | |
| 194 void resolveType(Type node) { | |
| 195 } | |
| 196 } | |
| OLD | NEW |