You could write a function like this (note I'm just typing this in directly as email, so this should be close syntactically, but I may have forgotten something):
let print_var_type (v:varinfo) : string =
sprint 200 (d_type () v.vtype)
The sprint function is part of the Pretty module, so you should either "open Pretty" in your code or say something like "module P = Pretty" and then refer to the function as "P.sprint". The number following sprint is the number of characters on a line, but since you don't want line breaks (I'm assuming) it just has to be larger than the largest type you would want to print. d_type is defined in Cil, and just uses the default Cil printer to print out the type, which would save you from having to write code to actually print out the type information.
If you want the printed types to be actual types, versus typedef names, you should also look at the functions unrollType and unrollTypeDeep; the former will just unroll a typedef into the type it represents, while the latter will do this recursively through the structure of the type (through pointers, structures, etc).
I hope this helps.
Mark
-----Original Message-----
From: cil-users-bounces@... [mailto:cil-users-bounces@...] On Behalf Of Rekha Bachwani
Sent: Monday, September 08, 2008 12:25 PM
To: cil-users@...
Subject: [CIL users] Cil.typ to string
Hi,
CIL infers types of variable automatically. However,
I want to get the typeinfo and pass it as a string to
another function. Does someone know how to do
this?
thanks,
Rekha
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
CIL-users mailing list
CIL-users@...
https://lists.sourceforge.net/lists/listinfo/cil-users

Hello Rekha -
On Mon, Sep 08, 2008 at 01:25:11PM -0400, Rekha Bachwani wrote:
> CIL infers types of variable automatically.
If you look at cilly as a black-box then yes. ;-)
> However, I want to get the typeinfo and pass it as a string to
> another function. Does someone know how to do this?
The answer depends on what you want to do with the resulting string
later on: print it, parse it, store it on disk, etc? Here are some
possibilities.
The DIY solution does not require any external library.
let typename_as_string = function
Cil.TVoid _ -> "unit"
| Cil.TInt _ -> "integral"
| Cil.TFloat _ -> "floating-point"
| Cil.TPtr _ -> "pointer"
| Cil.TArray _ -> "array"
| Cil.TFun _ -> "function"
| Cil.TNamed _ -> "named-something"
| Cil.TComp _ -> "composite"
| Cil.TEnum _ -> "enumeration"
| Cil.TBuiltin_va_list _ -> "builtin-variable-argument-list"
I left out the arguments for clarity. Note that C-types can be
recursively defined. Thus, a complete string representation must
include a reference notation. In the end you will probably wind up
with a set of mutually recursive functions converting "typeinfo",
"compinfo", "enuminfo", "typ", "ikind", "fkind", and so on into
strings; not to forget "attributes" and the types connected to them.
If you just want to serialize you data as a string the Marshal module
of the standard library does the job.
http://caml.inria.fr/pub/docs/manual-ocaml/libref/Marshal.html
Martin Jambon's "Json Static" library serializes into JSON-like
strings.
http://martin.jambon.free.fr/json-static.html
The OcamlNet library supports XDR:
http://projects.camlcity.org/projects/ocamlnet.html
and the Sexplib of Jane Street Capital translates data to and from
S-expressions
http://www.janestcapital.com/ocaml/index.html
Conceivable, even more serializing libraries for OCaml are out there
in the world wild web.
HTH,
/Chris
--
Dr. Christoph L. Spiel