1 //: Some simple sanity checks for types, and also attempts to guess them where
  2 //: they aren't provided.
  3 //:
  4 //: You still have to provide the full type the first time you mention a
  5 //: variable in a recipe. You have to explicitly name :offset and :variant
  6 //: every single time. You can't use the same name with multiple types in a
  7 //: single recipe.
  8 
  9 :(scenario transform_fails_on_reusing_name_with_different_type)
 10 % Hide_errors = true;
 11 def main [
 12   x:num <- copy 1
 13   x:bool <- copy 1
 14 ]
 15 +error: main: 'x' used with multiple types
 16 
 17 :(after "Transform.push_back(expand_type_abbreviations)")
 18 Transform.push_back(check_or_set_types_by_name);  // idempotent
 19 
 20 :(code)
 21 void check_or_set_types_by_name(const recipe_ordinal r) {
 22   trace(9991, "transform") << "--- deduce types for recipe " << get(Recipe, r).name << end();
 23   recipe& caller = get(Recipe, r);
 24   set<reagent> known;
 25   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
 26   ¦ instruction& inst = caller.steps.at(i);
 27   ¦ for (int in = 0;  in < SIZE(inst.ingredients);  ++in) {
 28   ¦ ¦ deduce_missing_type(known, inst.ingredients.at(in));
 29   ¦ ¦ check_type(known, inst.ingredients.at(in), caller);
 30   ¦ }
 31   ¦ for (int out = 0;  out < SIZE(inst.products);  ++out) {
 32   ¦ ¦ deduce_missing_type(known, inst.products.at(out));
 33   ¦ ¦ check_type(known, inst.products.at(out), caller);
 34   ¦ }
 35   }
 36 }
 37 
 38 void deduce_missing_type(set<reagent>& known, reagent& x) {
 39   if (x.type) return;
 40   if (is_jump_target(x.name)) {
 41   ¦ x.type = new type_tree("label");
 42   ¦ return;
 43   }
 44   if (known.find(x) == known.end()) return;
 45   x.type = new type_tree(*known.find(x)->type);
 46   trace(9992, "transform") << x.name << " <= " << names_to_string(x.type) << end();
 47 }
 48 
 49 void check_type(set<reagent>& known, const reagent& x, const recipe& caller) {
 50   if (is_literal(x)) return;
 51   if (is_integer(x.name)) return;  // if you use raw locations you're probably doing something unsafe
 52   if (!x.type) return;  // might get filled in by other logic later
 53   if (is_jump_target(x.name)) {
 54   ¦ if (!x.type->atom || x.type->name != "label")
 55   ¦ ¦ raise << maybe(caller.name) << "non-label '" << x.name << "' must begin with a letter\n" << end();
 56   ¦ return;
 57   }
 58   if (known.find(x) == known.end()) {
 59   ¦ trace(9992, "transform") << x.name << " => " << names_to_string(x.type) << end();
 60   ¦ known.insert(x);
 61   }
 62   if (!types_strictly_match(known.find(x)->type, x.type)) {
 63   ¦ raise << maybe(caller.name) << "'" << x.name << "' used with multiple types\n" << end();
 64   ¦ return;
 65   }
 66   if (is_mu_array(x)) {
 67   ¦ if (!x.type->right) {
 68   ¦ ¦ raise << maybe(caller.name) << "'" << x.name << ": can't be just an array. What is it an array of?\n" << end();
 69   ¦ ¦ return;
 70   ¦ }
 71   ¦ if (!x.type->right->right) {
 72   ¦ ¦ raise << caller.name << " can't determine the size of array variable '" << x.name << "'. Either allocate it separately and make the type of '" << x.name << "' an address, or specify the length of the array in the type of '" << x.name << "'.\n" << end();
 73   ¦ ¦ return;
 74   ¦ }
 75   }
 76 }
 77 
 78 :(scenario transform_fills_in_missing_types)
 79 def main [
 80   x:num <- copy 1
 81   y:num <- add x, 1
 82 ]
 83 # x is in location 1, y in location 2
 84 +mem: storing 2 in location 2
 85 
 86 :(scenario transform_fills_in_missing_types_in_product)
 87 def main [
 88   x:num <- copy 1
 89   x <- copy 2
 90 ]
 91 # x is in location 1
 92 +mem: storing 2 in location 1
 93 
 94 :(scenario transform_fills_in_missing_types_in_product_and_ingredient)
 95 def main [
 96   x:num <- copy 1
 97   x <- add x, 1
 98 ]
 99 # x is in location 1
100 +mem: storing 2 in location 1
101 
102 :(scenario transform_fills_in_missing_label_type)
103 def main [
104   jump +target
105   1:num <- copy 0
106   +target
107 ]
108 -mem: storing 0 in location 1
109 
110 :(scenario transform_fails_on_missing_types_in_first_mention)
111 % Hide_errors = true;
112 def main [
113   x <- copy 1
114   x:num <- copy 2
115 ]
116 +error: main: missing type for 'x' in 'x <- copy 1'
117 
118 :(scenario transform_fails_on_wrong_type_for_label)
119 % Hide_errors = true;
120 def main [
121   +foo:num <- copy 34
122 ]
123 +error: main: non-label '+foo' must begin with a letter
124 
125 :(scenario typo_in_address_type_fails)
126 % Hide_errors = true;
127 def main [
128   y:&:charcter <- new character:type
129   *y <- copy 67
130 ]
131 +error: main: unknown type charcter in 'y:&:charcter <- new character:type'
132 
133 :(scenario array_type_without_size_fails)
134 % Hide_errors = true;
135 def main [
136   x:@:num <- merge 2, 12, 13
137 ]
138 +error: main can't determine the size of array variable 'x'. Either allocate it separately and make the type of 'x' an address, or specify the length of the array in the type of 'x'.
139 
140 :(scenarios transform)
141 :(scenario transform_checks_types_of_identical_reagents_in_multiple_spaces)
142 def foo [  # dummy
143 ]
144 def main [
145   local-scope
146   0:space/names:foo <- copy 0  # specify surrounding space
147   x:bool <- copy 1/true
148   x:num/space:1 <- copy 34
149   x/space:1 <- copy 35
150 ]
151 $error: 0
152 
153 :(scenario transform_handles_empty_reagents)
154 % Hide_errors = true;
155 def main [
156   add *
157 ]
158 +error: illegal name '*'
159 # no crash