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), caller);
 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), caller);
 33   ¦ ¦ check_type(known, inst.products.at(out), caller);
 34   ¦ }
 35   }
 36 }
 37 
 38 void deduce_missing_type(set<reagent>& known, reagent& x, const recipe& caller) {
 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   const reagent& exemplar = *known.find(x);
 46   x.type = new type_tree(*exemplar.type);
 47   trace(9992, "transform") << x.name << " <= " << names_to_string(x.type) << end();
 48   // spaces are special; their type includes their /names property
 49   if (is_mu_space(x) && !has_property(x, "names")) {
 50   ¦ if (!has_property(exemplar, "names")) {
 51   ¦ ¦ raise << maybe(caller.name) << "missing /names property for space variable '" << exemplar.name << "'\n" << end();
 52   ¦ ¦ return;
 53   ¦ }
 54   ¦ x.properties.push_back(pair<string, string_tree*>("names", new string_tree(*property(exemplar, "names"))));
 55   }
 56 }
 57 
 58 void check_type(set<reagent>& known, const reagent& x, const recipe& caller) {
 59   if (is_literal(x)) return;
 60   if (is_integer(x.name)) return;  // if you use raw locations you're probably doing something unsafe
 61   if (!x.type) return;  // might get filled in by other logic later
 62   if (is_jump_target(x.name)) {
 63   ¦ if (!x.type->atom || x.type->name != "label")
 64   ¦ ¦ raise << maybe(caller.name) << "non-label '" << x.name << "' must begin with a letter\n" << end();
 65   ¦ return;
 66   }
 67   if (known.find(x) == known.end()) {
 68   ¦ trace(9992, "transform") << x.name << " => " << names_to_string(x.type) << end();
 69   ¦ known.insert(x);
 70   }
 71   if (!types_strictly_match(known.find(x)->type, x.type)) {
 72   ¦ raise << maybe(caller.name) << "'" << x.name << "' used with multiple types\n" << end();
 73   ¦ return;
 74   }
 75   if (is_mu_array(x)) {
 76   ¦ if (!x.type->right) {
 77   ¦ ¦ raise << maybe(caller.name) << "'" << x.name << ": can't be just an array. What is it an array of?\n" << end();
 78   ¦ ¦ return;
 79   ¦ }
 80   ¦ if (!x.type->right->right) {
 81   ¦ ¦ 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();
 82   ¦ ¦ return;
 83   ¦ }
 84   }
 85 }
 86 
 87 :(scenario transform_fills_in_missing_types)
 88 def main [
 89   x:num <- copy 1
 90   y:num <- add x, 1
 91 ]
 92 # x is in location 1, y in location 2
 93 +mem: storing 2 in location 2
 94 
 95 :(scenario transform_fills_in_missing_types_in_product)
 96 def main [
 97   x:num <- copy 1
 98   x <- copy 2
 99 ]
100 # x is in location 1
101 +mem: storing 2 in location 1
102 
103 :(scenario transform_fills_in_missing_types_in_product_and_ingredient)
104 def main [
105   x:num <- copy 1
106   x <- add x, 1
107 ]
108 # x is in location 1
109 +mem: storing 2 in location 1
110 
111 :(scenario transform_fills_in_missing_label_type)
112 def main [
113   jump +target
114   1:num <- copy 0
115   +target
116 ]
117 -mem: storing 0 in location 1
118 
119 :(scenario transform_fails_on_missing_types_in_first_mention)
120 % Hide_errors = true;
121 def main [
122   x <- copy 1
123   x:num <- copy 2
124 ]
125 +error: main: missing type for 'x' in 'x <- copy 1'
126 
127 :(scenario transform_fails_on_wrong_type_for_label)
128 % Hide_errors = true;
129 def main [
130   +foo:num <- copy 34
131 ]
132 +error: main: non-label '+foo' must begin with a letter
133 
134 :(scenario typo_in_address_type_fails)
135 % Hide_errors = true;
136 def main [
137   y:&:charcter <- new character:type
138   *y <- copy 67
139 ]
140 +error: main: unknown type charcter in 'y:&:charcter <- new character:type'
141 
142 :(scenario array_type_without_size_fails)
143 % Hide_errors = true;
144 def main [
145   x:@:num <- merge 2, 12, 13
146 ]
147 +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'.
148 
149 :(scenarios transform)
150 :(scenario transform_checks_types_of_identical_reagents_in_multiple_spaces)
151 def foo [  # dummy
152 ]
153 def main [
154   local-scope
155   0:space/names:foo <- copy 0  # specify surrounding space
156   x:bool <- copy 1/true
157   x:num/space:1 <- copy 34
158   x/space:1 <- copy 35
159 ]
160 $error: 0
161 
162 :(scenario transform_handles_empty_reagents)
163 % Hide_errors = true;
164 def main [
165   add *
166 ]
167 +error: illegal name '*'
168 # no crash