29.01.2014 Views

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

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 8. DEPENDANT TYPE QUALIFIERS 68<br />

let tut8 (f : file) : unit =<br />

tut8 init f;<br />

let c = context for globals f in<br />

c | > checkColorTypes | > onlyFunctions | > iterGlobals f;<br />

eraseColors f<br />

8.2 test/tut8.c<br />

In this test we dene a structure type in which the type <strong>of</strong> one <strong>of</strong> the elds depends on the values <strong>of</strong><br />

the other elds, and two functions in which the type <strong>of</strong> one <strong>of</strong> the formal parameters depends on the<br />

values to be bound to the other formal parameters at each call site. The test demonstrates the type<br />

safe initialization <strong>of</strong> the elds <strong>of</strong> the structure (i.e. elds whose type depends on the other elds are<br />

initialized last.) Also, we check that the compiler catches a call with poorly-typed parameters (i.e.<br />

the call to bar).<br />

# include <br />

struct bar {<br />

int r, g, b;<br />

int ExactRGB(r,g,b) c;<br />

};<br />

../test/tut8.c<br />

void foo(int LowerRGB(r,g,b) c, int r, int g, int b) {return;}<br />

void bar(int UpperRGB(r,g,b) c, int r, int g, int b) {return;}<br />

int main()<br />

{<br />

struct bar B;<br />

B.r = 50;<br />

B.g = 50;<br />

B.b = 50;<br />

B.c = AddRGB(50,50,50,50);<br />

foo(B.c, B.r, B.g, B.b);<br />

bar(B.c, 10, 10, 10);<br />

}<br />

return 0;<br />

We compile this example:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!