about summary refs log tree commit diff stats
path: root/048typecheck.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-07-28 15:26:40 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-07-28 15:26:47 -0700
commit78c78c0636503de0ebfdeb8549eba9f4ed92b378 (patch)
treed18d663e6877973453cb8b1e26f03eac38d2748c /048typecheck.cc
parent805fde84245f36594731b2a4e1c1628a0a92cadf (diff)
downloadmu-78c78c0636503de0ebfdeb8549eba9f4ed92b378.tar.gz
1872
Diffstat (limited to '048typecheck.cc')
-rw-r--r--048typecheck.cc39
1 files changed, 39 insertions, 0 deletions
diff --git a/048typecheck.cc b/048typecheck.cc
new file mode 100644
index 00000000..7da0190a
--- /dev/null
+++ b/048typecheck.cc
@@ -0,0 +1,39 @@
+//: Some simple sanity checks for types, and also attempts to guess them where
+//: they aren't provided.
+
+:(scenario transform_types_warns_on_reusing_name_with_different_type)
+% Hide_warnings = true;
+recipe main [
+  x:number <- copy 1
+  x:boolean <- copy 1
+]
++warn: x used with multiple types in main
+
+:(after "int main")
+  Transform.push_back(transform_types);
+
+:(code)
+void transform_types(const recipe_ordinal r) {
+  map<string, vector<type_ordinal> > metadata;
+  for (long long int i = 0; i < SIZE(Recipe[r].steps); ++i) {
+    instruction& inst = Recipe[r].steps.at(i);
+    for (long long int in = 0; in < SIZE(inst.ingredients); ++in) {
+      check_metadata(metadata, inst.ingredients.at(in), r);
+    }
+    for (long long int out = 0; out < SIZE(inst.products); ++out) {
+      check_metadata(metadata, inst.products.at(out), r);
+    }
+  }
+}
+
+void check_metadata(map<string, vector<type_ordinal> >& metadata, const reagent& x, const recipe_ordinal r) {
+  if (is_literal(x)) return;
+  if (is_raw(x)) return;
+  // if you use raw locations you're probably doing something unsafe
+  if (is_integer(x.name)) return;
+  if (x.types.empty()) return;  // will throw a more precise warning elsewhere
+  if (metadata.find(x.name) == metadata.end())
+    metadata[x.name] = x.types;
+  if (metadata[x.name] != x.types)
+    raise << x.name << " used with multiple types in " << Recipe[r].name << '\n' << end();
+}