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

29.01.2014 Views

Chapter 7 Type Qualiers Over the next three chapters, we'll explore how to make changes to C's type-system. This will be achieved by adding type-qualiers to C's types, and by performing some extra type-checking. In this section, we'll write a very basic type-checker for types that may be qualied by one or more of the following colors: red, green, or blue. In the exercises, you'll nd suggestions about how to complete it. In the next section, we'll look at interpreting dependent type qualiers. Finally, in Chapter 9, we'll see how to do some basic type qualier inference. 7.1 tut7.ml In tut7.ml, rst we'll write functions to extract qualiers from types. Then, we'll perform our additional type-checking. 7.1.1 Qualier Types We'll dene some OCaml types representing the C type qualiers. Then, from C types, we'll extract a possibly empty list of the qualiers. type color = Red | Blue | Green We'll set up some global constants for the string representation of the qualiers, and use them everywhere instead of the strings, in case we want to change them later on. let redStr = "red" let blueStr = "blue" let greenStr = "green" Putting the strings in a list will help a bit later on. 52

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

Chapter 7<br />

Type Qualiers<br />

Over the next three chapters, we'll explore how to make changes to C's type-system. This will be<br />

achieved by adding type-qualiers to C's types, and by performing some extra type-checking. In<br />

this section, we'll write a very basic type-checker for types that may be qualied by one or more<br />

<strong>of</strong> the following colors: red, green, or blue. In the exercises, you'll nd suggestions about how to<br />

complete it. In the next section, we'll look at interpreting dependent type qualiers. Finally, in<br />

Chapter 9, we'll see how to do some basic type qualier inference.<br />

7.1 tut7.ml<br />

In tut7.ml, rst we'll write functions to extract qualiers from types. Then, we'll perform our<br />

additional type-checking.<br />

7.1.1 Qualier Types<br />

We'll dene some OCaml types representing the C type qualiers. Then, from C types, we'll extract<br />

a possibly empty list <strong>of</strong> the qualiers.<br />

type color = Red | Blue | Green<br />

We'll set up some global constants for the string representation <strong>of</strong> the qualiers, and use them<br />

everywhere instead <strong>of</strong> the strings, in case we want to change them later on.<br />

let redStr = "red"<br />

let blueStr = "blue"<br />

let greenStr = "green"<br />

Putting the strings in a list will help a bit later on.<br />

52

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

Saved successfully!

Ooh no, something went wrong!