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