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
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
CHAPTER 7. TYPE QUALIFIERS 55<br />
let warning for tcres (tcr : typecheck result) : unit =<br />
match tcr with<br />
| TypesMismatch(t1, t2) →<br />
E.warn "%a: type mismatch: %a %a" d loc (!currentLoc) d type t1 d type t2<br />
| ColorsMismatch(t1, t2) →<br />
E.warn "%a: color mismatch: %a %a" d loc (!currentLoc) d type t1 d type t2<br />
| TypesOkay → ()<br />
Now, we'll visit the AST looking for places where we must check type compatibility. We must check<br />
compatibility at assignments, casts, and parameter passing. In the case for casts, we allow constants<br />
to be cast to a qualied type. Without this exception, it would be impossible to initialize variables<br />
with qualied types without an error or warning. The case for function calls is left as an exercise.<br />
class colorCheckVisitor = object(self)<br />
inherit nopCilVisitor<br />
method vinst (i : instr) =<br />
match i with<br />
| Set(lv, e, loc) →<br />
let tcres = colorTypesCompat (typeOfLval lv) (typeOf e) in<br />
warning for tcres tcres;<br />
DoChildren<br />
| Call(rlvo, fe, args, loc) → DoChildren (∗ See exercise ∗)<br />
| → DoChildren<br />
method vexpr (e : exp) =<br />
match e with<br />
| CastE(t, e) when ¬(isConstant e) →<br />
let tcres = colorTypesCompat t (typeOf e) in<br />
warning for tcres tcres;<br />
DoChildren<br />
| → DoChildren<br />
end<br />
The function checkColorTypes invokes the visitor colorCheckVisitor on a function.<br />
let checkColorTypes (fd : fundec) (loc : location) : unit =<br />
let vis = new colorCheckVisitor in<br />
ignore(visitCilFunction vis fd)<br />
Since gcc doesn't understand the color type attributes, we must use the colorEraserVisitor to<br />
remove them from the program before passing the code on to it. We do this by overriding the vattr<br />
method <strong>of</strong> the nopCilVisitor and ltering out the color attributes.