blob: 8dee5b3bc997bcc806ad5d0a4ddda2981b019146 [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
33 /// PreExpArgTokens - Pre-expanded tokens for arguments that need them. Empty
34 /// if not yet computed. This includes the EOF marker at the end of the
35 /// stream.
36 std::vector<std::vector<Token> > PreExpArgTokens;
37
38 /// StringifiedArgs - This contains arguments in 'stringified' form. If the
39 /// stringified form of an argument has not yet been computed, this is empty.
40 std::vector<Token> StringifiedArgs;
41
42 /// VarargsElided - True if this is a C99 style varargs macro invocation and
43 /// there was no argument specified for the "..." argument. If the argument
44 /// was specified (even empty) or this isn't a C99 style varargs function, or
45 /// if in strict mode and the C99 varargs macro had only a ... argument, this
46 /// is false.
47 bool VarargsElided;
Mike Stump1eb44332009-09-09 15:08:12 +000048
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000049 MacroArgs(unsigned NumToks, bool varargsElided)
50 : NumUnexpArgTokens(NumToks), VarargsElided(varargsElided) {}
51 ~MacroArgs() {}
52public:
53 /// MacroArgs ctor function - Create a new MacroArgs object with the specified
54 /// macro and argument info.
55 static MacroArgs *create(const MacroInfo *MI,
56 const Token *UnexpArgTokens,
57 unsigned NumArgTokens, bool VarargsElided);
Mike Stump1eb44332009-09-09 15:08:12 +000058
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000059 /// destroy - Destroy and deallocate the memory for this object.
60 ///
61 void destroy();
Mike Stump1eb44332009-09-09 15:08:12 +000062
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000063 /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
64 /// by pre-expansion, return false. Otherwise, conservatively return true.
65 bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const;
Mike Stump1eb44332009-09-09 15:08:12 +000066
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000067 /// getUnexpArgument - Return a pointer to the first token of the unexpanded
68 /// token list for the specified formal.
69 ///
70 const Token *getUnexpArgument(unsigned Arg) const;
Mike Stump1eb44332009-09-09 15:08:12 +000071
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000072 /// getArgLength - Given a pointer to an expanded or unexpanded argument,
73 /// return the number of tokens, not counting the EOF, that make up the
74 /// argument.
75 static unsigned getArgLength(const Token *ArgPtr);
Mike Stump1eb44332009-09-09 15:08:12 +000076
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000077 /// getPreExpArgument - Return the pre-expanded form of the specified
78 /// argument.
79 const std::vector<Token> &
Mike Stump1eb44332009-09-09 15:08:12 +000080 getPreExpArgument(unsigned Arg, Preprocessor &PP);
81
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000082 /// getStringifiedArgument - Compute, cache, and return the specified argument
83 /// that has been 'stringified' as required by the # operator.
84 const Token &getStringifiedArgument(unsigned ArgNo, Preprocessor &PP);
Mike Stump1eb44332009-09-09 15:08:12 +000085
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000086 /// getNumArguments - Return the number of arguments passed into this macro
87 /// invocation.
88 unsigned getNumArguments() const { return NumUnexpArgTokens; }
Mike Stump1eb44332009-09-09 15:08:12 +000089
90
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000091 /// isVarargsElidedUse - Return true if this is a C99 style varargs macro
92 /// invocation and there was no argument specified for the "..." argument. If
93 /// the argument was specified (even empty) or this isn't a C99 style varargs
94 /// function, or if in strict mode and the C99 varargs macro had only a ...
95 /// argument, this returns false.
96 bool isVarargsElidedUse() const { return VarargsElided; }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000098 /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
99 /// tokens into the literal string token that should be produced by the C #
100 /// preprocessor operator. If Charify is true, then it should be turned into
101 /// a character literal for the Microsoft charize (#@) extension.
102 ///
103 static Token StringifyArgument(const Token *ArgToks,
104 Preprocessor &PP, bool Charify = false);
105};
106
107} // end namespace clang
108
109#endif