1 //===--- Action.h - Abstract compilation steps ------------------*- 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 #ifndef LLVM_CLANG_DRIVER_ACTION_H
11 #define LLVM_CLANG_DRIVER_ACTION_H
12 
13 #include "clang/Driver/Types.h"
14 #include "clang/Driver/Util.h"
15 #include "llvm/ADT/SmallVector.h"
16 
17 namespace llvm {
18 namespace opt {
19   class Arg;
20 }
21 }
22 
23 namespace clang {
24 namespace driver {
25 
26 /// Action - Represent an abstract compilation step to perform.
27 ///
28 /// An action represents an edge in the compilation graph; typically
29 /// it is a job to transform an input using some tool.
30 ///
31 /// The current driver is hard wired to expect actions which produce a
32 /// single primary output, at least in terms of controlling the
33 /// compilation. Actions can produce auxiliary files, but can only
34 /// produce a single output to feed into subsequent actions.
35 class Action {
36 public:
37   typedef ActionList::size_type size_type;
38   typedef ActionList::iterator iterator;
39   typedef ActionList::const_iterator const_iterator;
40 
41   enum ActionClass {
42     InputClass = 0,
43     BindArchClass,
44     CudaDeviceClass,
45     CudaHostClass,
46     PreprocessJobClass,
47     PrecompileJobClass,
48     AnalyzeJobClass,
49     MigrateJobClass,
50     CompileJobClass,
51     BackendJobClass,
52     AssembleJobClass,
53     LinkJobClass,
54     LipoJobClass,
55     DsymutilJobClass,
56     VerifyDebugInfoJobClass,
57     VerifyPCHJobClass,
58 
59     JobClassFirst=PreprocessJobClass,
60     JobClassLast=VerifyPCHJobClass
61   };
62 
63   static const char *getClassName(ActionClass AC);
64 
65 private:
66   ActionClass Kind;
67 
68   /// The output type of this action.
69   types::ID Type;
70 
71   ActionList Inputs;
72 
73   unsigned OwnsInputs : 1;
74 
75 protected:
Action(ActionClass Kind,types::ID Type)76   Action(ActionClass Kind, types::ID Type)
77     : Kind(Kind), Type(Type), OwnsInputs(true)  {}
Action(ActionClass Kind,std::unique_ptr<Action> Input,types::ID Type)78   Action(ActionClass Kind, std::unique_ptr<Action> Input, types::ID Type)
79       : Kind(Kind), Type(Type), Inputs(1, Input.release()), OwnsInputs(true) {
80   }
Action(ActionClass Kind,std::unique_ptr<Action> Input)81   Action(ActionClass Kind, std::unique_ptr<Action> Input)
82       : Kind(Kind), Type(Input->getType()), Inputs(1, Input.release()),
83         OwnsInputs(true) {}
Action(ActionClass Kind,const ActionList & Inputs,types::ID Type)84   Action(ActionClass Kind, const ActionList &Inputs, types::ID Type)
85     : Kind(Kind), Type(Type), Inputs(Inputs), OwnsInputs(true) {}
86 public:
87   virtual ~Action();
88 
getClassName()89   const char *getClassName() const { return Action::getClassName(getKind()); }
90 
getOwnsInputs()91   bool getOwnsInputs() { return OwnsInputs; }
setOwnsInputs(bool Value)92   void setOwnsInputs(bool Value) { OwnsInputs = Value; }
93 
getKind()94   ActionClass getKind() const { return Kind; }
getType()95   types::ID getType() const { return Type; }
96 
getInputs()97   ActionList &getInputs() { return Inputs; }
getInputs()98   const ActionList &getInputs() const { return Inputs; }
99 
size()100   size_type size() const { return Inputs.size(); }
101 
begin()102   iterator begin() { return Inputs.begin(); }
end()103   iterator end() { return Inputs.end(); }
begin()104   const_iterator begin() const { return Inputs.begin(); }
end()105   const_iterator end() const { return Inputs.end(); }
106 };
107 
108 class InputAction : public Action {
109   virtual void anchor();
110   const llvm::opt::Arg &Input;
111 
112 public:
113   InputAction(const llvm::opt::Arg &Input, types::ID Type);
114 
getInputArg()115   const llvm::opt::Arg &getInputArg() const { return Input; }
116 
classof(const Action * A)117   static bool classof(const Action *A) {
118     return A->getKind() == InputClass;
119   }
120 };
121 
122 class BindArchAction : public Action {
123   virtual void anchor();
124   /// The architecture to bind, or 0 if the default architecture
125   /// should be bound.
126   const char *ArchName;
127 
128 public:
129   BindArchAction(std::unique_ptr<Action> Input, const char *ArchName);
130 
getArchName()131   const char *getArchName() const { return ArchName; }
132 
classof(const Action * A)133   static bool classof(const Action *A) {
134     return A->getKind() == BindArchClass;
135   }
136 };
137 
138 class CudaDeviceAction : public Action {
139   virtual void anchor();
140   /// GPU architecture to bind -- e.g 'sm_35'.
141   const char *GpuArchName;
142   /// True when action results are not consumed by the host action (e.g when
143   /// -fsyntax-only or --cuda-device-only options are used).
144   bool AtTopLevel;
145 
146 public:
147   CudaDeviceAction(std::unique_ptr<Action> Input, const char *ArchName,
148                    bool AtTopLevel);
149 
getGpuArchName()150   const char *getGpuArchName() const { return GpuArchName; }
isAtTopLevel()151   bool isAtTopLevel() const { return AtTopLevel; }
152 
classof(const Action * A)153   static bool classof(const Action *A) {
154     return A->getKind() == CudaDeviceClass;
155   }
156 };
157 
158 class CudaHostAction : public Action {
159   virtual void anchor();
160   ActionList DeviceActions;
161 
162 public:
163   CudaHostAction(std::unique_ptr<Action> Input,
164                  const ActionList &DeviceActions);
165   ~CudaHostAction() override;
166 
getDeviceActions()167   const ActionList &getDeviceActions() const { return DeviceActions; }
168 
classof(const Action * A)169   static bool classof(const Action *A) { return A->getKind() == CudaHostClass; }
170 };
171 
172 class JobAction : public Action {
173   virtual void anchor();
174 protected:
175   JobAction(ActionClass Kind, std::unique_ptr<Action> Input, types::ID Type);
176   JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type);
177 
178 public:
classof(const Action * A)179   static bool classof(const Action *A) {
180     return (A->getKind() >= JobClassFirst &&
181             A->getKind() <= JobClassLast);
182   }
183 };
184 
185 class PreprocessJobAction : public JobAction {
186   void anchor() override;
187 public:
188   PreprocessJobAction(std::unique_ptr<Action> Input, types::ID OutputType);
189 
classof(const Action * A)190   static bool classof(const Action *A) {
191     return A->getKind() == PreprocessJobClass;
192   }
193 };
194 
195 class PrecompileJobAction : public JobAction {
196   void anchor() override;
197 public:
198   PrecompileJobAction(std::unique_ptr<Action> Input, types::ID OutputType);
199 
classof(const Action * A)200   static bool classof(const Action *A) {
201     return A->getKind() == PrecompileJobClass;
202   }
203 };
204 
205 class AnalyzeJobAction : public JobAction {
206   void anchor() override;
207 public:
208   AnalyzeJobAction(std::unique_ptr<Action> Input, types::ID OutputType);
209 
classof(const Action * A)210   static bool classof(const Action *A) {
211     return A->getKind() == AnalyzeJobClass;
212   }
213 };
214 
215 class MigrateJobAction : public JobAction {
216   void anchor() override;
217 public:
218   MigrateJobAction(std::unique_ptr<Action> Input, types::ID OutputType);
219 
classof(const Action * A)220   static bool classof(const Action *A) {
221     return A->getKind() == MigrateJobClass;
222   }
223 };
224 
225 class CompileJobAction : public JobAction {
226   void anchor() override;
227 public:
228   CompileJobAction(std::unique_ptr<Action> Input, types::ID OutputType);
229 
classof(const Action * A)230   static bool classof(const Action *A) {
231     return A->getKind() == CompileJobClass;
232   }
233 };
234 
235 class BackendJobAction : public JobAction {
236   void anchor() override;
237 public:
238   BackendJobAction(std::unique_ptr<Action> Input, types::ID OutputType);
239 
classof(const Action * A)240   static bool classof(const Action *A) {
241     return A->getKind() == BackendJobClass;
242   }
243 };
244 
245 class AssembleJobAction : public JobAction {
246   void anchor() override;
247 public:
248   AssembleJobAction(std::unique_ptr<Action> Input, types::ID OutputType);
249 
classof(const Action * A)250   static bool classof(const Action *A) {
251     return A->getKind() == AssembleJobClass;
252   }
253 };
254 
255 class LinkJobAction : public JobAction {
256   void anchor() override;
257 public:
258   LinkJobAction(ActionList &Inputs, types::ID Type);
259 
classof(const Action * A)260   static bool classof(const Action *A) {
261     return A->getKind() == LinkJobClass;
262   }
263 };
264 
265 class LipoJobAction : public JobAction {
266   void anchor() override;
267 public:
268   LipoJobAction(ActionList &Inputs, types::ID Type);
269 
classof(const Action * A)270   static bool classof(const Action *A) {
271     return A->getKind() == LipoJobClass;
272   }
273 };
274 
275 class DsymutilJobAction : public JobAction {
276   void anchor() override;
277 public:
278   DsymutilJobAction(ActionList &Inputs, types::ID Type);
279 
classof(const Action * A)280   static bool classof(const Action *A) {
281     return A->getKind() == DsymutilJobClass;
282   }
283 };
284 
285 class VerifyJobAction : public JobAction {
286   void anchor() override;
287 public:
288   VerifyJobAction(ActionClass Kind, std::unique_ptr<Action> Input,
289                   types::ID Type);
classof(const Action * A)290   static bool classof(const Action *A) {
291     return A->getKind() == VerifyDebugInfoJobClass ||
292            A->getKind() == VerifyPCHJobClass;
293   }
294 };
295 
296 class VerifyDebugInfoJobAction : public VerifyJobAction {
297   void anchor() override;
298 public:
299   VerifyDebugInfoJobAction(std::unique_ptr<Action> Input, types::ID Type);
classof(const Action * A)300   static bool classof(const Action *A) {
301     return A->getKind() == VerifyDebugInfoJobClass;
302   }
303 };
304 
305 class VerifyPCHJobAction : public VerifyJobAction {
306   void anchor() override;
307 public:
308   VerifyPCHJobAction(std::unique_ptr<Action> Input, types::ID Type);
classof(const Action * A)309   static bool classof(const Action *A) {
310     return A->getKind() == VerifyPCHJobClass;
311   }
312 };
313 
314 } // end namespace driver
315 } // end namespace clang
316 
317 #endif
318