X-Git-Url: https://git.hcoop.net/bpt/coccinelle.git/blobdiff_plain/b1b2de814d2c59af2526bc19d41bb22a0c1fd16d..9bc82bae75129fec4d981ebf245f2f7d7ca73a41:/parsing_cocci/visitor_ast.ml diff --git a/parsing_cocci/visitor_ast.ml b/parsing_cocci/visitor_ast.ml index d01dc06..67a6996 100644 --- a/parsing_cocci/visitor_ast.ml +++ b/parsing_cocci/visitor_ast.ml @@ -1,23 +1,49 @@ (* -* Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen -* Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller -* This file is part of Coccinelle. -* -* Coccinelle is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, according to version 2 of the License. -* -* Coccinelle is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Coccinelle. If not, see . -* -* The authors reserve the right to distribute this or future versions of -* Coccinelle under other licenses. -*) + * Copyright 2010, INRIA, University of Copenhagen + * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix + * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen + * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix + * This file is part of Coccinelle. + * + * Coccinelle is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, according to version 2 of the License. + * + * Coccinelle is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Coccinelle. If not, see . + * + * The authors reserve the right to distribute this or future versions of + * Coccinelle under other licenses. + *) + + +(* + * Copyright 2010, INRIA, University of Copenhagen + * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix + * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen + * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix + * This file is part of Coccinelle. + * + * Coccinelle is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, according to version 2 of the License. + * + * Coccinelle is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Coccinelle. If not, see . + * + * The authors reserve the right to distribute this or future versions of + * Coccinelle under other licenses. + *) module Ast0 = Ast0_cocci @@ -47,7 +73,8 @@ type 'a combiner = combiner_anything : Ast.anything -> 'a; combiner_expression_dots : Ast.expression Ast.dots -> 'a; combiner_statement_dots : Ast.statement Ast.dots -> 'a; - combiner_declaration_dots : Ast.declaration Ast.dots -> 'a} + combiner_declaration_dots : Ast.declaration Ast.dots -> 'a; + combiner_initialiser_dots : Ast.initialiser Ast.dots -> 'a} type ('mc,'a) cmcode = 'a combiner -> 'mc Ast_cocci.mcode -> 'a type ('cd,'a) ccode = 'a combiner -> ('cd -> 'a) -> 'cd -> 'a @@ -58,7 +85,7 @@ let combiner bind option_default unary_mcodefn binary_mcodefn cv_mcodefn sign_mcodefn struct_mcodefn storage_mcodefn inc_file_mcodefn - expdotsfn paramdotsfn stmtdotsfn decldotsfn + expdotsfn paramdotsfn stmtdotsfn decldotsfn initdotsfn identfn exprfn ftfn tyfn initfn paramfn declfn rulefn stmtfn casefn topfn anyfn = let multibind l = @@ -71,6 +98,13 @@ let combiner bind option_default Some x -> f x | None -> option_default in + let dotsfn param default all_functions arg = + let k d = + match Ast.unwrap d with + Ast.DOTS(l) | Ast.CIRCLES(l) | Ast.STARS(l) -> + multibind (List.map default l) in + param all_functions k arg in + let rec meta_mcode x = meta_mcodefn all_functions x and string_mcode x = string_mcodefn all_functions x and const_mcode x = const_mcodefn all_functions x @@ -84,44 +118,22 @@ let combiner bind option_default and storage_mcode x = storage_mcodefn all_functions x and inc_file_mcode x = inc_file_mcodefn all_functions x - and expression_dots d = - let k d = - match Ast.unwrap d with - Ast.DOTS(l) | Ast.CIRCLES(l) | Ast.STARS(l) -> - multibind (List.map expression l) in - expdotsfn all_functions k d - - and parameter_dots d = - let k d = - match Ast.unwrap d with - Ast.DOTS(l) | Ast.CIRCLES(l) | Ast.STARS(l) -> - multibind (List.map parameterTypeDef l) in - paramdotsfn all_functions k d - - and statement_dots d = - let k d = - match Ast.unwrap d with - Ast.DOTS(l) | Ast.CIRCLES(l) | Ast.STARS(l) -> - multibind (List.map statement l) in - stmtdotsfn all_functions k d - - and declaration_dots d = - let k d = - match Ast.unwrap d with - Ast.DOTS(l) | Ast.CIRCLES(l) | Ast.STARS(l) -> - multibind (List.map declaration l) in - decldotsfn all_functions k d + and expression_dots d = dotsfn expdotsfn expression all_functions d + and parameter_dots d = dotsfn paramdotsfn parameterTypeDef all_functions d + and statement_dots d = dotsfn stmtdotsfn statement all_functions d + and declaration_dots d = dotsfn decldotsfn declaration all_functions d + and initialiser_dots d = dotsfn initdotsfn initialiser all_functions d and ident i = let k i = match Ast.unwrap i with - Ast.Id(name) -> string_mcode name - | Ast.MetaId(name,_,_,_) -> meta_mcode name - | Ast.MetaFunc(name,_,_,_) -> meta_mcode name - | Ast.MetaLocalFunc(name,_,_,_) -> meta_mcode name - | Ast.OptIdent(id) -> ident id - | Ast.UniqueIdent(id) -> ident id in - identfn all_functions k i + Ast.Id(name) -> string_mcode name + | Ast.MetaId(name,_,_,_) -> meta_mcode name + | Ast.MetaFunc(name,_,_,_) -> meta_mcode name + | Ast.MetaLocalFunc(name,_,_,_) -> meta_mcode name + | Ast.OptIdent(id) -> ident id + | Ast.UniqueIdent(id) -> ident id in + identfn all_functions k i and expression e = let k e = @@ -168,8 +180,11 @@ let combiner bind option_default | Ast.MetaExprList(name,_,_,_) -> meta_mcode name | Ast.EComma(cm) -> string_mcode cm | Ast.DisjExpr(exp_list) -> multibind (List.map expression exp_list) - | Ast.NestExpr(expr_dots,whencode,multi) -> - bind (expression_dots expr_dots) (get_option expression whencode) + | Ast.NestExpr(starter,expr_dots,ender,whencode,multi) -> + bind (string_mcode starter) + (bind (expression_dots expr_dots) + (bind (string_mcode ender) + (get_option expression whencode))) | Ast.Edots(dots,whencode) | Ast.Ecircles(dots,whencode) | Ast.Estars(dots,whencode) -> bind (string_mcode dots) (get_option expression whencode) @@ -216,7 +231,12 @@ let combiner bind option_default | Ast.FunctionType (_,ty,lp1,params,rp1) -> function_type (ty,lp1,params,rp1) [] | Ast.Array(ty,lb,size,rb) -> array_type (ty,lb,size,rb) [] - | Ast.EnumName(kind,name) -> bind (string_mcode kind) (ident name) + | Ast.EnumName(kind,name) -> + bind (string_mcode kind) (get_option ident name) + | Ast.EnumDef(ty,lb,ids,rb) -> + multibind + [fullType ty; string_mcode lb; expression_dots ids; + string_mcode rb] | Ast.StructUnionName(kind,name) -> bind (struct_mcode kind) (get_option ident name) | Ast.StructUnionDef(ty,lb,decls,rb) -> @@ -242,7 +262,8 @@ let combiner bind option_default and declaration d = let k d = match Ast.unwrap d with - Ast.Init(stg,ty,id,eq,ini,sem) -> + Ast.MetaDecl(name,_,_) | Ast.MetaField(name,_,_) -> meta_mcode name + | Ast.Init(stg,ty,id,eq,ini,sem) -> bind (get_option storage_mcode stg) (bind (named_type ty id) (multibind @@ -261,7 +282,6 @@ let combiner bind option_default | Ast.DisjDecl(decls) -> multibind (List.map declaration decls) | Ast.Ddots(dots,whencode) -> bind (string_mcode dots) (get_option declaration whencode) - | Ast.MetaDecl(name,_,_) -> meta_mcode name | Ast.OptDecl(decl) -> declaration decl | Ast.UniqueDecl(decl) -> declaration decl in declfn all_functions k d @@ -271,7 +291,10 @@ let combiner bind option_default match Ast.unwrap i with Ast.MetaInit(name,_,_) -> meta_mcode name | Ast.InitExpr(exp) -> expression exp - | Ast.InitList(lb,initlist,rb,whencode) -> + | Ast.ArInitList(lb,initlist,rb) -> + multibind + [string_mcode lb; initialiser_dots initlist; string_mcode rb] + | Ast.StrInitList(allminus,lb,initlist,rb,whencode) -> multibind [string_mcode lb; multibind (List.map initialiser initlist); @@ -284,6 +307,8 @@ let combiner bind option_default ((List.map designator designators) @ [string_mcode eq; initialiser ini]) | Ast.IComma(cm) -> string_mcode cm + | Ast.Idots(dots,whencode) -> + bind (string_mcode dots) (get_option initialiser whencode) | Ast.OptIni(i) -> initialiser i | Ast.UniqueIni(i) -> initialiser i in initfn all_functions k i @@ -411,8 +436,8 @@ let combiner bind option_default process_bef_aft s; let k s = match Ast.unwrap s with - Ast.Seq(lbrace,decls,body,rbrace) -> - multibind [rule_elem lbrace; statement_dots decls; + Ast.Seq(lbrace,body,rbrace) -> + multibind [rule_elem lbrace; statement_dots body; rule_elem rbrace] | Ast.IfThen(header,branch,_) -> multibind [rule_elem header; statement branch] @@ -426,20 +451,23 @@ let combiner bind option_default | Ast.For(header,body,_) -> multibind [rule_elem header; statement body] | Ast.Iterator(header,body,_) -> multibind [rule_elem header; statement body] - | Ast.Switch(header,lb,cases,rb) -> + | Ast.Switch(header,lb,decls,cases,rb) -> multibind [rule_elem header;rule_elem lb; + statement_dots decls; multibind (List.map case_line cases); rule_elem rb] | Ast.Atomic(re) -> rule_elem re | Ast.Disj(stmt_dots_list) -> multibind (List.map statement_dots stmt_dots_list) - | Ast.Nest(stmt_dots,whn,_,_,_) -> - bind (statement_dots stmt_dots) - (multibind (List.map (whencode statement_dots statement) whn)) - | Ast.FunDecl(header,lbrace,decls,body,rbrace) -> + | Ast.Nest(starter,stmt_dots,ender,whn,_,_,_) -> + bind (string_mcode starter) + (bind (statement_dots stmt_dots) + (bind (string_mcode ender) + (multibind + (List.map (whencode statement_dots statement) whn)))) + | Ast.FunDecl(header,lbrace,body,rbrace) -> multibind [rule_elem header; rule_elem lbrace; - statement_dots decls; statement_dots body; - rule_elem rbrace] + statement_dots body; rule_elem rbrace] | Ast.Define(header,body) -> bind (rule_elem header) (statement_dots body) | Ast.Dots(d,whn,_,_) | Ast.Circles(d,whn,_,_) | Ast.Stars(d,whn,_,_) -> @@ -533,7 +561,8 @@ let combiner bind option_default combiner_anything = anything; combiner_expression_dots = expression_dots; combiner_statement_dots = statement_dots; - combiner_declaration_dots = declaration_dots} in + combiner_declaration_dots = declaration_dots; + combiner_initialiser_dots = initialiser_dots} in all_functions (* ---------------------------------------------------------------------- *) @@ -556,6 +585,7 @@ type rebuilder = rebuilder_expression_dots : Ast.expression Ast.dots inout; rebuilder_statement_dots : Ast.statement Ast.dots inout; rebuilder_declaration_dots : Ast.declaration Ast.dots inout; + rebuilder_initialiser_dots : Ast.initialiser Ast.dots inout; rebuilder_define_param_dots : Ast.define_param Ast.dots inout; rebuilder_define_param : Ast.define_param inout; rebuilder_define_parameters : Ast.define_parameters inout; @@ -569,47 +599,27 @@ let rebuilder meta_mcode string_mcode const_mcode assign_mcode fix_mcode unary_mcode binary_mcode cv_mcode sign_mcode struct_mcode storage_mcode inc_file_mcode - expdotsfn paramdotsfn stmtdotsfn decldotsfn + expdotsfn paramdotsfn stmtdotsfn decldotsfn initdotsfn identfn exprfn ftfn tyfn initfn paramfn declfn rulefn stmtfn casefn topfn anyfn = let get_option f = function Some x -> Some (f x) | None -> None in - let rec expression_dots d = - let k d = - Ast.rewrap d - (match Ast.unwrap d with - Ast.DOTS(l) -> Ast.DOTS(List.map expression l) - | Ast.CIRCLES(l) -> Ast.CIRCLES(List.map expression l) - | Ast.STARS(l) -> Ast.STARS(List.map expression l)) in - expdotsfn all_functions k d - - and parameter_dots d = - let k d = - Ast.rewrap d - (match Ast.unwrap d with - Ast.DOTS(l) -> Ast.DOTS(List.map parameterTypeDef l) - | Ast.CIRCLES(l) -> Ast.CIRCLES(List.map parameterTypeDef l) - | Ast.STARS(l) -> Ast.STARS(List.map parameterTypeDef l)) in - paramdotsfn all_functions k d - and statement_dots d = + let dotsfn param default all_functions arg = let k d = Ast.rewrap d (match Ast.unwrap d with - Ast.DOTS(l) -> Ast.DOTS(List.map statement l) - | Ast.CIRCLES(l) -> Ast.CIRCLES(List.map statement l) - | Ast.STARS(l) -> Ast.STARS(List.map statement l)) in - stmtdotsfn all_functions k d + Ast.DOTS(l) -> Ast.DOTS(List.map default l) + | Ast.CIRCLES(l) -> Ast.CIRCLES(List.map default l) + | Ast.STARS(l) -> Ast.STARS(List.map default l)) in + param all_functions k arg in - and declaration_dots d = - let k d = - Ast.rewrap d - (match Ast.unwrap d with - Ast.DOTS(l) -> Ast.DOTS(List.map declaration l) - | Ast.CIRCLES(l) -> Ast.CIRCLES(List.map declaration l) - | Ast.STARS(l) -> Ast.STARS(List.map declaration l)) in - decldotsfn all_functions k d + let rec expression_dots d = dotsfn expdotsfn expression all_functions d + and parameter_dots d = dotsfn paramdotsfn parameterTypeDef all_functions d + and statement_dots d = dotsfn stmtdotsfn statement all_functions d + and declaration_dots d = dotsfn decldotsfn declaration all_functions d + and initialiser_dots d = dotsfn initdotsfn initialiser all_functions d and ident i = let k i = @@ -675,8 +685,9 @@ let rebuilder Ast.MetaExprList(meta_mcode name,lenname_inh,keep,inherited) | Ast.EComma(cm) -> Ast.EComma(string_mcode cm) | Ast.DisjExpr(exp_list) -> Ast.DisjExpr(List.map expression exp_list) - | Ast.NestExpr(expr_dots,whencode,multi) -> - Ast.NestExpr(expression_dots expr_dots, + | Ast.NestExpr(starter,expr_dots,ender,whencode,multi) -> + Ast.NestExpr(string_mcode starter,expression_dots expr_dots, + string_mcode ender, get_option expression whencode,multi) | Ast.Edots(dots,whencode) -> Ast.Edots(string_mcode dots,get_option expression whencode) @@ -720,7 +731,10 @@ let rebuilder Ast.Array(fullType ty, string_mcode lb, get_option expression size, string_mcode rb) | Ast.EnumName(kind,name) -> - Ast.EnumName(string_mcode kind, ident name) + Ast.EnumName(string_mcode kind, get_option ident name) + | Ast.EnumDef(ty,lb,ids,rb) -> + Ast.EnumDef (fullType ty, string_mcode lb, expression_dots ids, + string_mcode rb) | Ast.StructUnionName(kind,name) -> Ast.StructUnionName (struct_mcode kind, get_option ident name) | Ast.StructUnionDef(ty,lb,decls,rb) -> @@ -736,7 +750,11 @@ let rebuilder let k d = Ast.rewrap d (match Ast.unwrap d with - Ast.Init(stg,ty,id,eq,ini,sem) -> + Ast.MetaDecl(name,keep,inherited) -> + Ast.MetaDecl(meta_mcode name,keep,inherited) + | Ast.MetaField(name,keep,inherited) -> + Ast.MetaField(meta_mcode name,keep,inherited) + | Ast.Init(stg,ty,id,eq,ini,sem) -> Ast.Init(get_option storage_mcode stg, fullType ty, ident id, string_mcode eq, initialiser ini, string_mcode sem) | Ast.UnInit(stg,ty,id,sem) -> @@ -752,8 +770,6 @@ let rebuilder | Ast.DisjDecl(decls) -> Ast.DisjDecl(List.map declaration decls) | Ast.Ddots(dots,whencode) -> Ast.Ddots(string_mcode dots, get_option declaration whencode) - | Ast.MetaDecl(name,keep,inherited) -> - Ast.MetaDecl(meta_mcode name,keep,inherited) | Ast.OptDecl(decl) -> Ast.OptDecl(declaration decl) | Ast.UniqueDecl(decl) -> Ast.UniqueDecl(declaration decl)) in declfn all_functions k d @@ -765,8 +781,12 @@ let rebuilder Ast.MetaInit(name,keep,inherited) -> Ast.MetaInit(meta_mcode name,keep,inherited) | Ast.InitExpr(exp) -> Ast.InitExpr(expression exp) - | Ast.InitList(lb,initlist,rb,whencode) -> - Ast.InitList(string_mcode lb, List.map initialiser initlist, + | Ast.ArInitList(lb,initlist,rb) -> + Ast.ArInitList(string_mcode lb, initialiser_dots initlist, + string_mcode rb) + | Ast.StrInitList(allminus,lb,initlist,rb,whencode) -> + Ast.StrInitList(allminus, + string_mcode lb, List.map initialiser initlist, string_mcode rb, List.map initialiser whencode) | Ast.InitGccName(name,eq,ini) -> Ast.InitGccName(ident name, string_mcode eq, initialiser ini) @@ -775,6 +795,8 @@ let rebuilder (List.map designator designators, string_mcode eq, initialiser ini) | Ast.IComma(cm) -> Ast.IComma(string_mcode cm) + | Ast.Idots(dots,whencode) -> + Ast.Idots(string_mcode dots,get_option initialiser whencode) | Ast.OptIni(i) -> Ast.OptIni(initialiser i) | Ast.UniqueIni(i) -> Ast.UniqueIni(initialiser i)) in initfn all_functions k i @@ -920,8 +942,8 @@ let rebuilder let k s = Ast.rewrap s (match Ast.unwrap s with - Ast.Seq(lbrace,decls,body,rbrace) -> - Ast.Seq(rule_elem lbrace, statement_dots decls, + Ast.Seq(lbrace,body,rbrace) -> + Ast.Seq(rule_elem lbrace, statement_dots body, rule_elem rbrace) | Ast.IfThen(header,branch,aft) -> Ast.IfThen(rule_elem header, statement branch,aft) @@ -936,19 +958,20 @@ let rebuilder Ast.For(rule_elem header, statement body, aft) | Ast.Iterator(header,body,aft) -> Ast.Iterator(rule_elem header, statement body, aft) - | Ast.Switch(header,lb,cases,rb) -> + | Ast.Switch(header,lb,decls,cases,rb) -> Ast.Switch(rule_elem header,rule_elem lb, + statement_dots decls, List.map case_line cases,rule_elem rb) | Ast.Atomic(re) -> Ast.Atomic(rule_elem re) | Ast.Disj(stmt_dots_list) -> Ast.Disj (List.map statement_dots stmt_dots_list) - | Ast.Nest(stmt_dots,whn,multi,bef,aft) -> - Ast.Nest(statement_dots stmt_dots, + | Ast.Nest(starter,stmt_dots,ender,whn,multi,bef,aft) -> + Ast.Nest(string_mcode starter,statement_dots stmt_dots, + string_mcode ender, List.map (whencode statement_dots statement) whn, multi,bef,aft) - | Ast.FunDecl(header,lbrace,decls,body,rbrace) -> + | Ast.FunDecl(header,lbrace,body,rbrace) -> Ast.FunDecl(rule_elem header,rule_elem lbrace, - statement_dots decls, statement_dots body, rule_elem rbrace) | Ast.Define(header,body) -> Ast.Define(rule_elem header,statement_dots body) @@ -1043,7 +1066,7 @@ let rebuilder and all_functions = {rebuilder_ident = ident; rebuilder_expression = expression; - rebuilder_fullType= fullType; + rebuilder_fullType = fullType; rebuilder_typeC = typeC; rebuilder_declaration = declaration; rebuilder_initialiser = initialiser; @@ -1056,6 +1079,7 @@ let rebuilder rebuilder_expression_dots = expression_dots; rebuilder_statement_dots = statement_dots; rebuilder_declaration_dots = declaration_dots; + rebuilder_initialiser_dots = initialiser_dots; rebuilder_define_param_dots = define_param_dots; rebuilder_define_param = define_param; rebuilder_define_parameters = define_parameters;