clang API Documentation

MacroArgs.h
Go to the documentation of this file.
00001 //===--- MacroArgs.h - Formal argument info for Macros ----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the MacroArgs interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_MACROARGS_H
00015 #define LLVM_CLANG_MACROARGS_H
00016 
00017 #include "llvm/ADT/ArrayRef.h"
00018 
00019 #include <vector>
00020 
00021 namespace clang {
00022   class MacroInfo;
00023   class Preprocessor;
00024   class Token;
00025   class SourceLocation;
00026 
00027 /// MacroArgs - An instance of this class captures information about
00028 /// the formal arguments specified to a function-like macro invocation.
00029 class MacroArgs {
00030   /// NumUnexpArgTokens - The number of raw, unexpanded tokens for the
00031   /// arguments.  All of the actual argument tokens are allocated immediately
00032   /// after the MacroArgs object in memory.  This is all of the arguments
00033   /// concatenated together, with 'EOF' markers at the end of each argument.
00034   unsigned NumUnexpArgTokens;
00035 
00036   /// VarargsElided - True if this is a C99 style varargs macro invocation and
00037   /// there was no argument specified for the "..." argument.  If the argument
00038   /// was specified (even empty) or this isn't a C99 style varargs function, or
00039   /// if in strict mode and the C99 varargs macro had only a ... argument, this
00040   /// is false.
00041   bool VarargsElided;
00042   
00043   /// PreExpArgTokens - Pre-expanded tokens for arguments that need them.  Empty
00044   /// if not yet computed.  This includes the EOF marker at the end of the
00045   /// stream.
00046   std::vector<std::vector<Token> > PreExpArgTokens;
00047 
00048   /// StringifiedArgs - This contains arguments in 'stringified' form.  If the
00049   /// stringified form of an argument has not yet been computed, this is empty.
00050   std::vector<Token> StringifiedArgs;
00051 
00052   /// ArgCache - This is a linked list of MacroArgs objects that the
00053   /// Preprocessor owns which we use to avoid thrashing malloc/free.
00054   MacroArgs *ArgCache;
00055   
00056   MacroArgs(unsigned NumToks, bool varargsElided)
00057     : NumUnexpArgTokens(NumToks), VarargsElided(varargsElided), ArgCache(0) {}
00058   ~MacroArgs() {}
00059 public:
00060   /// MacroArgs ctor function - Create a new MacroArgs object with the specified
00061   /// macro and argument info.
00062   static MacroArgs *create(const MacroInfo *MI,
00063                            llvm::ArrayRef<Token> UnexpArgTokens,
00064                            bool VarargsElided, Preprocessor &PP);
00065 
00066   /// destroy - Destroy and deallocate the memory for this object.
00067   ///
00068   void destroy(Preprocessor &PP);
00069 
00070   /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
00071   /// by pre-expansion, return false.  Otherwise, conservatively return true.
00072   bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const;
00073 
00074   /// getUnexpArgument - Return a pointer to the first token of the unexpanded
00075   /// token list for the specified formal.
00076   ///
00077   const Token *getUnexpArgument(unsigned Arg) const;
00078 
00079   /// getArgLength - Given a pointer to an expanded or unexpanded argument,
00080   /// return the number of tokens, not counting the EOF, that make up the
00081   /// argument.
00082   static unsigned getArgLength(const Token *ArgPtr);
00083 
00084   /// getPreExpArgument - Return the pre-expanded form of the specified
00085   /// argument.
00086   const std::vector<Token> &
00087     getPreExpArgument(unsigned Arg, const MacroInfo *MI, Preprocessor &PP);
00088 
00089   /// getStringifiedArgument - Compute, cache, and return the specified argument
00090   /// that has been 'stringified' as required by the # operator.
00091   const Token &getStringifiedArgument(unsigned ArgNo, Preprocessor &PP,
00092                                       SourceLocation ExpansionLocStart,
00093                                       SourceLocation ExpansionLocEnd);
00094 
00095   /// getNumArguments - Return the number of arguments passed into this macro
00096   /// invocation.
00097   unsigned getNumArguments() const { return NumUnexpArgTokens; }
00098 
00099 
00100   /// isVarargsElidedUse - Return true if this is a C99 style varargs macro
00101   /// invocation and there was no argument specified for the "..." argument.  If
00102   /// the argument was specified (even empty) or this isn't a C99 style varargs
00103   /// function, or if in strict mode and the C99 varargs macro had only a ...
00104   /// argument, this returns false.
00105   bool isVarargsElidedUse() const { return VarargsElided; }
00106 
00107   /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
00108   /// tokens into the literal string token that should be produced by the C #
00109   /// preprocessor operator.  If Charify is true, then it should be turned into
00110   /// a character literal for the Microsoft charize (#@) extension.
00111   ///
00112   static Token StringifyArgument(const Token *ArgToks,
00113                                  Preprocessor &PP, bool Charify,
00114                                  SourceLocation ExpansionLocStart,
00115                                  SourceLocation ExpansionLocEnd);
00116   
00117   
00118   /// deallocate - This should only be called by the Preprocessor when managing
00119   /// its freelist.
00120   MacroArgs *deallocate();
00121 };
00122 
00123 }  // end namespace clang
00124 
00125 #endif