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 53<br />
let color strings = [redStr; blueStr; greenStr; ]<br />
As mentioned in a previous chapter, it is useful to have functions that convert a type to and from<br />
a string. The function string <strong>of</strong> color returns redStr, blueStr, or greenStr as appropriate.<br />
let string <strong>of</strong> color (c : color) : string =<br />
match c with<br />
| Red → redStr<br />
| Blue → blueStr<br />
| Green → greenStr<br />
The function color <strong>of</strong> string returns the color corresponding to the string cs it gets as input.<br />
let color <strong>of</strong> string (cs : string) : color =<br />
match S.lowercase cs with<br />
| s when s = redStr → Red<br />
| s when s = blueStr → Blue<br />
| s when s = greenStr → Green<br />
| → E.s(E.bug "Expected a color string, got: %s" cs)<br />
The function isColorType returns true when a type is qualied by a particular color attribute. The<br />
function isTypeColor the same with the order <strong>of</strong> the arguments reversed. The next three functions<br />
tell if a type is qualied by a particular color. These functions to largely similar things, but they'll<br />
be useful in dierent situations.<br />
isColorType is written with the function hasAttribute, which comes from the core Cil module.<br />
It returns true when a list <strong>of</strong> attributes contains an attribute with the given name. typeAttrs<br />
extracts the attributes from a type. Attributes are one <strong>of</strong> the extensions to C accepted by gcc. <strong>CIL</strong><br />
parses these attributes, even custom attributes not understood by gcc, and includes them in its<br />
AST attached to types, elds, functions, formal parameters, and blocks <strong>of</strong> code. All we care about<br />
for now, though, is whether or not a type is qualied by one <strong>of</strong> the colors.<br />
let isColorType (cs : string) (t : typ) : bool =<br />
hasAttribute cs (typeAttrs t)<br />
let isTypeColor (t : typ) (cs : string) : bool = isColorType cs t<br />
let isRedType : typ → bool = isColorType redStr<br />
let isBlueType : typ → bool = isColorType blueStr<br />
let isGreenType : typ → bool = isColorType greenStr<br />
The function colors <strong>of</strong> type takes a type and returns a list <strong>of</strong> colors that qualify the type.