blob: 6ff4856b4e1c03183f618c2945427d80440544dc [file] [log] [blame]
Chris Lattnere5c8ffe2008-03-09 02:55:12 +00001//===--- 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
17#include <vector>
18
19namespace clang {
20 class MacroInfo;
21 class Preprocessor;
22 class Token;
Mike Stump1eb44332009-09-09 15:08:12 +000023
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000024/// MacroArgs - An instance of this class captures information about
25/// the formal arguments specified to a function-like macro invocation.
26class MacroArgs {
27 /// NumUnexpArgTokens - The number of raw, unexpanded tokens for the
28 /// arguments. All of the actual argument tokens are allocated immediately
29 /// after the MacroArgs object in memory. This is all of the arguments
30 /// concatenated together, with 'EOF' markers at the end of each argument.
31 unsigned NumUnexpArgTokens;
32
Chris Lattner561395b2009-12-14 22:12:52 +000033 /// VarargsElided - True if this is a C99 style varargs macro invocation and
34 /// there was no argument specified for the "..." argument. If the argument
35 /// was specified (even empty) or this isn't a C99 style varargs function, or
36 /// if in strict mode and the C99 varargs macro had only a ... argument, this
37 /// is false.
38 bool VarargsElided;
39
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000040 /// PreExpArgTokens - Pre-expanded tokens for arguments that need them. Empty
41 /// if not yet computed. This includes the EOF marker at the end of the
42 /// stream.
43 std::vector<std::vector<Token> > PreExpArgTokens;
44
45 /// StringifiedArgs - This contains arguments in 'stringified' form. If the
46 /// stringified form of an argument has not yet been computed, this is empty.
47 std::vector<Token> StringifiedArgs;
48
Chris Lattner23f77e52009-12-15 01:51:03 +000049 /// ArgCache - This is a linked list of MacroArgs objects that the
50 /// Preprocessor owns which we use to avoid thrashing malloc/free.
51 MacroArgs *ArgCache;
52
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000053 MacroArgs(unsigned NumToks, bool varargsElided)
Chris Lattner23f77e52009-12-15 01:51:03 +000054 : NumUnexpArgTokens(NumToks), VarargsElided(varargsElided), ArgCache(0) {}
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000055 ~MacroArgs() {}
56public:
57 /// MacroArgs ctor function - Create a new MacroArgs object with the specified
58 /// macro and argument info.
59 static MacroArgs *create(const MacroInfo *MI,
60 const Token *UnexpArgTokens,
Chris Lattner561395b2009-12-14 22:12:52 +000061 unsigned NumArgTokens, bool VarargsElided,
62 Preprocessor &PP);
Mike Stump1eb44332009-09-09 15:08:12 +000063
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000064 /// destroy - Destroy and deallocate the memory for this object.
65 ///
Chris Lattner561395b2009-12-14 22:12:52 +000066 void destroy(Preprocessor &PP);
Mike Stump1eb44332009-09-09 15:08:12 +000067
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000068 /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
69 /// by pre-expansion, return false. Otherwise, conservatively return true.
70 bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const;
Mike Stump1eb44332009-09-09 15:08:12 +000071
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000072 /// getUnexpArgument - Return a pointer to the first token of the unexpanded
73 /// token list for the specified formal.
74 ///
75 const Token *getUnexpArgument(unsigned Arg) const;
Mike Stump1eb44332009-09-09 15:08:12 +000076
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000077 /// getArgLength - Given a pointer to an expanded or unexpanded argument,
78 /// return the number of tokens, not counting the EOF, that make up the
79 /// argument.
80 static unsigned getArgLength(const Token *ArgPtr);
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000082 /// getPreExpArgument - Return the pre-expanded form of the specified
83 /// argument.
84 const std::vector<Token> &
Chris Lattnerf5809a72009-12-28 06:17:16 +000085 getPreExpArgument(unsigned Arg, const MacroInfo *MI, Preprocessor &PP);
Mike Stump1eb44332009-09-09 15:08:12 +000086
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000087 /// getStringifiedArgument - Compute, cache, and return the specified argument
88 /// that has been 'stringified' as required by the # operator.
89 const Token &getStringifiedArgument(unsigned ArgNo, Preprocessor &PP);
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000091 /// getNumArguments - Return the number of arguments passed into this macro
92 /// invocation.
93 unsigned getNumArguments() const { return NumUnexpArgTokens; }
Mike Stump1eb44332009-09-09 15:08:12 +000094
95
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000096 /// isVarargsElidedUse - Return true if this is a C99 style varargs macro
97 /// invocation and there was no argument specified for the "..." argument. If
98 /// the argument was specified (even empty) or this isn't a C99 style varargs
99 /// function, or if in strict mode and the C99 varargs macro had only a ...
100 /// argument, this returns false.
101 bool isVarargsElidedUse() const { return VarargsElided; }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000103 /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
104 /// tokens into the literal string token that should be produced by the C #
105 /// preprocessor operator. If Charify is true, then it should be turned into
106 /// a character literal for the Microsoft charize (#@) extension.
107 ///
108 static Token StringifyArgument(const Token *ArgToks,
109 Preprocessor &PP, bool Charify = false);
Chris Lattner23f77e52009-12-15 01:51:03 +0000110
111
112 /// deallocate - This should only be called by the Preprocessor when managing
113 /// its freelist.
114 MacroArgs *deallocate();
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000115};
116
117} // end namespace clang
118
119#endif