A CIL Tutorial - Department of Computer Science - ETH Zürich
A CIL Tutorial - Department of Computer Science - ETH Zürich
A CIL Tutorial - Department of Computer Science - ETH Zürich
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
CHAPTER 7. TYPE QUALIFIERS 54<br />
let colors <strong>of</strong> type (t : typ) : color list =<br />
color strings<br />
|> L.filter (isTypeColor t)<br />
|> L.map color <strong>of</strong> string<br />
7.1.2 Type-checking<br />
Now that we can extract the qualiers we care about from a type, we can check that two types are<br />
compatible. First we dene a type codifying the results <strong>of</strong> comparing two types, typecheck result.<br />
Then, we'll write a function that performs the comparison, checkColorTypes. TypesMismatch and<br />
ColorsMismatch are parameterized by the types that didn't match. This is to enable writing better<br />
error and warning messages.<br />
type typecheck result =<br />
| TypesOkay<br />
| TypesMismatch <strong>of</strong> typ × typ<br />
| ColorsMismatch <strong>of</strong> typ × typ<br />
The function colorTypesCompat rst ensures that two types have the same colors before descending<br />
recursively into the structure <strong>of</strong> a type. That is, we check that pointer types have the same colors<br />
before checking that the pointed-to types also have the same colors. We'll leave as exercises the<br />
correct handling <strong>of</strong> function types, and also make some suggestions about how to modify this code<br />
to make it more general-purpose.<br />
let rec colorTypesCompat (t1 : typ) (t2 : typ) : typecheck result =<br />
let cl1 = colors <strong>of</strong> type t1 in<br />
let cl2 = colors <strong>of</strong> type t2 in<br />
if cl1 ≠ cl2 then ColorsMismatch(t1, t2) else begin<br />
match t1, t2 with<br />
| TVoid , TVoid → TypesOkay<br />
| TPtr(t1, ), TPtr(t2, )<br />
| TArray(t1, , ), TArray(t2, , ) → colorTypesCompat t1 t2<br />
| TFun , TFun → TypesOkay (∗ See the exercise below ∗)<br />
(∗ ... ∗)<br />
| , → TypesMismatch(t1, t2)<br />
end<br />
The function warning for tcres generates warning messages for the various kinds <strong>of</strong> typecheck results.<br />
In certain situations, some mismatches may be okay. To handle that, one would extend this function<br />
with a second parameter that indicates which mismatches are okay in a particular situation.