From 78c78c0636503de0ebfdeb8549eba9f4ed92b378 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 28 Jul 2015 15:26:40 -0700 Subject: 1872 --- 048typecheck.cc | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 048typecheck.cc (limited to '048typecheck.cc') 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 > 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 >& 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(); +} -- cgit 1.4.1-2-gfad0 ame='id' value='423105085a90e706f34f9b4b2fb75169246bc71e'/>