]> matita.cs.unibo.it Git - pkg-cerco/acc-trusted.git/blob - cparser/Cabs.ml
Imported Upstream version 0.1
[pkg-cerco/acc-trusted.git] / cparser / Cabs.ml
1 (* 
2  *
3  * Copyright (c) 2001-2002, 
4  *  George C. Necula    <necula@cs.berkeley.edu>
5  *  Scott McPeak        <smcpeak@cs.berkeley.edu>
6  *  Wes Weimer          <weimer@cs.berkeley.edu>
7  * All rights reserved.
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * 3. The names of the contributors may not be used to endorse or promote
21  * products derived from this software without specific prior written
22  * permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  *)
37
38 (** This file was originally part of Hugues Casee's frontc 2.0, and has been 
39  * extensively changed since. 
40 **
41 ** 1.0  3.22.99 Hugues Cassé    First version.
42 ** 2.0  George Necula 12/12/00: Many extensions
43  **)
44
45 (*
46 ** Types
47 *)
48
49 type cabsloc = {
50  lineno : int;
51  filename: string;
52  byteno: int;
53  ident : int;
54 }
55
56 type typeSpecifier = (* Merge all specifiers into one type *)
57     Tvoid                             (* Type specifier ISO 6.7.2 *)
58   | Tchar
59   | Tshort
60   | Tint
61   | Tlong
62   | Tint64
63   | T_Bool
64   | Tfloat
65   | Tdouble
66   | Tsigned
67   | Tunsigned
68   | Tnamed of string
69   (* each of the following three kinds of specifiers contains a field 
70    * or item list iff it corresponds to a definition (as opposed to
71    * a forward declaration or simple reference to the type); they
72    * also have a list of __attribute__s that appeared between the
73    * keyword and the type name (definitions only) *)
74   | Tstruct of string * field_group list option * attribute list
75   | Tunion of string * field_group list option * attribute list
76   | Tenum of string * enum_item list option * attribute list
77   | TtypeofE of expression                      (* GCC __typeof__ *)
78   | TtypeofT of specifier * decl_type       (* GCC __typeof__ *)
79
80 and storage =
81     NO_STORAGE | AUTO | STATIC | EXTERN | REGISTER
82
83 and funspec = 
84     INLINE | VIRTUAL | EXPLICIT
85
86 and cvspec =
87     CV_CONST | CV_VOLATILE | CV_RESTRICT
88
89 (* Type specifier elements. These appear at the start of a declaration *)
90 (* Everywhere they appear in this file, they appear as a 'spec_elem list', *)
91 (* which is not interpreted by cabs -- rather, this "word soup" is passed *)
92 (* on to the compiler.  Thus, we can represent e.g. 'int long float x' even *)
93 (* though the compiler will of course choke. *)
94 and spec_elem =
95     SpecTypedef          
96   | SpecCV of cvspec            (* const/volatile *)
97   | SpecAttr of attribute       (* __attribute__ *)
98   | SpecStorage of storage
99   | SpecInline
100   | SpecType of typeSpecifier
101
102 (* decided to go ahead and replace 'spec_elem list' with specifier *)
103 and specifier = spec_elem list
104
105
106 (* Declarator type. They modify the base type given in the specifier. Keep
107  * them in the order as they are printed (this means that the top level
108  * constructor for ARRAY and PTR is the inner-level in the meaning of the
109  * declared type) *)
110 and decl_type =
111  | JUSTBASE                               (* Prints the declared name *)
112  | PARENTYPE of attribute list * decl_type * attribute list
113                                           (* Prints "(attrs1 decl attrs2)".
114                                            * attrs2 are attributes of the
115                                            * declared identifier and it is as
116                                            * if they appeared at the very end
117                                            * of the declarator. attrs1 can
118                                            * contain attributes for the
119                                            * identifier or attributes for the
120                                            * enclosing type.  *)
121  | ARRAY of decl_type * attribute list * expression
122                                           (* Prints "decl [ attrs exp ]".
123                                            * decl is never a PTR. *)
124  | PTR of attribute list * decl_type      (* Prints "* attrs decl" *)
125  | PROTO of decl_type * single_name list * bool 
126                                           (* Prints "decl (args[, ...])".
127                                            * decl is never a PTR.*)
128
129 (* The base type and the storage are common to all names. Each name might
130  * contain type or storage modifiers *)
131 (* e.g.: int x, y; *)
132 and name_group = specifier * name list
133
134 (* The optional expression is the bitfield *)
135 and field_group = specifier * (name * expression option) list
136
137 (* like name_group, except the declared variables are allowed to have initializers *)
138 (* e.g.: int x=1, y=2; *)
139 and init_name_group = specifier * init_name list
140
141 (* The decl_type is in the order in which they are printed. Only the name of
142  * the declared identifier is pulled out. The attributes are those that are
143  * printed after the declarator *)
144 (* e.g: in "int *x", "*x" is the declarator; "x" will be pulled out as *)
145 (* the string, and decl_type will be PTR([], JUSTBASE) *)
146 and name = string * decl_type * attribute list * cabsloc
147
148 (* A variable declarator ("name") with an initializer *)
149 and init_name = name * init_expression
150
151 (* Single names are for declarations that cannot come in groups, like
152  * function parameters and functions *)
153 and single_name = specifier * name
154
155
156 and enum_item = string * expression * cabsloc
157
158 (*
159 ** Declaration definition (at toplevel)
160 *)
161 and definition =
162    FUNDEF of single_name * block * cabsloc * cabsloc
163  | DECDEF of init_name_group * cabsloc        (* global variable(s), or function prototype *)
164  | TYPEDEF of name_group * cabsloc
165  | ONLYTYPEDEF of specifier * cabsloc
166  | GLOBASM of string * cabsloc
167  | PRAGMA of string * cabsloc
168  | LINKAGE of string * cabsloc * definition list (* extern "C" { ... } *)
169
170 (* the string is a file name, and then the list of toplevel forms *)
171 and file = string * definition list
172
173
174 (*
175 ** statements
176 *)
177
178 (* A block contains a list of local label declarations ( GCC's ({ __label__ 
179  * l1, l2; ... }) ) , a list of definitions and a list of statements  *)
180 and block = 
181     { blabels: string list;
182       battrs: attribute list;
183       bstmts: statement list
184     } 
185
186 (* GCC asm directives have lots of extra information to guide the optimizer *)
187 and asm_details =
188     { aoutputs: (string option * string * expression) list; (* optional name, constraints and expressions for outputs *)
189       ainputs: (string option * string * expression) list; (* optional name, constraints and expressions for inputs *)
190       aclobbers: string list (* clobbered registers *)
191     }
192
193 and statement =
194    NOP of cabsloc
195  | COMPUTATION of expression * cabsloc
196  | BLOCK of block * cabsloc
197 (* | SEQUENCE of statement * statement * cabsloc *)
198  | IF of expression * statement * statement * cabsloc
199  | WHILE of expression * statement * cabsloc
200  | DOWHILE of expression * statement * cabsloc
201  | FOR of for_clause * expression * expression * statement * cabsloc
202  | BREAK of cabsloc
203  | CONTINUE of cabsloc
204  | RETURN of expression * cabsloc
205  | SWITCH of expression * statement * cabsloc
206  | CASE of expression * statement * cabsloc
207  | CASERANGE of expression * expression * statement * cabsloc
208  | DEFAULT of statement * cabsloc
209  | LABEL of string * statement * cabsloc
210  | GOTO of string * cabsloc
211  | COMPGOTO of expression * cabsloc (* GCC's "goto *exp" *)
212  | DEFINITION of definition (*definition or declaration of a variable or type*)
213
214  | ASM of attribute list * (* typically only volatile and const *)
215           string list * (* template *)
216           asm_details option * (* extra details to guide GCC's optimizer *)
217           cabsloc
218
219    (** MS SEH *)
220  | TRY_EXCEPT of block * expression * block * cabsloc
221  | TRY_FINALLY of block * block * cabsloc
222  
223 and for_clause = 
224    FC_EXP of expression
225  | FC_DECL of definition
226
227 (*
228 ** Expressions
229 *)
230 and binary_operator =
231     ADD | SUB | MUL | DIV | MOD
232   | AND | OR
233   | BAND | BOR | XOR | SHL | SHR
234   | EQ | NE | LT | GT | LE | GE
235   | ASSIGN
236   | ADD_ASSIGN | SUB_ASSIGN | MUL_ASSIGN | DIV_ASSIGN | MOD_ASSIGN
237   | BAND_ASSIGN | BOR_ASSIGN | XOR_ASSIGN | SHL_ASSIGN | SHR_ASSIGN
238
239 and unary_operator =
240     MINUS | PLUS | NOT | BNOT | MEMOF | ADDROF
241   | PREINCR | PREDECR | POSINCR | POSDECR
242
243 and expression =
244     NOTHING
245   | UNARY of unary_operator * expression
246   | LABELADDR of string  (* GCC's && Label *)
247   | BINARY of binary_operator * expression * expression
248   | QUESTION of expression * expression * expression
249
250    (* A CAST can actually be a constructor expression *)
251   | CAST of (specifier * decl_type) * init_expression
252
253     (* There is a special form of CALL in which the function called is
254        __builtin_va_arg and the second argument is sizeof(T). This 
255        should be printed as just T *)
256   | CALL of expression * expression list
257   | COMMA of expression list
258   | CONSTANT of constant
259   | PAREN of expression
260   | VARIABLE of string
261   | EXPR_SIZEOF of expression
262   | TYPE_SIZEOF of specifier * decl_type
263   | EXPR_ALIGNOF of expression
264   | TYPE_ALIGNOF of specifier * decl_type
265   | INDEX of expression * expression
266   | MEMBEROF of expression * string
267   | MEMBEROFPTR of expression * string
268   | GNU_BODY of block
269
270 and constant =
271   | CONST_INT of string   (* the textual representation *)
272   | CONST_FLOAT of string (* the textual representaton *)
273   | CONST_CHAR of int64 list
274   | CONST_WCHAR of int64 list
275   | CONST_STRING of string
276   | CONST_WSTRING of int64 list 
277     (* ww: wstrings are stored as an int64 list at this point because
278      * we might need to feed the wide characters piece-wise into an 
279      * array initializer (e.g., wchar_t foo[] = L"E\xabcd";). If that
280      * doesn't happen we will convert it to an (escaped) string before
281      * passing it to Cil. *) 
282
283 and init_expression =
284   | NO_INIT
285   | SINGLE_INIT of expression
286   | COMPOUND_INIT of (initwhat * init_expression) list
287
288 and initwhat =
289     NEXT_INIT
290   | INFIELD_INIT of string * initwhat
291   | ATINDEX_INIT of expression * initwhat
292   | ATINDEXRANGE_INIT of expression * expression
293  
294
295                                         (* Each attribute has a name and some
296                                          * optional arguments *)
297 and attribute = string * expression list
298                                               
299