clang API Documentation

TokenLexer.h
Go to the documentation of this file.
00001 //===--- TokenLexer.h - Lex from a token buffer -----------------*- 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 TokenLexer interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_TOKENLEXER_H
00015 #define LLVM_CLANG_TOKENLEXER_H
00016 
00017 #include "clang/Basic/SourceLocation.h"
00018 
00019 namespace clang {
00020   class MacroInfo;
00021   class Preprocessor;
00022   class Token;
00023   class MacroArgs;
00024 
00025 /// TokenLexer - This implements a lexer that returns token from a macro body
00026 /// or token stream instead of lexing from a character buffer.  This is used for
00027 /// macro expansion and _Pragma handling, for example.
00028 ///
00029 class TokenLexer {
00030   /// Macro - The macro we are expanding from.  This is null if expanding a
00031   /// token stream.
00032   ///
00033   MacroInfo *Macro;
00034 
00035   /// ActualArgs - The actual arguments specified for a function-like macro, or
00036   /// null.  The TokenLexer owns the pointed-to object.
00037   MacroArgs *ActualArgs;
00038 
00039   /// PP - The current preprocessor object we are expanding for.
00040   ///
00041   Preprocessor &PP;
00042 
00043   /// Tokens - This is the pointer to an array of tokens that the macro is
00044   /// defined to, with arguments expanded for function-like macros.  If this is
00045   /// a token stream, these are the tokens we are returning.  This points into
00046   /// the macro definition we are lexing from, a cache buffer that is owned by
00047   /// the preprocessor, or some other buffer that we may or may not own
00048   /// (depending on OwnsTokens).
00049   /// Note that if it points into Preprocessor's cache buffer, the Preprocessor
00050   /// may update the pointer as needed.
00051   const Token *Tokens;
00052   friend class Preprocessor;
00053 
00054   /// NumTokens - This is the length of the Tokens array.
00055   ///
00056   unsigned NumTokens;
00057 
00058   /// CurToken - This is the next token that Lex will return.
00059   ///
00060   unsigned CurToken;
00061 
00062   /// ExpandLocStart/End - The source location range where this macro was
00063   /// expanded.
00064   SourceLocation ExpandLocStart, ExpandLocEnd;
00065 
00066   /// \brief Source location pointing at the source location entry chunk that
00067   /// was reserved for the current macro expansion.
00068   SourceLocation MacroExpansionStart;
00069   
00070   /// \brief The offset of the macro expansion in the
00071   /// "source location address space".
00072   unsigned MacroStartSLocOffset;
00073 
00074   /// \brief Location of the macro definition.
00075   SourceLocation MacroDefStart;
00076   /// \brief Length of the macro definition.
00077   unsigned MacroDefLength;
00078 
00079   /// Lexical information about the expansion point of the macro: the identifier
00080   /// that the macro expanded from had these properties.
00081   bool AtStartOfLine : 1;
00082   bool HasLeadingSpace : 1;
00083 
00084   /// OwnsTokens - This is true if this TokenLexer allocated the Tokens
00085   /// array, and thus needs to free it when destroyed.  For simple object-like
00086   /// macros (for example) we just point into the token buffer of the macro
00087   /// definition, we don't make a copy of it.
00088   bool OwnsTokens : 1;
00089 
00090   /// DisableMacroExpansion - This is true when tokens lexed from the TokenLexer
00091   /// should not be subject to further macro expansion.
00092   bool DisableMacroExpansion : 1;
00093 
00094   TokenLexer(const TokenLexer&);  // DO NOT IMPLEMENT
00095   void operator=(const TokenLexer&); // DO NOT IMPLEMENT
00096 public:
00097   /// Create a TokenLexer for the specified macro with the specified actual
00098   /// arguments.  Note that this ctor takes ownership of the ActualArgs pointer.
00099   /// ILEnd specifies the location of the ')' for a function-like macro or the
00100   /// identifier for an object-like macro.
00101   TokenLexer(Token &Tok, SourceLocation ILEnd, MacroArgs *ActualArgs,
00102              Preprocessor &pp)
00103     : Macro(0), ActualArgs(0), PP(pp), OwnsTokens(false) {
00104     Init(Tok, ILEnd, ActualArgs);
00105   }
00106 
00107   /// Init - Initialize this TokenLexer to expand from the specified macro
00108   /// with the specified argument information.  Note that this ctor takes
00109   /// ownership of the ActualArgs pointer.  ILEnd specifies the location of the
00110   /// ')' for a function-like macro or the identifier for an object-like macro.
00111   void Init(Token &Tok, SourceLocation ILEnd, MacroArgs *ActualArgs);
00112 
00113   /// Create a TokenLexer for the specified token stream.  If 'OwnsTokens' is
00114   /// specified, this takes ownership of the tokens and delete[]'s them when
00115   /// the token lexer is empty.
00116   TokenLexer(const Token *TokArray, unsigned NumToks, bool DisableExpansion,
00117              bool ownsTokens, Preprocessor &pp)
00118     : Macro(0), ActualArgs(0), PP(pp), OwnsTokens(false) {
00119     Init(TokArray, NumToks, DisableExpansion, ownsTokens);
00120   }
00121 
00122   /// Init - Initialize this TokenLexer with the specified token stream.
00123   /// This does not take ownership of the specified token vector.
00124   ///
00125   /// DisableExpansion is true when macro expansion of tokens lexed from this
00126   /// stream should be disabled.
00127   void Init(const Token *TokArray, unsigned NumToks,
00128             bool DisableMacroExpansion, bool OwnsTokens);
00129 
00130   ~TokenLexer() { destroy(); }
00131 
00132   /// isNextTokenLParen - If the next token lexed will pop this macro off the
00133   /// expansion stack, return 2.  If the next unexpanded token is a '(', return
00134   /// 1, otherwise return 0.
00135   unsigned isNextTokenLParen() const;
00136 
00137   /// Lex - Lex and return a token from this macro stream.
00138   void Lex(Token &Tok);
00139 
00140   /// isParsingPreprocessorDirective - Return true if we are in the middle of a
00141   /// preprocessor directive.
00142   bool isParsingPreprocessorDirective() const;
00143 
00144 private:
00145   void destroy();
00146 
00147   /// isAtEnd - Return true if the next lex call will pop this macro off the
00148   /// include stack.
00149   bool isAtEnd() const {
00150     return CurToken == NumTokens;
00151   }
00152 
00153   /// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ##
00154   /// operator.  Read the ## and RHS, and paste the LHS/RHS together.  If there
00155   /// are is another ## after it, chomp it iteratively.  Return the result as
00156   /// Tok.  If this returns true, the caller should immediately return the
00157   /// token.
00158   bool PasteTokens(Token &Tok);
00159 
00160   /// Expand the arguments of a function-like macro so that we can quickly
00161   /// return preexpanded tokens from Tokens.
00162   void ExpandFunctionArguments();
00163 
00164   /// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes
00165   /// together to form a comment that comments out everything in the current
00166   /// macro, other active macros, and anything left on the current physical
00167   /// source line of the expanded buffer.  Handle this by returning the
00168   /// first token on the next line.
00169   void HandleMicrosoftCommentPaste(Token &Tok);
00170 
00171   /// \brief If \arg loc is a FileID and points inside the current macro
00172   /// definition, returns the appropriate source location pointing at the
00173   /// macro expansion source location entry.
00174   SourceLocation getExpansionLocForMacroDefLoc(SourceLocation loc) const;
00175 
00176   /// \brief Creates SLocEntries and updates the locations of macro argument
00177   /// tokens to their new expanded locations.
00178   ///
00179   /// \param ArgIdSpellLoc the location of the macro argument id inside the
00180   /// macro definition.
00181   void updateLocForMacroArgTokens(SourceLocation ArgIdSpellLoc,
00182                                   Token *begin_tokens, Token *end_tokens);
00183 };
00184 
00185 }  // end namespace clang
00186 
00187 #endif