# Mu - 046check_type_by_name.cc

      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 //: we need surrounding-space info for type-checking variables in other spaces
     18 :(after "Transform.push_back(collect_surrounding_spaces)")
     19 Transform.push_back(check_or_set_types_by_name);  // idempotent
     20
     21 // Keep the name->type mapping for all recipes around for the entire
     22 // transformation phase.
     23 :(before "End Globals")
     24 map<recipe_ordinal, set<reagent, name_lt> > Types_by_space;  // internal to transform; no need to snapshot
     25 :(before "End Reset")
     26 Types_by_space.clear();
     27 :(before "End transform_all")
     28 Types_by_space.clear();
     29 :(before "End Types")
     30 struct name_lt {
     31   bool operator()(const reagent& a, const reagent& b) const { return a.name < b.name; }
     32 };
     33
     34 :(code)
     35 void check_or_set_types_by_name(const recipe_ordinal r) {
     36   recipe& caller = get(Recipe, r);
     37   trace(9991, "transform") << "--- deduce types for recipe " << caller.name << end();
     38   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
     39     instruction& inst = caller.steps.at(i);
     40     for (int in = 0;  in < SIZE(inst.ingredients);  ++in)
     41       check_or_set_type(inst.ingredients.at(in), caller);
     42     for (int out = 0;  out < SIZE(inst.products);  ++out)
     43       check_or_set_type(inst.products.at(out), caller);
     44   }
     45 }
     46
     47 void check_or_set_type(reagent& curr, const recipe& caller) {
     48   if (is_literal(curr)) return;
     49   if (is_integer(curr.name)) return;  // no type-checking for raw locations
     50   set<reagent, name_lt>& known_types = Types_by_space[owning_recipe(curr, caller.ordinal)];
     51   deduce_missing_type(known_types, curr, caller);
     52   check_type(known_types, curr, caller);
     53 }
     54
     55 void deduce_missing_type(set<reagent, name_lt>& known_types, reagent& x, const recipe& caller) {
     56   // Deduce Missing Type(x, caller)
     57   if (x.type) return;
     58   if (is_jump_target(x.name)) {
     59     x.type = new type_tree("label");
     60     return;
     61   }
     62   if (known_types.find(x) == known_types.end()) return;
     63   const reagent& exemplar = *known_types.find(x);
     64   x.type = new type_tree(*exemplar.type);
     65   trace(9992, "transform") << x.name << " <= " << names_to_string(x.type) << end();
     66   // spaces are special; their type includes their /names property
     67   if (is_mu_space(x) && !has_property(x, "names")) {
     68     if (!has_property(exemplar, "names")) {
     69       raise << maybe(caller.name) << "missing /names property for space variable '" << exemplar.name << "'\n" << end();
     70       return;
     71     }
     72     x.properties.push_back(pair<string, string_tree*>("names", new string_tree(*property(exemplar, "names"))));
     73   }
     74 }
     75
     76 void check_type(set<reagent, name_lt>& known_types, const reagent& x, const recipe& caller) {
     77   if (is_literal(x)) return;
     78   if (!x.type) return;  // might get filled in by other logic later
     79   if (is_jump_target(x.name)) {
     80     if (!x.type->atom || x.type->name != "label")
     81       raise << maybe(caller.name) << "non-label '" << x.name << "' must begin with a letter\n" << end();
     82     return;
     83   }
     84   if (known_types.find(x) == known_types.end()) {
     85     trace(9992, "transform") << x.name << " => " << names_to_string(x.type) << end();
     86     known_types.insert(x);
     87   }
     88   if (!types_strictly_match(known_types.find(x)->type, x.type)) {
     89     raise << maybe(caller.name) << "'" << x.name << "' used with multiple types\n" << end();
     90     raise << "  " << to_string(known_types.find(x)->type) << " vs " << to_string(x.type) << '\n' << end();
     91     return;
     92   }
     93   if (is_mu_array(x)) {
     94     if (!x.type->right) {
     95       raise << maybe(caller.name) << "'" << x.name << ": can't be just an array. What is it an array of?\n" << end();
     96       return;
     97     }
     98     if (!x.type->right->right) {
     99       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();
    100       return;
    101     }
    102   }
    103 }
    104
    105 recipe_ordinal owning_recipe(const reagent& x, recipe_ordinal r) {
    106   for (int s = space_index(x); s > 0; --s) {
    107     if (!contains_key(Surrounding_space, r)) break;  // error raised elsewhere
    108     r = Surrounding_space[r];
    109   }
    110   return r;
    111 }
    112
    113 :(scenario transform_fills_in_missing_types)
    114 def main [
    115   x:num <- copy 11
    116   y:num <- add x, 1
    117 ]
    118 # x is in location 2, y in location 3
    119 +mem: storing 12 in location 3
    120
    121 :(scenario transform_fills_in_missing_types_in_product)
    122 def main [
    123   x:num <- copy 11
    124   x <- copy 12
    125 ]
    126 # x is in location 2
    127 +mem: storing 12 in location 2
    128
    129 :(scenario transform_fills_in_missing_types_in_product_and_ingredient)
    130 def main [
    131   x:num <- copy 11
    132   x <- add x, 1
    133 ]
    134 # x is in location 2
    135 +mem: storing 12 in location 2
    136
    137 :(scenario transform_fills_in_missing_label_type)
    138 def main [
    139   jump +target
    140   1:num <- copy 0
    141   +target
    142 ]
    143 -mem: storing 0 in location 1
    144
    145 :(scenario transform_fails_on_missing_types_in_first_mention)
    146 % Hide_errors = true;
    147 def main [
    148   x <- copy 1
    149   x:num <- copy 2
    150 ]
    151 +error: main: missing type for 'x' in 'x <- copy 1'
    152
    153 :(scenario transform_fails_on_wrong_type_for_label)
    154 % Hide_errors = true;
    155 def main [
    156   +foo:num <- copy 34
    157 ]
    158 +error: main: non-label '+foo' must begin with a letter
    159
    160 :(scenario typo_in_address_type_fails)
    161 % Hide_errors = true;
    162 def main [
    163   y:&:charcter <- new character:type
    164   *y <- copy 67
    165 ]
    166 +error: main: unknown type charcter in 'y:&:charcter <- new character:type'
    167
    168 :(scenario array_type_without_size_fails)
    169 % Hide_errors = true;
    170 def main [
    171   x:@:num <- merge 2, 12, 13
    172 ]
    173 +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'.
    174
    175 :(scenarios transform)
    176 :(scenario transform_checks_types_of_identical_reagents_in_multiple_spaces)
    177 def foo [  # dummy
    178 ]
    179 def main [
    180   local-scope
    181   0:space/names:foo <- copy null  # specify surrounding space
    182   x:bool <- copy true
    183   x:num/space:1 <- copy 34
    184   x/space:1 <- copy 35
    185 ]
    186 $error: 0
    187
    188 :(scenario transform_handles_empty_reagents)
    189 % Hide_errors = true;
    190 def main [
    191   add *
    192 ]
    193 +error: illegal name '*'
    194 # no crash
    195
    196 :(scenario transform_checks_types_in_surrounding_spaces)
    197 % Hide_errors = true;
    198 # 'x' is a bool in foo's space
    199 def foo [
    200   local-scope
    201   x:bool <- copy false
    202   return default-space/names:foo
    203 ]
    204 # try to read 'x' as a num in foo's space
    205 def main [
    206   local-scope
    207   0:space/names:foo <- foo
    208   x:num/space:1 <- copy 34
    209 ]
    210 error: foo: 'x' used with multiple types