| 1 | (* Yoann Padioleau |
| 2 | * |
| 3 | * Copyright (C) 2010, University of Copenhagen DIKU and INRIA. |
| 4 | * Copyright (C) 2009 University of Urbana Champaign |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License (GPL) |
| 8 | * version 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * file license.txt for more details. |
| 14 | *) |
| 15 | |
| 16 | |
| 17 | open Common |
| 18 | |
| 19 | (*****************************************************************************) |
| 20 | (* Prelude *) |
| 21 | (*****************************************************************************) |
| 22 | |
| 23 | (* This file may seems redundant with the tokens generated by Yacc |
| 24 | * from parser.mly in parser_c.mli. The problem is that we need for |
| 25 | * many reasons to remember in the ast_c the tokens invoved in this |
| 26 | * ast, not just the string, especially for the comment and cpp_passed |
| 27 | * tokens which pour le coup were not in the ast at all. So, |
| 28 | * to avoid recursive mutual dependencies, we provide this file |
| 29 | * so that ast_c does not need to depend on yacc which depends on |
| 30 | * ast_c, etc. |
| 31 | * |
| 32 | * Also, ocamlyacc imposes some stupid constraints on the way we can define |
| 33 | * the token type. ocamlyacc forces us to do a token type that |
| 34 | * cant be a pair of a sum type, it must be directly a sum type. |
| 35 | * We don't have this constraint here. |
| 36 | * |
| 37 | * Also, some yacc tokens are not used in the grammar because they are filtered |
| 38 | * in some intermediate phases. But they still must be declared because |
| 39 | * ocamllex may generate them, or some intermediate phase may also |
| 40 | * generate them (like some functions in parsing_hacks.ml). |
| 41 | * Here we don't have this problem again so we can have a clearer token type. |
| 42 | * |
| 43 | * |
| 44 | *) |
| 45 | |
| 46 | (*****************************************************************************) |
| 47 | (* Cpp constructs put in comments in lexer or parsing_hack *) |
| 48 | (*****************************************************************************) |
| 49 | |
| 50 | (* history: was in ast_c.ml before: |
| 51 | * This type is not in the Ast but is associated with the TCommentCpp |
| 52 | * token. I put this enum here because parser_c.mly need it. I could have put |
| 53 | * it also in lexer_parser. |
| 54 | * |
| 55 | * update: now in token_c.ml, and actually right now we want those tokens |
| 56 | * to be in the ast so that in the matching/transforming of C code, we |
| 57 | * can detect if some metavariables match code which have some |
| 58 | * cpp_passed tokens next to them (and so where we should issue a warning). |
| 59 | *) |
| 60 | type cppcommentkind = |
| 61 | | CppDirective |
| 62 | | CppAttr |
| 63 | | CppMacro |
| 64 | | CppPassingNormal (* ifdef 0, cplusplus, etc *) |
| 65 | | CppPassingCosWouldGetError (* expr passsing *) |
| 66 | | CppPassingExplicit (* skip_start/end tag *) |
| 67 | |
| 68 | (*****************************************************************************) |
| 69 | (* Types *) |
| 70 | (*****************************************************************************) |
| 71 | |
| 72 | (* |
| 73 | * TODO? Do we want to handle also non OriginTok-like tokens here ? |
| 74 | * Right now we use this file to be able to later store in the |
| 75 | * ast some information about comments and passed cpp tokens, to |
| 76 | * improve our matching/transforming and unparsing in coccinelle. |
| 77 | * So we should be concerned really only with origin tok, so right |
| 78 | * now I use a simple Common.parse_info, not the more complex |
| 79 | * Ast_c.parse_info, or even more complex Ast_c.info. |
| 80 | * Also right now I defined only the token_tags of comment-like |
| 81 | * tokens. |
| 82 | *) |
| 83 | |
| 84 | type info = Common.parse_info |
| 85 | |
| 86 | (* I try to be consistent with the names in parser_c.mli *) |
| 87 | type token = token_tag * info |
| 88 | and token_tag = |
| 89 | | TCommentSpace |
| 90 | | TCommentNewline |
| 91 | |
| 92 | | TComment |
| 93 | |
| 94 | (* the passed tokens because of our limited handling of cpp *) |
| 95 | | TCommentCpp of cppcommentkind |
| 96 | |
| 97 | (*| TUnknown ? *) |
| 98 | |
| 99 | |
| 100 | |
| 101 | (* Later if decide to include more kinds of tokens, then may |
| 102 | * have to move the current token_tag like TCommentXxx in their |
| 103 | * own type and have a generic TCommentLike of comment_like_token |
| 104 | * in token_tag. Could also do like in token_helpers have some |
| 105 | * is_xxx predicate, but it's not very pretty (but required when |
| 106 | * some tokens can belong to multiple categories). |
| 107 | * |
| 108 | * It's supposed to be all the tokens that are not otherwise represented |
| 109 | * in the ast via regular constructors and info. |
| 110 | *) |
| 111 | type comment_like_token = token |
| 112 | |
| 113 | |
| 114 | |
| 115 | (*****************************************************************************) |
| 116 | (* Getters *) |
| 117 | (*****************************************************************************) |
| 118 | |
| 119 | (* simpler than in token_helpers :) because we don't have the ocamlyacc |
| 120 | * constraints on how to define the token type. *) |
| 121 | let info_of_token = snd |
| 122 | |
| 123 | |
| 124 | |
| 125 | (*****************************************************************************) |
| 126 | (*****************************************************************************) |
| 127 | (* remaining tokens |
| 128 | |
| 129 | could define a type token_class = Comment | Ident | Operator | ... |
| 130 | |
| 131 | | TInt of (string * Ast_c.info) |
| 132 | | TFloat of ((string * Ast_c.floatType) * Ast_c.info) |
| 133 | | TChar of ((string * Ast_c.isWchar) * Ast_c.info) |
| 134 | | TString of ((string * Ast_c.isWchar) * Ast_c.info) |
| 135 | |
| 136 | | TIdent of (string * Ast_c.info) |
| 137 | | TypedefIdent of (string * Ast_c.info) |
| 138 | |
| 139 | | TOPar of (Ast_c.info) |
| 140 | | TCPar of (Ast_c.info) |
| 141 | | TOBrace of (Ast_c.info) |
| 142 | | TCBrace of (Ast_c.info) |
| 143 | | TOCro of (Ast_c.info) |
| 144 | | TCCro of (Ast_c.info) |
| 145 | | TDot of (Ast_c.info) |
| 146 | | TComma of (Ast_c.info) |
| 147 | | TPtrOp of (Ast_c.info) |
| 148 | | TInc of (Ast_c.info) |
| 149 | | TDec of (Ast_c.info) |
| 150 | | TAssign of (Ast_c.assignOp * Ast_c.info) |
| 151 | | TEq of (Ast_c.info) |
| 152 | | TWhy of (Ast_c.info) |
| 153 | | TTilde of (Ast_c.info) |
| 154 | | TBang of (Ast_c.info) |
| 155 | | TEllipsis of (Ast_c.info) |
| 156 | | TDotDot of (Ast_c.info) |
| 157 | | TPtVirg of (Ast_c.info) |
| 158 | | TOrLog of (Ast_c.info) |
| 159 | | TAndLog of (Ast_c.info) |
| 160 | | TOr of (Ast_c.info) |
| 161 | | TXor of (Ast_c.info) |
| 162 | | TAnd of (Ast_c.info) |
| 163 | | TEqEq of (Ast_c.info) |
| 164 | | TNotEq of (Ast_c.info) |
| 165 | | TInf of (Ast_c.info) |
| 166 | | TSup of (Ast_c.info) |
| 167 | | TInfEq of (Ast_c.info) |
| 168 | | TSupEq of (Ast_c.info) |
| 169 | | TShl of (Ast_c.info) |
| 170 | | TShr of (Ast_c.info) |
| 171 | | TPlus of (Ast_c.info) |
| 172 | | TMinus of (Ast_c.info) |
| 173 | | TMul of (Ast_c.info) |
| 174 | | TDiv of (Ast_c.info) |
| 175 | | TMod of (Ast_c.info) |
| 176 | | Tchar of (Ast_c.info) |
| 177 | | Tshort of (Ast_c.info) |
| 178 | | Tint of (Ast_c.info) |
| 179 | | Tdouble of (Ast_c.info) |
| 180 | | Tfloat of (Ast_c.info) |
| 181 | | Tlong of (Ast_c.info) |
| 182 | | Tunsigned of (Ast_c.info) |
| 183 | | Tsigned of (Ast_c.info) |
| 184 | | Tvoid of (Ast_c.info) |
| 185 | | Tauto of (Ast_c.info) |
| 186 | | Tregister of (Ast_c.info) |
| 187 | | Textern of (Ast_c.info) |
| 188 | | Tstatic of (Ast_c.info) |
| 189 | | Ttypedef of (Ast_c.info) |
| 190 | | Tconst of (Ast_c.info) |
| 191 | | Tvolatile of (Ast_c.info) |
| 192 | | Tstruct of (Ast_c.info) |
| 193 | | Tunion of (Ast_c.info) |
| 194 | | Tenum of (Ast_c.info) |
| 195 | | Tbreak of (Ast_c.info) |
| 196 | | Telse of (Ast_c.info) |
| 197 | | Tswitch of (Ast_c.info) |
| 198 | | Tcase of (Ast_c.info) |
| 199 | | Tcontinue of (Ast_c.info) |
| 200 | | Tfor of (Ast_c.info) |
| 201 | | Tdo of (Ast_c.info) |
| 202 | | Tif of (Ast_c.info) |
| 203 | | Twhile of (Ast_c.info) |
| 204 | | Treturn of (Ast_c.info) |
| 205 | | Tgoto of (Ast_c.info) |
| 206 | | Tdefault of (Ast_c.info) |
| 207 | | Tsizeof of (Ast_c.info) |
| 208 | | Trestrict of (Ast_c.info) |
| 209 | | Tasm of (Ast_c.info) |
| 210 | | Tattribute of (Ast_c.info) |
| 211 | | Tinline of (Ast_c.info) |
| 212 | | Ttypeof of (Ast_c.info) |
| 213 | |
| 214 | | TDefine of (Ast_c.info) |
| 215 | | TDefParamVariadic of ((string * Ast_c.info)) |
| 216 | |
| 217 | | TCppEscapedNewline of (Ast_c.info) |
| 218 | |
| 219 | | TOParDefine of (Ast_c.info) |
| 220 | | TOBraceDefineInit of (Ast_c.info) |
| 221 | | TIdentDefine of ((string * Ast_c.info)) |
| 222 | | TDefEOL of (Ast_c.info) |
| 223 | | TInclude of ((string * string * bool ref * Ast_c.info)) |
| 224 | | TIncludeStart of ((Ast_c.info * bool ref)) |
| 225 | | TIncludeFilename of ((string * Ast_c.info)) |
| 226 | | TIfdef of (((int * int) option ref * Ast_c.info)) |
| 227 | | TIfdefelse of (((int * int) option ref * Ast_c.info)) |
| 228 | | TIfdefelif of (((int * int) option ref * Ast_c.info)) |
| 229 | | TEndif of (((int * int) option ref * Ast_c.info)) |
| 230 | | TIfdefBool of ((bool * (int * int) option ref * Ast_c.info)) |
| 231 | | TIfdefMisc of ((bool * (int * int) option ref * Ast_c.info)) |
| 232 | | TIfdefVersion of ((bool * (int * int) option ref * Ast_c.info)) |
| 233 | | TUndef of (string * Ast_c.info) |
| 234 | | TCppDirectiveOther of (Ast_c.info) |
| 235 | |
| 236 | | TMacroAttr of ((string * Ast_c.info)) |
| 237 | | TMacroStmt of ((string * Ast_c.info)) |
| 238 | | TMacroString of ((string * Ast_c.info)) |
| 239 | | TMacroDecl of ((string * Ast_c.info)) |
| 240 | | TMacroDeclConst of (Ast_c.info) |
| 241 | | TMacroStructDecl of ((string * Ast_c.info)) |
| 242 | | TMacroIterator of ((string * Ast_c.info)) |
| 243 | | TMacroAttrStorage of ((string * Ast_c.info)) |
| 244 | |
| 245 | | TCommentSkipTagStart of (Ast_c.info) |
| 246 | | TCommentSkipTagEnd of (Ast_c.info) |
| 247 | |
| 248 | | TCParEOL of (Ast_c.info) |
| 249 | | TAction of (Ast_c.info) |
| 250 | |
| 251 | | TCommentMisc xxx |
| 252 | |
| 253 | | EOF of (Ast_c.info) |
| 254 | *) |
| 255 | |
| 256 | |
| 257 | (*****************************************************************************) |
| 258 | (* Helpers *) |
| 259 | (*****************************************************************************) |