blob: cf86d710adb711d33827f931e1e81efb163adfaa [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
David Blaikied7bb6a02011-09-22 02:03:12 +000017#include "llvm/ADT/ArrayRef.h"
18
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000019#include <vector>
20
21namespace clang {
22 class MacroInfo;
23 class Preprocessor;
24 class Token;
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000025 class SourceLocation;
Mike Stump1eb44332009-09-09 15:08:12 +000026
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000027/// MacroArgs - An instance of this class captures information about
28/// the formal arguments specified to a function-like macro invocation.
29class MacroArgs {
30 /// NumUnexpArgTokens - The number of raw, unexpanded tokens for the
31 /// arguments. All of the actual argument tokens are allocated immediately
32 /// after the MacroArgs object in memory. This is all of the arguments
33 /// concatenated together, with 'EOF' markers at the end of each argument.
34 unsigned NumUnexpArgTokens;
35
Chris Lattner561395b2009-12-14 22:12:52 +000036 /// VarargsElided - True if this is a C99 style varargs macro invocation and
37 /// there was no argument specified for the "..." argument. If the argument
38 /// was specified (even empty) or this isn't a C99 style varargs function, or
39 /// if in strict mode and the C99 varargs macro had only a ... argument, this
40 /// is false.
41 bool VarargsElided;
42
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000043 /// PreExpArgTokens - Pre-expanded tokens for arguments that need them. Empty
44 /// if not yet computed. This includes the EOF marker at the end of the
45 /// stream.
46 std::vector<std::vector<Token> > PreExpArgTokens;
47
48 /// StringifiedArgs - This contains arguments in 'stringified' form. If the
49 /// stringified form of an argument has not yet been computed, this is empty.
50 std::vector<Token> StringifiedArgs;
51
Chris Lattner23f77e52009-12-15 01:51:03 +000052 /// ArgCache - This is a linked list of MacroArgs objects that the
53 /// Preprocessor owns which we use to avoid thrashing malloc/free.
54 MacroArgs *ArgCache;
55
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000056 MacroArgs(unsigned NumToks, bool varargsElided)
Chris Lattner23f77e52009-12-15 01:51:03 +000057 : NumUnexpArgTokens(NumToks), VarargsElided(varargsElided), ArgCache(0) {}
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000058 ~MacroArgs() {}
59public:
60 /// MacroArgs ctor function - Create a new MacroArgs object with the specified
61 /// macro and argument info.
62 static MacroArgs *create(const MacroInfo *MI,
David Blaikied7bb6a02011-09-22 02:03:12 +000063 llvm::ArrayRef<Token> UnexpArgTokens,
64 bool VarargsElided, Preprocessor &PP);
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000066 /// destroy - Destroy and deallocate the memory for this object.
67 ///
Chris Lattner561395b2009-12-14 22:12:52 +000068 void destroy(Preprocessor &PP);
Mike Stump1eb44332009-09-09 15:08:12 +000069
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000070 /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
71 /// by pre-expansion, return false. Otherwise, conservatively return true.
72 bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000074 /// getUnexpArgument - Return a pointer to the first token of the unexpanded
75 /// token list for the specified formal.
76 ///
77 const Token *getUnexpArgument(unsigned Arg) const;
Mike Stump1eb44332009-09-09 15:08:12 +000078
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000079 /// getArgLength - Given a pointer to an expanded or unexpanded argument,
80 /// return the number of tokens, not counting the EOF, that make up the
81 /// argument.
82 static unsigned getArgLength(const Token *ArgPtr);
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000084 /// getPreExpArgument - Return the pre-expanded form of the specified
85 /// argument.
86 const std::vector<Token> &
Chris Lattnerf5809a72009-12-28 06:17:16 +000087 getPreExpArgument(unsigned Arg, const MacroInfo *MI, Preprocessor &PP);
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000089 /// getStringifiedArgument - Compute, cache, and return the specified argument
90 /// that has been 'stringified' as required by the # operator.
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000091 const Token &getStringifiedArgument(unsigned ArgNo, Preprocessor &PP,
Abramo Bagnaraa08529c2011-10-03 18:39:03 +000092 SourceLocation ExpansionLocStart,
93 SourceLocation ExpansionLocEnd);
Mike Stump1eb44332009-09-09 15:08:12 +000094
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000095 /// getNumArguments - Return the number of arguments passed into this macro
96 /// invocation.
97 unsigned getNumArguments() const { return NumUnexpArgTokens; }
Mike Stump1eb44332009-09-09 15:08:12 +000098
99
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000100 /// isVarargsElidedUse - Return true if this is a C99 style varargs macro
101 /// invocation and there was no argument specified for the "..." argument. If
102 /// the argument was specified (even empty) or this isn't a C99 style varargs
103 /// function, or if in strict mode and the C99 varargs macro had only a ...
104 /// argument, this returns false.
105 bool isVarargsElidedUse() const { return VarargsElided; }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000107 /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
108 /// tokens into the literal string token that should be produced by the C #
109 /// preprocessor operator. If Charify is true, then it should be turned into
110 /// a character literal for the Microsoft charize (#@) extension.
111 ///
112 static Token StringifyArgument(const Token *ArgToks,
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +0000113 Preprocessor &PP, bool Charify,
Abramo Bagnaraa08529c2011-10-03 18:39:03 +0000114 SourceLocation ExpansionLocStart,
115 SourceLocation ExpansionLocEnd);
Chris Lattner23f77e52009-12-15 01:51:03 +0000116
117
118 /// deallocate - This should only be called by the Preprocessor when managing
119 /// its freelist.
120 MacroArgs *deallocate();
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000121};
122
123} // end namespace clang
124
125#endif