b864abe56802e1721f4226ff9ab92c37ebaa71ff
[bpt/coccinelle.git] / parsing_cocci / index.ml
1 (*
2 * Copyright 2010, INRIA, University of Copenhagen
3 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
4 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
5 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
6 * This file is part of Coccinelle.
7 *
8 * Coccinelle is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, according to version 2 of the License.
11 *
12 * Coccinelle is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
22 *)
23
24
25 (* create an index for each constructor *)
26 (* current max is 150 *)
27
28 (* doesn't really work - requires that identical terms with no token
29 subterms (eg dots) not appear on the same line *)
30
31 module Ast = Ast_cocci
32 module Ast0 = Ast0_cocci
33
34 (* if a dot list is empty, add the starting line of the dot list to the
35 address. Otherwise add 0. An empty dot list should only match with another
36 empty one. *)
37 let expression_dots d =
38 let ln = (Ast0.get_info d).Ast0.pos_info.Ast0.line_start in
39 match Ast0.unwrap d with
40 Ast0.DOTS(l) -> 1::(if l = [] then [ln] else [0])
41 | Ast0.CIRCLES(l) -> 2::(if l = [] then [ln] else [0])
42 | Ast0.STARS(l) -> 3::(if l = [] then [ln] else [0])
43
44 let initialiser_dots d =
45 let ln = (Ast0.get_info d).Ast0.pos_info.Ast0.line_start in
46 match Ast0.unwrap d with
47 Ast0.DOTS(l) -> 113::(if l = [] then [ln] else [0])
48 | Ast0.CIRCLES(l) -> 114::(if l = [] then [ln] else [0])
49 | Ast0.STARS(l) -> 115::(if l = [] then [ln] else [0])
50
51 let parameter_dots d =
52 let ln = (Ast0.get_info d).Ast0.pos_info.Ast0.line_start in
53 match Ast0.unwrap d with
54 Ast0.DOTS(l) -> 4::(if l = [] then [ln] else [0])
55 | Ast0.CIRCLES(l) -> 5::(if l = [] then [ln] else [0])
56 | Ast0.STARS(l) -> 6::(if l = [] then [ln] else [0])
57
58 let statement_dots d =
59 let ln = (Ast0.get_info d).Ast0.pos_info.Ast0.line_start in
60 match Ast0.unwrap d with
61 Ast0.DOTS(l) -> 7::(if l = [] then [ln] else [0])
62 | Ast0.CIRCLES(l) -> 8::(if l = [] then [ln] else [0])
63 | Ast0.STARS(l) -> 9::(if l = [] then [ln] else [0])
64
65 let declaration_dots d =
66 let ln = (Ast0.get_info d).Ast0.pos_info.Ast0.line_start in
67 match Ast0.unwrap d with
68 Ast0.DOTS(l) -> 134::(if l = [] then [ln] else [0])
69 | Ast0.CIRCLES(l) -> 135::(if l = [] then [ln] else [0])
70 | Ast0.STARS(l) -> 136::(if l = [] then [ln] else [0])
71
72 let case_line_dots d =
73 let ln = (Ast0.get_info d).Ast0.pos_info.Ast0.line_start in
74 match Ast0.unwrap d with
75 Ast0.DOTS(l) -> 138::(if l = [] then [ln] else [0])
76 | Ast0.CIRCLES(l) -> 139::(if l = [] then [ln] else [0])
77 | Ast0.STARS(l) -> 140::(if l = [] then [ln] else [0])
78
79 let ident i =
80 match Ast0.unwrap i with
81 Ast0.Id(name) -> [10]
82 | Ast0.MetaId(name,_,_) -> [11]
83 | Ast0.MetaFunc(name,_,_) -> [12]
84 | Ast0.MetaLocalFunc(name,_,_) -> [13]
85 | Ast0.OptIdent(id) -> [14]
86 | Ast0.UniqueIdent(id) -> [15]
87
88 let expression e =
89 match Ast0.unwrap e with
90 Ast0.Ident(id) -> [17]
91 | Ast0.Constant(const) -> [18]
92 | Ast0.FunCall(fn,lp,args,rp) -> [19]
93 | Ast0.Assignment(left,op,right,simple) -> [20]
94 | Ast0.CondExpr(exp1,why,exp2,colon,exp3) -> [21]
95 | Ast0.Postfix(exp,op) -> [22]
96 | Ast0.Infix(exp,op) -> [23]
97 | Ast0.Unary(exp,op) -> [24]
98 | Ast0.Binary(left,op,right) -> [25]
99 | Ast0.Nested(left,op,right) -> failwith "nested in index not possible"
100 | Ast0.Paren(lp,exp,rp) -> [26]
101 | Ast0.ArrayAccess(exp1,lb,exp2,rb) -> [27]
102 | Ast0.RecordAccess(exp,pt,field) -> [28]
103 | Ast0.RecordPtAccess(exp,ar,field) -> [29]
104 | Ast0.Cast(lp,ty,rp,exp) -> [30]
105 | Ast0.SizeOfExpr(szf,exp) -> [98] (* added after *)
106 | Ast0.SizeOfType(szf,lp,ty,rp) -> [99] (* added after *)
107 | Ast0.TypeExp(ty) -> [123] (* added after *)
108 | Ast0.MetaErr(name,_,_) -> [32]
109 | Ast0.MetaExpr(name,_,ty,_,_) -> [33]
110 | Ast0.MetaExprList(name,_,_) -> [34]
111 | Ast0.EComma(cm) -> [35]
112 | Ast0.DisjExpr(_,expr_list,_,_) -> [36]
113 | Ast0.NestExpr(_,expr_dots,_,_,_) -> [37]
114 | Ast0.Edots(dots,whencode) -> [38]
115 | Ast0.Ecircles(dots,whencode) -> [39]
116 | Ast0.Estars(dots,whencode) -> [40]
117 | Ast0.OptExp(exp) -> [41]
118 | Ast0.UniqueExp(exp) -> [42]
119
120 let typeC t =
121 match Ast0.unwrap t with
122 Ast0.ConstVol(cv,ty) -> [44]
123 | Ast0.BaseType(ty,strings) -> [48]
124 | Ast0.Signed(sign,ty) -> [129]
125 | Ast0.Pointer(ty,star) -> [49]
126 | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> [131]
127 | Ast0.FunctionType(ty,lp1,params,rp1) -> [132]
128 | Ast0.Array(ty,lb,size,rb) -> [50]
129 | Ast0.EnumName(kind,name) -> [146]
130 | Ast0.EnumDef(ty,lb,decls,rb) -> [150]
131 | Ast0.StructUnionName(kind,name) -> [51]
132 | Ast0.StructUnionDef(ty,lb,decls,rb) -> [117]
133 | Ast0.TypeName(name) -> [52]
134 | Ast0.MetaType(name,_) -> [53]
135 | Ast0.DisjType(_,type_list,_,_) -> [130]
136 | Ast0.OptType(ty) -> [45]
137 | Ast0.UniqueType(ty) -> [46]
138
139 let declaration d =
140 match Ast0.unwrap d with
141 Ast0.MetaDecl(name,_) -> [148]
142 | Ast0.MetaField(name,_) -> [149]
143 | Ast0.Init(stg,ty,id,eq,exp,sem) -> [54]
144 | Ast0.UnInit(stg,ty,id,sem) -> [55]
145 | Ast0.MacroDecl(name,lp,args,rp,sem) -> [137]
146 | Ast0.TyDecl(ty,sem) -> [116]
147 | Ast0.Typedef(stg,ty,id,sem) -> [143]
148 | Ast0.DisjDecl(_,decls,_,_) -> [97] (* added after *)
149 | Ast0.Ddots(dots,whencode) -> [133]
150 | Ast0.OptDecl(decl) -> [56]
151 | Ast0.UniqueDecl(decl) -> [57]
152
153 let initialiser i =
154 match Ast0.unwrap i with
155 Ast0.MetaInit(nm,_) -> [106] (* added after *)
156 | Ast0.InitExpr(exp) -> [102]
157 | Ast0.InitList(lb,initlist,rb,ordered) -> [103]
158 | Ast0.InitGccExt(designators,eq,ini) -> [104]
159 | Ast0.InitGccName(name,eq,ini) -> [105]
160 | Ast0.IComma(cm) -> [108]
161 | Ast0.Idots(d,whencode) -> [109]
162 | Ast0.OptIni(id) -> [110]
163 | Ast0.UniqueIni(id) -> [111]
164
165 let parameterTypeDef p =
166 match Ast0.unwrap p with
167 Ast0.VoidParam(ty) -> [59]
168 | Ast0.Param(ty,id) -> [60]
169 | Ast0.MetaParam(name,_) -> [61]
170 | Ast0.MetaParamList(name,_,_) -> [62]
171 | Ast0.PComma(cm) -> [63]
172 | Ast0.Pdots(dots) -> [64]
173 | Ast0.Pcircles(dots) -> [65]
174 | Ast0.OptParam(param) -> [66]
175 | Ast0.UniqueParam(param) -> [67]
176
177 let statement s =
178 match Ast0.unwrap s with
179 Ast0.FunDecl(bef,fninfo,name,lp,params,rp,lbrace,body,rbrace) -> [68]
180 | Ast0.Decl(bef,decl) -> [69]
181 | Ast0.Seq(lbrace,body,rbrace) -> [70]
182 | Ast0.ExprStatement(exp,sem) -> [71]
183 | Ast0.IfThen(iff,lp,exp,rp,branch1,aft) -> [72]
184 | Ast0.IfThenElse(iff,lp,exp,rp,branch1,els,branch2,aft) -> [73]
185 | Ast0.While(whl,lp,exp,rp,body,_) -> [74]
186 | Ast0.Do(d,body,whl,lp,exp,rp,sem) -> [75]
187 | Ast0.For(fr,lp,e1,sem1,e2,sem2,e3,rp,body,_) -> [76]
188 | Ast0.Iterator(nm,lp,args,rp,body,_) -> [142]
189 | Ast0.Switch(switch,lp,exp,rp,lb,decls,cases,rb) -> [125]
190 | Ast0.Break(br,sem) -> [100]
191 | Ast0.Continue(cont,sem) -> [101]
192 | Ast0.Label(l,dd) -> [144]
193 | Ast0.Goto(goto,l,sem) -> [145]
194 | Ast0.Return(ret,sem) -> [77]
195 | Ast0.ReturnExpr(ret,exp,sem) -> [78]
196 | Ast0.MetaStmt(name,_) -> [79]
197 | Ast0.MetaStmtList(name,_) -> [80]
198 | Ast0.Disj(_,statement_dots_list,_,_) -> [81]
199 | Ast0.Nest(_,stmt_dots,_,_,_) -> [82]
200 | Ast0.Exp(exp) -> [83]
201 | Ast0.TopExp(exp) -> [141]
202 | Ast0.Ty(ty) -> [124]
203 | Ast0.TopInit(init) -> [146]
204 | Ast0.Dots(d,whencode) -> [84]
205 | Ast0.Circles(d,whencode) -> [85]
206 | Ast0.Stars(d,whencode) -> [86]
207 | Ast0.Include(inc,name) -> [118]
208 | Ast0.Define(def,id,params,body) -> [119]
209 | Ast0.OptStm(re) -> [87]
210 | Ast0.UniqueStm(re) -> [88]
211
212 let case_line c =
213 match Ast0.unwrap c with
214 Ast0.Default(def,colon,code) -> [126]
215 | Ast0.Case(case,exp,colon,code) -> [127]
216 | Ast0.DisjCase(_,case_lines,_,_) -> [107]
217 | Ast0.OptCase(case) -> [128]
218
219 let top_level t =
220 match Ast0.unwrap t with
221 Ast0.DECL(stmt) -> [90]
222 | Ast0.FILEINFO(old_file,new_file) -> [92]
223 | Ast0.CODE(stmt_dots) -> [94]
224 | Ast0.ERRORWORDS(exps) -> [95]
225 | Ast0.OTHER(_) -> [96]
226
227 (* 99-101 already used *)