1193326Sed//===--- Pragma.h - Pragma registration and handling ------------*- C++ -*-===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed// This file defines the PragmaHandler and PragmaTable interfaces.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#ifndef LLVM_CLANG_PRAGMA_H
15193326Sed#define LLVM_CLANG_PRAGMA_H
16193326Sed
17226890Sdim#include "clang/Basic/LLVM.h"
18210299Sed#include "llvm/ADT/StringMap.h"
19210299Sed#include "llvm/ADT/StringRef.h"
20193326Sed#include <cassert>
21193326Sed
22193326Sednamespace clang {
23193326Sed  class Preprocessor;
24193326Sed  class Token;
25193326Sed  class IdentifierInfo;
26193326Sed  class PragmaNamespace;
27193326Sed
28218893Sdim  /**
29245431Sdim   * \brief Describes how the pragma was introduced, e.g., with \#pragma,
30218893Sdim   * _Pragma, or __pragma.
31218893Sdim   */
32218893Sdim  enum PragmaIntroducerKind {
33218893Sdim    /**
34245431Sdim     * \brief The pragma was introduced via \#pragma.
35218893Sdim     */
36218893Sdim    PIK_HashPragma,
37218893Sdim
38218893Sdim    /**
39218893Sdim     * \brief The pragma was introduced via the C99 _Pragma(string-literal).
40218893Sdim     */
41218893Sdim    PIK__Pragma,
42218893Sdim
43218893Sdim    /**
44218893Sdim     * \brief The pragma was introduced via the Microsoft
45218893Sdim     * __pragma(token-string).
46218893Sdim     */
47218893Sdim    PIK___pragma
48218893Sdim  };
49218893Sdim
50193326Sed/// PragmaHandler - Instances of this interface defined to handle the various
51193326Sed/// pragmas that the language front-end uses.  Each handler optionally has a
52193326Sed/// name (e.g. "pack") and the HandlePragma method is invoked when a pragma with
53193326Sed/// that identifier is found.  If a handler does not match any of the declared
54193326Sed/// pragmas the handler with a null identifier is invoked, if it exists.
55193326Sed///
56193326Sed/// Note that the PragmaNamespace class can be used to subdivide pragmas, e.g.
57245431Sdim/// we treat "\#pragma STDC" and "\#pragma GCC" as namespaces that contain other
58193326Sed/// pragmas.
59193326Sedclass PragmaHandler {
60210299Sed  std::string Name;
61193326Sedpublic:
62226890Sdim  explicit PragmaHandler(StringRef name) : Name(name) {}
63210299Sed  PragmaHandler() {}
64193326Sed  virtual ~PragmaHandler();
65198092Srdivacky
66226890Sdim  StringRef getName() const { return Name; }
67218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
68218893Sdim                            Token &FirstToken) = 0;
69198092Srdivacky
70193326Sed  /// getIfNamespace - If this is a namespace, return it.  This is equivalent to
71193326Sed  /// using a dynamic_cast, but doesn't require RTTI.
72193326Sed  virtual PragmaNamespace *getIfNamespace() { return 0; }
73193326Sed};
74193326Sed
75210299Sed/// EmptyPragmaHandler - A pragma handler which takes no action, which can be
76210299Sed/// used to ignore particular pragmas.
77210299Sedclass EmptyPragmaHandler : public PragmaHandler {
78210299Sedpublic:
79210299Sed  EmptyPragmaHandler();
80210299Sed
81218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
82218893Sdim                            Token &FirstToken);
83210299Sed};
84210299Sed
85193326Sed/// PragmaNamespace - This PragmaHandler subdivides the namespace of pragmas,
86193326Sed/// allowing hierarchical pragmas to be defined.  Common examples of namespaces
87245431Sdim/// are "\#pragma GCC", "\#pragma STDC", and "\#pragma omp", but any namespaces
88245431Sdim/// may be (potentially recursively) defined.
89193326Sedclass PragmaNamespace : public PragmaHandler {
90210299Sed  /// Handlers - This is a map of the handlers in this namespace with their name
91210299Sed  /// as key.
92193326Sed  ///
93210299Sed  llvm::StringMap<PragmaHandler*> Handlers;
94193326Sedpublic:
95226890Sdim  explicit PragmaNamespace(StringRef Name) : PragmaHandler(Name) {}
96193326Sed  virtual ~PragmaNamespace();
97198092Srdivacky
98193326Sed  /// FindHandler - Check to see if there is already a handler for the
99210299Sed  /// specified name.  If not, return the handler for the null name if it
100193326Sed  /// exists, otherwise return null.  If IgnoreNull is true (the default) then
101193326Sed  /// the null handler isn't returned on failure to match.
102226890Sdim  PragmaHandler *FindHandler(StringRef Name,
103193326Sed                             bool IgnoreNull = true) const;
104198092Srdivacky
105193326Sed  /// AddPragma - Add a pragma to this namespace.
106193326Sed  ///
107210299Sed  void AddPragma(PragmaHandler *Handler);
108193326Sed
109193326Sed  /// RemovePragmaHandler - Remove the given handler from the
110193326Sed  /// namespace.
111193326Sed  void RemovePragmaHandler(PragmaHandler *Handler);
112193326Sed
113198092Srdivacky  bool IsEmpty() {
114198092Srdivacky    return Handlers.empty();
115193326Sed  }
116193326Sed
117218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
118218893Sdim                            Token &FirstToken);
119198092Srdivacky
120193326Sed  virtual PragmaNamespace *getIfNamespace() { return this; }
121193326Sed};
122193326Sed
123193326Sed
124193326Sed}  // end namespace clang
125193326Sed
126193326Sed#endif
127