10b57cec5SDimitry Andric //===--- MacroArgs.h - Formal argument info for Macros ----------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines the MacroArgs interface.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef LLVM_CLANG_LEX_MACROARGS_H
140b57cec5SDimitry Andric #define LLVM_CLANG_LEX_MACROARGS_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "clang/Basic/LLVM.h"
170b57cec5SDimitry Andric #include "clang/Lex/Token.h"
180b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
190b57cec5SDimitry Andric #include "llvm/Support/TrailingObjects.h"
200b57cec5SDimitry Andric #include <vector>
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric namespace clang {
230b57cec5SDimitry Andric   class MacroInfo;
240b57cec5SDimitry Andric   class Preprocessor;
250b57cec5SDimitry Andric   class SourceLocation;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric /// MacroArgs - An instance of this class captures information about
280b57cec5SDimitry Andric /// the formal arguments specified to a function-like macro invocation.
290b57cec5SDimitry Andric class MacroArgs final
300b57cec5SDimitry Andric     : private llvm::TrailingObjects<MacroArgs, Token> {
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric   friend TrailingObjects;
330b57cec5SDimitry Andric   /// NumUnexpArgTokens - The number of raw, unexpanded tokens for the
340b57cec5SDimitry Andric   /// arguments.  All of the actual argument tokens are allocated immediately
350b57cec5SDimitry Andric   /// after the MacroArgs object in memory.  This is all of the arguments
360b57cec5SDimitry Andric   /// concatenated together, with 'EOF' markers at the end of each argument.
370b57cec5SDimitry Andric   unsigned NumUnexpArgTokens;
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   /// VarargsElided - True if this is a C99 style varargs macro invocation and
400b57cec5SDimitry Andric   /// there was no argument specified for the "..." argument.  If the argument
410b57cec5SDimitry Andric   /// was specified (even empty) or this isn't a C99 style varargs function, or
420b57cec5SDimitry Andric   /// if in strict mode and the C99 varargs macro had only a ... argument, this
430b57cec5SDimitry Andric   /// is false.
440b57cec5SDimitry Andric   bool VarargsElided;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric   /// PreExpArgTokens - Pre-expanded tokens for arguments that need them.  Empty
470b57cec5SDimitry Andric   /// if not yet computed.  This includes the EOF marker at the end of the
480b57cec5SDimitry Andric   /// stream.
490b57cec5SDimitry Andric   std::vector<std::vector<Token> > PreExpArgTokens;
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   /// ArgCache - This is a linked list of MacroArgs objects that the
520b57cec5SDimitry Andric   /// Preprocessor owns which we use to avoid thrashing malloc/free.
530b57cec5SDimitry Andric   MacroArgs *ArgCache;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   /// MacroArgs - The number of arguments the invoked macro expects.
560b57cec5SDimitry Andric   unsigned NumMacroArgs;
570b57cec5SDimitry Andric 
MacroArgs(unsigned NumToks,bool varargsElided,unsigned MacroArgs)580b57cec5SDimitry Andric   MacroArgs(unsigned NumToks, bool varargsElided, unsigned MacroArgs)
590b57cec5SDimitry Andric       : NumUnexpArgTokens(NumToks), VarargsElided(varargsElided),
600b57cec5SDimitry Andric         ArgCache(nullptr), NumMacroArgs(MacroArgs) {}
610b57cec5SDimitry Andric   ~MacroArgs() = default;
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric public:
640b57cec5SDimitry Andric   /// MacroArgs ctor function - Create a new MacroArgs object with the specified
650b57cec5SDimitry Andric   /// macro and argument info.
660b57cec5SDimitry Andric   static MacroArgs *create(const MacroInfo *MI,
670b57cec5SDimitry Andric                            ArrayRef<Token> UnexpArgTokens,
680b57cec5SDimitry Andric                            bool VarargsElided, Preprocessor &PP);
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   /// destroy - Destroy and deallocate the memory for this object.
710b57cec5SDimitry Andric   ///
720b57cec5SDimitry Andric   void destroy(Preprocessor &PP);
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
750b57cec5SDimitry Andric   /// by pre-expansion, return false.  Otherwise, conservatively return true.
760b57cec5SDimitry Andric   bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const;
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   /// getUnexpArgument - Return a pointer to the first token of the unexpanded
790b57cec5SDimitry Andric   /// token list for the specified formal.
800b57cec5SDimitry Andric   ///
810b57cec5SDimitry Andric   const Token *getUnexpArgument(unsigned Arg) const;
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   /// getArgLength - Given a pointer to an expanded or unexpanded argument,
840b57cec5SDimitry Andric   /// return the number of tokens, not counting the EOF, that make up the
850b57cec5SDimitry Andric   /// argument.
860b57cec5SDimitry Andric   static unsigned getArgLength(const Token *ArgPtr);
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   /// getPreExpArgument - Return the pre-expanded form of the specified
890b57cec5SDimitry Andric   /// argument.
900b57cec5SDimitry Andric   const std::vector<Token> &
910b57cec5SDimitry Andric     getPreExpArgument(unsigned Arg, Preprocessor &PP);
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   /// getNumMacroArguments - Return the number of arguments the invoked macro
940b57cec5SDimitry Andric   /// expects.
getNumMacroArguments()950b57cec5SDimitry Andric   unsigned getNumMacroArguments() const { return NumMacroArgs; }
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   /// isVarargsElidedUse - Return true if this is a C99 style varargs macro
980b57cec5SDimitry Andric   /// invocation and there was no argument specified for the "..." argument.  If
990b57cec5SDimitry Andric   /// the argument was specified (even empty) or this isn't a C99 style varargs
1000b57cec5SDimitry Andric   /// function, or if in strict mode and the C99 varargs macro had only a ...
1010b57cec5SDimitry Andric   /// argument, this returns false.
isVarargsElidedUse()1020b57cec5SDimitry Andric   bool isVarargsElidedUse() const { return VarargsElided; }
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   /// Returns true if the macro was defined with a variadic (ellipsis) parameter
1050b57cec5SDimitry Andric   /// AND was invoked with at least one token supplied as a variadic argument
1060b57cec5SDimitry Andric   /// (after pre-expansion).
1070b57cec5SDimitry Andric   ///
1080b57cec5SDimitry Andric   /// \code
1090b57cec5SDimitry Andric   ///   #define F(a)  a
1100b57cec5SDimitry Andric   ///   #define V(a, ...) __VA_OPT__(a)
1110b57cec5SDimitry Andric   ///   F()     <-- returns false on this invocation.
1120b57cec5SDimitry Andric   ///   V(,a)   <-- returns true on this invocation.
1130b57cec5SDimitry Andric   ///   V(,)    <-- returns false on this invocation.
1140b57cec5SDimitry Andric   ///   V(,F()) <-- returns false on this invocation.
1150b57cec5SDimitry Andric   /// \endcode
1160b57cec5SDimitry Andric   ///
1170b57cec5SDimitry Andric   bool invokedWithVariadicArgument(const MacroInfo *const MI, Preprocessor &PP);
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
1200b57cec5SDimitry Andric   /// tokens into the literal string token that should be produced by the C #
1210b57cec5SDimitry Andric   /// preprocessor operator.  If Charify is true, then it should be turned into
1220b57cec5SDimitry Andric   /// a character literal for the Microsoft charize (#@) extension.
1230b57cec5SDimitry Andric   ///
1240b57cec5SDimitry Andric   static Token StringifyArgument(const Token *ArgToks,
1250b57cec5SDimitry Andric                                  Preprocessor &PP, bool Charify,
1260b57cec5SDimitry Andric                                  SourceLocation ExpansionLocStart,
1270b57cec5SDimitry Andric                                  SourceLocation ExpansionLocEnd);
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   /// deallocate - This should only be called by the Preprocessor when managing
1310b57cec5SDimitry Andric   /// its freelist.
1320b57cec5SDimitry Andric   MacroArgs *deallocate();
1330b57cec5SDimitry Andric };
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric }  // end namespace clang
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric #endif
1380b57cec5SDimitry Andric