Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 1 | //===--- MacroArgs.h - Formal argument info for Macros ----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the MacroArgs interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_CLANG_MACROARGS_H |
| 15 | #define LLVM_CLANG_MACROARGS_H |
| 16 | |
David Blaikie | d7bb6a0 | 2011-09-22 02:03:12 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ArrayRef.h" |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
| 20 | namespace clang { |
| 21 | class MacroInfo; |
| 22 | class Preprocessor; |
| 23 | class Token; |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 24 | class SourceLocation; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 25 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 26 | /// MacroArgs - An instance of this class captures information about |
| 27 | /// the formal arguments specified to a function-like macro invocation. |
| 28 | class MacroArgs { |
| 29 | /// NumUnexpArgTokens - The number of raw, unexpanded tokens for the |
| 30 | /// arguments. All of the actual argument tokens are allocated immediately |
| 31 | /// after the MacroArgs object in memory. This is all of the arguments |
| 32 | /// concatenated together, with 'EOF' markers at the end of each argument. |
| 33 | unsigned NumUnexpArgTokens; |
| 34 | |
Chris Lattner | 561395b | 2009-12-14 22:12:52 +0000 | [diff] [blame] | 35 | /// VarargsElided - True if this is a C99 style varargs macro invocation and |
| 36 | /// there was no argument specified for the "..." argument. If the argument |
| 37 | /// was specified (even empty) or this isn't a C99 style varargs function, or |
| 38 | /// if in strict mode and the C99 varargs macro had only a ... argument, this |
| 39 | /// is false. |
| 40 | bool VarargsElided; |
| 41 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 42 | /// PreExpArgTokens - Pre-expanded tokens for arguments that need them. Empty |
| 43 | /// if not yet computed. This includes the EOF marker at the end of the |
| 44 | /// stream. |
| 45 | std::vector<std::vector<Token> > PreExpArgTokens; |
| 46 | |
| 47 | /// StringifiedArgs - This contains arguments in 'stringified' form. If the |
| 48 | /// stringified form of an argument has not yet been computed, this is empty. |
| 49 | std::vector<Token> StringifiedArgs; |
| 50 | |
Chris Lattner | 23f77e5 | 2009-12-15 01:51:03 +0000 | [diff] [blame] | 51 | /// ArgCache - This is a linked list of MacroArgs objects that the |
| 52 | /// Preprocessor owns which we use to avoid thrashing malloc/free. |
| 53 | MacroArgs *ArgCache; |
| 54 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 55 | MacroArgs(unsigned NumToks, bool varargsElided) |
Chris Lattner | 23f77e5 | 2009-12-15 01:51:03 +0000 | [diff] [blame] | 56 | : NumUnexpArgTokens(NumToks), VarargsElided(varargsElided), ArgCache(0) {} |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 57 | ~MacroArgs() {} |
| 58 | public: |
| 59 | /// MacroArgs ctor function - Create a new MacroArgs object with the specified |
| 60 | /// macro and argument info. |
| 61 | static MacroArgs *create(const MacroInfo *MI, |
David Blaikie | d7bb6a0 | 2011-09-22 02:03:12 +0000 | [diff] [blame] | 62 | llvm::ArrayRef<Token> UnexpArgTokens, |
| 63 | bool VarargsElided, Preprocessor &PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 65 | /// destroy - Destroy and deallocate the memory for this object. |
| 66 | /// |
Chris Lattner | 561395b | 2009-12-14 22:12:52 +0000 | [diff] [blame] | 67 | void destroy(Preprocessor &PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 69 | /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected |
| 70 | /// by pre-expansion, return false. Otherwise, conservatively return true. |
| 71 | bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 73 | /// getUnexpArgument - Return a pointer to the first token of the unexpanded |
| 74 | /// token list for the specified formal. |
| 75 | /// |
| 76 | const Token *getUnexpArgument(unsigned Arg) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 78 | /// getArgLength - Given a pointer to an expanded or unexpanded argument, |
| 79 | /// return the number of tokens, not counting the EOF, that make up the |
| 80 | /// argument. |
| 81 | static unsigned getArgLength(const Token *ArgPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 83 | /// getPreExpArgument - Return the pre-expanded form of the specified |
| 84 | /// argument. |
| 85 | const std::vector<Token> & |
Chris Lattner | f5809a7 | 2009-12-28 06:17:16 +0000 | [diff] [blame] | 86 | getPreExpArgument(unsigned Arg, const MacroInfo *MI, Preprocessor &PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 88 | /// getStringifiedArgument - Compute, cache, and return the specified argument |
| 89 | /// that has been 'stringified' as required by the # operator. |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 90 | const Token &getStringifiedArgument(unsigned ArgNo, Preprocessor &PP, |
Abramo Bagnara | a08529c | 2011-10-03 18:39:03 +0000 | [diff] [blame] | 91 | SourceLocation ExpansionLocStart, |
| 92 | SourceLocation ExpansionLocEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 94 | /// getNumArguments - Return the number of arguments passed into this macro |
| 95 | /// invocation. |
| 96 | unsigned getNumArguments() const { return NumUnexpArgTokens; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
| 98 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 99 | /// isVarargsElidedUse - Return true if this is a C99 style varargs macro |
| 100 | /// invocation and there was no argument specified for the "..." argument. If |
| 101 | /// the argument was specified (even empty) or this isn't a C99 style varargs |
| 102 | /// function, or if in strict mode and the C99 varargs macro had only a ... |
| 103 | /// argument, this returns false. |
| 104 | bool isVarargsElidedUse() const { return VarargsElided; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 106 | /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of |
| 107 | /// tokens into the literal string token that should be produced by the C # |
| 108 | /// preprocessor operator. If Charify is true, then it should be turned into |
| 109 | /// a character literal for the Microsoft charize (#@) extension. |
| 110 | /// |
| 111 | static Token StringifyArgument(const Token *ArgToks, |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 112 | Preprocessor &PP, bool Charify, |
Abramo Bagnara | a08529c | 2011-10-03 18:39:03 +0000 | [diff] [blame] | 113 | SourceLocation ExpansionLocStart, |
| 114 | SourceLocation ExpansionLocEnd); |
Chris Lattner | 23f77e5 | 2009-12-15 01:51:03 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | /// deallocate - This should only be called by the Preprocessor when managing |
| 118 | /// its freelist. |
| 119 | MacroArgs *deallocate(); |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // end namespace clang |
| 123 | |
| 124 | #endif |