<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 060immutable.cc</title>
<meta name="Generator" content="Vim/7.3">
<meta name="plugin-version" content="vim7.3_v6">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css">
<style type="text/css">
<!--
pre { font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
.cSpecial { color: #008000; }
.Todo { color: #000000; background-color: #ffff00; }
.Identifier { color: #fcb165; }
.traceContains { color: #008000; }
.Constant { color: #00a0a0; }
.Normal { color: #eeeeee; background-color: #080808; }
.Special { color: #c00000; }
.Delimiter { color: #800080; }
.Comment { color: #9090ff; }
-->
</style>
</head>
<body>
<pre>
<span class="Comment">//: Addresses passed into of a recipe are meant to be immutable unless they're</span>
<span class="Comment">//: also products. This layer will start enforcing this check.</span>
<span class="Comment">//:</span>
<span class="Comment">//: One hole for now: variables in surrounding spaces are implicitly mutable.</span>
<span class="Delimiter">:(scenario can_modify_value_ingredients)</span>
def main [
local-scope
p:address:shared:point<span class="Special"> <- </span><span class="Normal">new</span> point:type
foo *p
]
def foo p:point [
local-scope
load-ingredients
x:address:number<span class="Special"> <- </span>get-address p<span class="Delimiter">,</span> x:offset
*x<span class="Special"> <- </span>copy <span class="Constant">34</span>
]
$error: <span class="Constant">0</span>
<span class="Delimiter">:(scenario can_modify_ingredients_that_are_also_products)</span>
def main [
local-scope
p:address:shared:point<span class="Special"> <- </span><span class="Normal">new</span> point:type
p<span class="Special"> <- </span>foo p
]
def foo p:address:shared:point <span class="Delimiter">-></span> p:address:shared:point [
local-scope
load-ingredients
x:address:number<span class="Special"> <- </span>get-address *p<span class="Delimiter">,</span> x:offset
*x<span class="Special"> <- </span>copy <span class="Constant">34</span>
]
$error: <span class="Constant">0</span>
<span class="Delimiter">:(scenario ignore_literal_ingredients_for_immutability_checks)</span>
def main [
local-scope
p:address:shared:d1<span class="Special"> <- </span><span class="Normal">new</span> d1:type
q:number<span class="Special"> <- </span>foo p
]
def foo p:address:shared:d1 <span class="Delimiter">-></span> q:number [
local-scope
load-ingredients
x:address:shared:d1<span class="Special"> <- </span><span class="Normal">new</span> d1:type
y:address:number<span class="Special"> <- </span>get-address *x<span class="Delimiter">,</span> p:offset <span class="Comment"># ignore this 'p'</span>
q<span class="Special"> <- </span>copy <span class="Constant">34</span>
]
container d1 [
p:number
q:number
]
$error: <span class="Constant">0</span>
<span class="Delimiter">:(scenario cannot_modify_immutable_ingredients)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
local-scope
x:address:shared:number<span class="Special"> <- </span><span class="Normal">new</span> number:type
foo x
]
def foo x:address:shared:number [
local-scope
load-ingredients
*x<span class="Special"> <- </span>copy <span class="Constant">34</span>
]
<span class="traceContains">+error: foo: cannot modify x in instruction '*x <- copy 34' because it's not also a product of foo</span>
<span class="Delimiter">:(scenario cannot_take_address_inside_immutable_ingredients)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
local-scope
p:address:shared:point<span class="Special"> <- </span><span class="Normal">new</span> point:type
foo p
]
def foo p:address:shared:point [
local-scope
load-ingredients
x:address:number<span class="Special"> <- </span>get-address *p<span class="Delimiter">,</span> x:offset
*x<span class="Special"> <- </span>copy <span class="Constant">34</span>
]
<span class="traceContains">+error: foo: cannot modify ingredient p after instruction 'x:address:number <- get-address *p, x:offset' because it's not also a product of foo</span>
<span class="Delimiter">:(scenario cannot_call_mutating_recipes_on_immutable_ingredients)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
local-scope
p:address:shared:point<span class="Special"> <- </span><span class="Normal">new</span> point:type
foo p
]
def foo p:address:shared:point [
local-scope
load-ingredients
bar p
]
def bar p:address:shared:point <span class="Delimiter">-></span> p:address:shared:point [
local-scope
load-ingredients
x:address:number<span class="Special"> <- </span>get-address *p<span class="Delimiter">,</span> x:offset
*x<span class="Special"> <- </span>copy <span class="Constant">34</span>
]
<span class="traceContains">+error: foo: cannot modify ingredient p at instruction 'bar p' because it's not also a product of foo</span>
<span class="Delimiter">:(scenario cannot_modify_copies_of_immutable_ingredients)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
local-scope
p:address:shared:point<span class="Special"> <- </span><span class="Normal">new</span> point:type
foo p
]
def foo p:address:shared:point [
local-scope
load-ingredients
q:address:shared:point<span class="Special"> <- </span>copy p
x:address:number<span class="Special"> <- </span>get-address *q<span class="Delimiter">,</span> x:offset
]
<span class="traceContains">+error: foo: cannot modify q after instruction 'x:address:number <- get-address *q, x:offset' because that would modify ingredient p which is not also a product of foo</span>
<span class="Delimiter">:(scenario can_modify_copies_of_mutable_ingredients)</span>
def main [
local-scope
p:address:shared:point<span class="Special"> <- </span><span class="Normal">new</span> point:type
foo p
]
def foo p:address:shared:point <span class="Delimiter">-></span> p:address:shared:point [
local-scope
load-ingredients
q:address:shared:point<span class="Special"> <- </span>copy p
x:address:number<span class="Special"> <- </span>get-address *q<span class="Delimiter">,</span> x:offset
]
$error: <span class="Constant">0</span>
<span class="Delimiter">:(scenario cannot_modify_address_inside_immutable_ingredients)</span>
<span class="Special">% Hide_errors = true;</span>
container foo [
x:address:shared:array:number <span class="Comment"># contains an address</span>
]
def main [
<span class="Comment"># don't run anything</span>
]
def foo a:address:shared:foo [
local-scope
load-ingredients
x:address:shared:array:number<span class="Special"> <- </span>get *a<span class="Delimiter">,</span> x:offset <span class="Comment"># just a regular get of the container</span>
y:address:number<span class="Special"> <- </span>index-address *x<span class="Delimiter">,</span> <span class="Constant">0</span> <span class="Comment"># but then index-address on the result</span>
*y<span class="Special"> <- </span>copy <span class="Constant">34</span>
]
<span class="traceContains">+error: foo: cannot modify x after instruction 'y:address:number <- index-address *x, 0' because that would modify ingredient a which is not also a product of foo</span>
<span class="Delimiter">:(scenario cannot_modify_address_inside_immutable_ingredients_2)</span>
container foo [
x:address:shared:array:number <span class="Comment"># contains an address</span>
]
def main [
<span class="Comment"># don't run anything</span>
]
def foo a:address:shared:foo [
local-scope
load-ingredients
b:foo<span class="Special"> <- </span>merge <span class="Constant">0</span> <span class="Comment"># completely unrelated to 'a'</span>
x:address:shared:array:number<span class="Special"> <- </span>get b<span class="Delimiter">,</span> x:offset <span class="Comment"># just a regular get of the container</span>
y:address:number<span class="Special"> <- </span>index-address *x<span class="Delimiter">,</span> <span class="Constant">0</span> <span class="Comment"># but then index-address on the result</span>
*y<span class="Special"> <- </span>copy <span class="Constant">34</span>
]
$error: <span class="Constant">0</span>
<span class="Delimiter">:(scenario cannot_modify_address_inside_immutable_ingredients_3)</span>
<span class="Special">% Hide_errors = true;</span>
container foo [
x:number
]
def main [
<span class="Comment"># don't run anything</span>
]
def foo a:address:shared:array:address:number [
local-scope
load-ingredients
x:address:number<span class="Special"> <- </span>index *a<span class="Delimiter">,</span> <span class="Constant">0</span> <span class="Comment"># just a regular index of the array</span>
*x<span class="Special"> <- </span>copy <span class="Constant">34</span> <span class="Comment"># but then modify the result</span>
]
<span class="Comment"># +error: foo: cannot modify x in instruction '*x <- copy 34' because that would modify ingredient a which is not also a product of foo</span>
<span class="traceContains">+error: foo: cannot modify x in instruction '*x <- copy 34' because it's not also a product of foo</span>
<span class="Delimiter">:(scenario cannot_modify_address_inside_immutable_ingredients_4)</span>
container foo [
x:address:shared:array:number <span class="Comment"># contains an address</span>
]
def main [
<span class="Comment"># don't run anything</span>
]
def foo a:address:shared:array:address:number [
local-scope
load-ingredients
b:address:shared:array:address:number<span class="Special"> <- </span><span class="Normal">new</span> <span class="Delimiter">{(</span>address number<span class="Delimiter">)</span>: type<span class="Delimiter">},</span> <span class="Constant">3</span> <span class="Comment"># completely unrelated to 'a'</span>
x:address:number<span class="Special"> <- </span>index *b<span class="Delimiter">,</span> <span class="Constant">0</span> <span class="Comment"># just a regular index of the array</span>
*x<span class="Special"> <- </span>copy <span class="Constant">34</span> <span class="Comment"># but then modify the result</span>
]
$error: <span class="Constant">0</span>
<span class="Delimiter">:(scenario can_traverse_immutable_ingredients)</span>
container test-list [
next:address:shared:test-list
]
def main [
local-scope
p:address:shared:test-list<span class="Special"> <- </span><span class="Normal">new</span> test-list:type
foo p
]
def foo p:address:shared:test-list [
local-scope
load-ingredients
p2:address:shared:test-list<span class="Special"> <- </span>bar p
]
def bar x:address:shared:test-list <span class="Delimiter">-></span> y:address:shared:test-list [
local-scope
load-ingredients
y<span class="Special"> <- </span>get *x<span class="Delimiter">,</span> next:offset
]
$error: <span class="Constant">0</span>
<span class="Delimiter">:(scenario handle_optional_ingredients_in_immutability_checks)</span>
def main [
k:address:shared:number<span class="Special"> <- </span><span class="Normal">new</span> number:type
test k
]
<span class="Comment"># recipe taking an immutable address ingredient</span>
def test k:address:shared:number [
local-scope
load-ingredients
foo k
]
<span class="Comment"># ..calling a recipe with an optional address ingredient</span>
def foo <span class="Delimiter">-></span> [
local-scope
load-ingredients
k:address:shared:number<span class="Delimiter">,</span> found?:boolean<span class="Special"> <- </span>next-ingredient
]
$error: <span class="Constant">0</span>
<span class="Comment">//: when checking for immutable ingredients, remember to take space into account</span>
<span class="Delimiter">:(scenario check_space_of_reagents_in_immutability_checks)</span>
def main [
a:address:shared:array:location<span class="Special"> <- </span><span class="Normal">new</span>-closure
b:address:shared:number<span class="Special"> <- </span><span class="Normal">new</span> number:type
run-closure b:address:shared:number<span class="Delimiter">,</span> a:address:shared:array:location
]
def <span class="Normal">new</span>-closure [
<span class="Normal">new</span>-<span class="Normal">default</span>-space
x:address:shared:number<span class="Special"> <- </span><span class="Normal">new</span> number:type
<span class="Identifier">return</span> <span class="Normal">default</span>-space
]
def run-closure x:address:shared:number<span class="Delimiter">,</span> s:address:shared:array:location [
local-scope
load-ingredients
<span class="Constant">0</span>:address:shared:array:location/names:<span class="Normal">new</span>-closure<span class="Special"> <- </span>copy s
*x:address:number/space:<span class="Constant">1</span><span class="Special"> <- </span>copy <span class="Constant">34</span>
]
$error: <span class="Constant">0</span>
<span class="Delimiter">:(before "End Transforms")</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>check_immutable_ingredients<span class="Delimiter">);</span> <span class="Comment">// idempotent</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> check_immutable_ingredients<span class="Delimiter">(</span>recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// to ensure an address reagent isn't modified, it suffices to show that</span>
<span class="Comment">// a) we never write to its contents directly,</span>
<span class="Comment">// b) we never call get-address or index-address with it, and</span>
<span class="Comment">// c) any non-primitive recipe calls in the body aren't returning it as a product</span>
<span class="Normal">const</span> recipe& caller = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"--- check mutability of ingredients in recipe "</span> << caller<span class="Delimiter">.</span>name << end<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!caller<span class="Delimiter">.</span>has_header<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span> <span class="Comment">// skip check for old-style recipes calling next-ingredient directly</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> reagent& current_ingredient = caller<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!is_mu_address<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Comment">// will be copied</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_present_in_products<span class="Delimiter">(</span>caller<span class="Delimiter">,</span> current_ingredient<span class="Delimiter">.</span>name<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Comment">// not expected to be immutable</span>
<span class="Comment">// End Immutable Ingredients Special-cases</span>
set<reagent> immutable_vars<span class="Delimiter">;</span>
immutable_vars<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">);</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> instruction& inst = caller<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
check_immutable_ingredient_in_instruction<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> immutable_vars<span class="Delimiter">,</span> current_ingredient<span class="Delimiter">.</span>name<span class="Delimiter">,</span> caller<span class="Delimiter">);</span>
update_aliases<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> immutable_vars<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> update_aliases<span class="Delimiter">(</span><span class="Normal">const</span> instruction& inst<span class="Delimiter">,</span> set<reagent>& current_ingredient_and_aliases<span class="Delimiter">)</span> <span class="Delimiter">{</span>
set<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>> current_ingredient_indices = ingredient_indices<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> current_ingredient_and_aliases<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>operation<span class="Delimiter">))</span> <span class="Delimiter">{</span>
<span class="Comment">// primitive recipe</span>
<span class="Normal">switch</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>operation<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">case</span> COPY:
<span class="Normal">for</span> <span class="Delimiter">(</span>set<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>>::iterator p = current_ingredient_indices<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != current_ingredient_indices<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span>
current_ingredient_and_aliases<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>*p<span class="Delimiter">).</span>name<span class="Delimiter">);</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Normal">case</span> GET:
<span class="Normal">case</span> INDEX:
<span class="Comment">// current_ingredient_indices can only have 0 or one value</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!current_ingredient_indices<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_mu_address<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span>
current_ingredient_and_aliases<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span>
<span class="Delimiter">}</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Normal">default</span>: <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
<span class="Comment">// defined recipe</span>
set<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>> contained_in_product_indices = scan_contained_in_product_indices<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> current_ingredient_indices<span class="Delimiter">);</span>
<span class="Normal">for</span> <span class="Delimiter">(</span>set<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>>::iterator p = contained_in_product_indices<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != contained_in_product_indices<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>*p < SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">))</span>
current_ingredient_and_aliases<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>*p<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
set<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>> scan_contained_in_product_indices<span class="Delimiter">(</span><span class="Normal">const</span> instruction& inst<span class="Delimiter">,</span> set<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>>& ingredient_indices<span class="Delimiter">)</span> <span class="Delimiter">{</span>
set<reagent> selected_ingredients<span class="Delimiter">;</span>
<span class="Normal">const</span> recipe& callee = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>operation<span class="Delimiter">);</span>
<span class="Normal">for</span> <span class="Delimiter">(</span>set<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>>::iterator p = ingredient_indices<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != ingredient_indices<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>*p >= SIZE<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> <span class="Comment">// optional immutable ingredient</span>
selected_ingredients<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>*p<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
set<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>> result<span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> reagent& current_product = callee<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
<span class="Comment">// </span><span class="Todo">TODO</span>
<span class="Normal">const</span> string_tree* contained_in_name = property<span class="Delimiter">(</span>current_product<span class="Delimiter">,</span> <span class="Constant">"contained-in"</span><span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>contained_in_name && selected_ingredients<span class="Delimiter">.</span>find<span class="Delimiter">(</span>contained_in_name<span class="Delimiter">-></span>value<span class="Delimiter">)</span> != selected_ingredients<span class="Delimiter">.</span>end<span class="Delimiter">())</span>
result<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenarios transform)</span>
<span class="Delimiter">:(scenario immutability_infects_contained_in_variables)</span>
<span class="Special">% Hide_errors = true;</span>
container test-list [
next:address:shared:test-list
]
def main [
local-scope
p:address:shared:test-list<span class="Special"> <- </span><span class="Normal">new</span> test-list:type
foo p
]
def foo p:address:shared:test-list [ <span class="Comment"># p is immutable</span>
local-scope
load-ingredients
p2:address:shared:test-list<span class="Special"> <- </span>test-next p <span class="Comment"># p2 is immutable</span>
p3:address:address:shared:test-list<span class="Special"> <- </span>get-address *p2<span class="Delimiter">,</span> next:offset <span class="Comment"># signal modification of p2</span>
]
def test-next x:address:shared:test-list <span class="Delimiter">-></span> y:address:shared:test-list/contained-in:x [
local-scope
load-ingredients
y<span class="Special"> <- </span>get *x<span class="Delimiter">,</span> next:offset
]
<span class="traceContains">+error: foo: cannot modify p2 after instruction 'p3:address:address:shared:test-list <- get-address *p2, next:offset' because that would modify ingredient p which is not also a product of foo</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> check_immutable_ingredient_in_instruction<span class="Delimiter">(</span><span class="Normal">const</span> instruction& inst<span class="Delimiter">,</span> <span class="Normal">const</span> set<reagent>& current_ingredient_and_aliases<span class="Delimiter">,</span> <span class="Normal">const</span> string& original_ingredient_name<span class="Delimiter">,</span> <span class="Normal">const</span> recipe& caller<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// first check if the instruction is directly modifying something it shouldn't</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>has_property<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> <span class="Constant">"lookup"</span><span class="Delimiter">)</span>
&& current_ingredient_and_aliases<span class="Delimiter">.</span>find<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))</span> != current_ingredient_and_aliases<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"cannot modify "</span> << inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name << <span class="Constant">" in instruction '"</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> << <span class="Constant">"' because it's not also a product of "</span> << caller<span class="Delimiter">.</span>name << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">// check if there's any indirect modification going on</span>
set<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>> current_ingredient_indices = ingredient_indices<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> current_ingredient_and_aliases<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>current_ingredient_indices<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span><span class="Delimiter">;</span> <span class="Comment">// ingredient not found in call</span>
<span class="Normal">for</span> <span class="Delimiter">(</span>set<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>>::iterator p = current_ingredient_indices<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != current_ingredient_indices<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> current_ingredient_index = *p<span class="Delimiter">;</span>
reagent current_ingredient = inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>current_ingredient_index<span class="Delimiter">);</span>
canonize_type<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">);</span>
<span class="Normal">const</span> string& current_ingredient_name = current_ingredient<span class="Delimiter">.</span>name<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>operation<span class="Delimiter">))</span> <span class="Delimiter">{</span>
<span class="Comment">// primitive recipe</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>operation == GET_ADDRESS || inst<span class="Delimiter">.</span>operation == INDEX_ADDRESS<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// only reason to use get-address or index-address is to modify, so stop right there</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>current_ingredient_name == original_ingredient_name<span class="Delimiter">)</span>
raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"cannot modify ingredient "</span> << current_ingredient_name << <span class="Constant">" after instruction '"</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> << <span class="Constant">"' because it's not also a product of "</span> << caller<span class="Delimiter">.</span>name << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Normal">else</span>
raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"cannot modify "</span> << current_ingredient_name << <span class="Constant">" after instruction '"</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> << <span class="Constant">"' because that would modify ingredient "</span> << original_ingredient_name << <span class="Constant">" which is not also a product of "</span> << caller<span class="Delimiter">.</span>name << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
<span class="Comment">// defined recipe</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!is_mu_address<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">))</span> <span class="Identifier">return</span><span class="Delimiter">;</span> <span class="Comment">// making a copy is ok</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_modified_in_recipe<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>operation<span class="Delimiter">,</span> current_ingredient_index<span class="Delimiter">,</span> caller<span class="Delimiter">))</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>current_ingredient_name == original_ingredient_name<span class="Delimiter">)</span>
raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"cannot modify ingredient "</span> << current_ingredient_name << <span class="Constant">" at instruction '"</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> << <span class="Constant">"' because it's not also a product of "</span> << caller<span class="Delimiter">.</span>name << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Normal">else</span>
raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"cannot modify "</span> << current_ingredient_name << <span class="Constant">" after instruction '"</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> << <span class="Constant">"' because that would modify ingredient "</span> << original_ingredient_name << <span class="Constant">" which is not also a product of "</span> << caller<span class="Delimiter">.</span>name << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> is_modified_in_recipe<span class="Delimiter">(</span>recipe_ordinal r<span class="Delimiter">,</span> <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> ingredient_index<span class="Delimiter">,</span> <span class="Normal">const</span> recipe& caller<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> recipe& callee = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!callee<span class="Delimiter">.</span>has_header<span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"can't check mutability of ingredients in "</span> << callee<span class="Delimiter">.</span>name << <span class="Constant">" because it uses 'next-ingredient' directly, rather than a recipe header.</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>ingredient_index >= SIZE<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Comment">// optional immutable ingredient</span>
<span class="Identifier">return</span> is_present_in_products<span class="Delimiter">(</span>callee<span class="Delimiter">,</span> callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>ingredient_index<span class="Delimiter">).</span>name<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> is_present_in_products<span class="Delimiter">(</span><span class="Normal">const</span> recipe& callee<span class="Delimiter">,</span> <span class="Normal">const</span> string& ingredient_name<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>callee<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name == ingredient_name<span class="Delimiter">)</span>
<span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">bool</span> is_present_in_ingredients<span class="Delimiter">(</span><span class="Normal">const</span> recipe& callee<span class="Delimiter">,</span> <span class="Normal">const</span> string& ingredient_name<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name == ingredient_name<span class="Delimiter">)</span>
<span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
set<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>> ingredient_indices<span class="Delimiter">(</span><span class="Normal">const</span> instruction& inst<span class="Delimiter">,</span> <span class="Normal">const</span> set<reagent>& ingredient_names<span class="Delimiter">)</span> <span class="Delimiter">{</span>
set<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>> result<span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_literal<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>ingredient_names<span class="Delimiter">.</span>find<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))</span> != ingredient_names<span class="Delimiter">.</span>end<span class="Delimiter">())</span>
result<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">//: Sometimes you want to pass in two addresses, one pointing inside the</span>
<span class="Comment">//: other. For example, you want to delete a node from a linked list. You</span>
<span class="Comment">//: can't pass both pointers back out, because if a caller tries to make both</span>
<span class="Comment">//: identical then you can't tell which value will be written on the way out.</span>
<span class="Comment">//:</span>
<span class="Comment">//: Experimental solution: just tell mu that one points inside the other.</span>
<span class="Comment">//: This way we can return just one pointer as high up as necessary to capture</span>
<span class="Comment">//: all modifications performed by a recipe.</span>
<span class="Comment">//:</span>
<span class="Comment">//: We'll see if we end up wanting to abuse /contained-in for other reasons.</span>
<span class="Delimiter">:(scenarios transform)</span>
<span class="Delimiter">:(scenario can_modify_contained_in_addresses)</span>
container test-list [
next:address:shared:test-list
]
def main [
local-scope
p:address:shared:test-list<span class="Special"> <- </span><span class="Normal">new</span> test-list:type
foo p
]
def foo p:address:shared:test-list <span class="Delimiter">-></span> p:address:shared:test-list [
local-scope
load-ingredients
p2:address:shared:test-list<span class="Special"> <- </span>test-next p
p<span class="Special"> <- </span>test-remove p2<span class="Delimiter">,</span> p
]
def test-next x:address:shared:test-list <span class="Delimiter">-></span> y:address:shared:test-list [
local-scope
load-ingredients
y<span class="Special"> <- </span>get *x<span class="Delimiter">,</span> next:offset
]
def test-remove x:address:shared:test-list/contained-in:from<span class="Delimiter">,</span> from:address:shared:test-list <span class="Delimiter">-></span> from:address:shared:test-list [
local-scope
load-ingredients
x2:address:address:shared:test-list<span class="Special"> <- </span>get-address *x<span class="Delimiter">,</span> next:offset <span class="Comment"># pretend modification</span>
]
$error: <span class="Constant">0</span>
<span class="Delimiter">:(before "End Immutable Ingredients Special-cases")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>has_property<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">,</span> <span class="Constant">"contained-in"</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> string_tree* tmp = property<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">,</span> <span class="Constant">"contained-in"</span><span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>tmp<span class="Delimiter">-></span>left || tmp<span class="Delimiter">-></span>right
|| !is_present_in_ingredients<span class="Delimiter">(</span>caller<span class="Delimiter">,</span> tmp<span class="Delimiter">-></span>value<span class="Delimiter">)</span>
|| !is_present_in_products<span class="Delimiter">(</span>caller<span class="Delimiter">,</span> tmp<span class="Delimiter">-></span>value<span class="Delimiter">))</span>
raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"contained-in can only point to another ingredient+product, but got "</span> << to_string<span class="Delimiter">(</span>property<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">,</span> <span class="Constant">"contained-in"</span><span class="Delimiter">))</span> << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>