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