xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Driver/InputInfo.h (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1*7330f729Sjoerg //===--- InputInfo.h - Input Source & Type Information ----------*- C++ -*-===//
2*7330f729Sjoerg //
3*7330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*7330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7330f729Sjoerg //
7*7330f729Sjoerg //===----------------------------------------------------------------------===//
8*7330f729Sjoerg 
9*7330f729Sjoerg #ifndef LLVM_CLANG_LIB_DRIVER_INPUTINFO_H
10*7330f729Sjoerg #define LLVM_CLANG_LIB_DRIVER_INPUTINFO_H
11*7330f729Sjoerg 
12*7330f729Sjoerg #include "clang/Driver/Action.h"
13*7330f729Sjoerg #include "clang/Driver/Types.h"
14*7330f729Sjoerg #include "llvm/Option/Arg.h"
15*7330f729Sjoerg #include <cassert>
16*7330f729Sjoerg #include <string>
17*7330f729Sjoerg 
18*7330f729Sjoerg namespace clang {
19*7330f729Sjoerg namespace driver {
20*7330f729Sjoerg 
21*7330f729Sjoerg /// InputInfo - Wrapper for information about an input source.
22*7330f729Sjoerg class InputInfo {
23*7330f729Sjoerg   // FIXME: The distinction between filenames and inputarg here is
24*7330f729Sjoerg   // gross; we should probably drop the idea of a "linker
25*7330f729Sjoerg   // input". Doing so means tweaking pipelining to still create link
26*7330f729Sjoerg   // steps when it sees linker inputs (but not treat them as
27*7330f729Sjoerg   // arguments), and making sure that arguments get rendered
28*7330f729Sjoerg   // correctly.
29*7330f729Sjoerg   enum Class {
30*7330f729Sjoerg     Nothing,
31*7330f729Sjoerg     Filename,
32*7330f729Sjoerg     InputArg,
33*7330f729Sjoerg     Pipe
34*7330f729Sjoerg   };
35*7330f729Sjoerg 
36*7330f729Sjoerg   union {
37*7330f729Sjoerg     const char *Filename;
38*7330f729Sjoerg     const llvm::opt::Arg *InputArg;
39*7330f729Sjoerg   } Data;
40*7330f729Sjoerg   Class Kind;
41*7330f729Sjoerg   const Action* Act;
42*7330f729Sjoerg   types::ID Type;
43*7330f729Sjoerg   const char *BaseInput;
44*7330f729Sjoerg 
GetActionType(const Action * A)45*7330f729Sjoerg   static types::ID GetActionType(const Action *A) {
46*7330f729Sjoerg     return A != nullptr ? A->getType() : types::TY_Nothing;
47*7330f729Sjoerg   }
48*7330f729Sjoerg 
49*7330f729Sjoerg public:
InputInfo()50*7330f729Sjoerg   InputInfo() : InputInfo(nullptr, nullptr) {}
InputInfo(const Action * A,const char * _BaseInput)51*7330f729Sjoerg   InputInfo(const Action *A, const char *_BaseInput)
52*7330f729Sjoerg       : Kind(Nothing), Act(A), Type(GetActionType(A)), BaseInput(_BaseInput) {}
53*7330f729Sjoerg 
InputInfo(types::ID _Type,const char * _Filename,const char * _BaseInput)54*7330f729Sjoerg   InputInfo(types::ID _Type, const char *_Filename, const char *_BaseInput)
55*7330f729Sjoerg       : Kind(Filename), Act(nullptr), Type(_Type), BaseInput(_BaseInput) {
56*7330f729Sjoerg     Data.Filename = _Filename;
57*7330f729Sjoerg   }
InputInfo(const Action * A,const char * _Filename,const char * _BaseInput)58*7330f729Sjoerg   InputInfo(const Action *A, const char *_Filename, const char *_BaseInput)
59*7330f729Sjoerg       : Kind(Filename), Act(A), Type(GetActionType(A)), BaseInput(_BaseInput) {
60*7330f729Sjoerg     Data.Filename = _Filename;
61*7330f729Sjoerg   }
62*7330f729Sjoerg 
InputInfo(types::ID _Type,const llvm::opt::Arg * _InputArg,const char * _BaseInput)63*7330f729Sjoerg   InputInfo(types::ID _Type, const llvm::opt::Arg *_InputArg,
64*7330f729Sjoerg             const char *_BaseInput)
65*7330f729Sjoerg       : Kind(InputArg), Act(nullptr), Type(_Type), BaseInput(_BaseInput) {
66*7330f729Sjoerg     Data.InputArg = _InputArg;
67*7330f729Sjoerg   }
InputInfo(const Action * A,const llvm::opt::Arg * _InputArg,const char * _BaseInput)68*7330f729Sjoerg   InputInfo(const Action *A, const llvm::opt::Arg *_InputArg,
69*7330f729Sjoerg             const char *_BaseInput)
70*7330f729Sjoerg       : Kind(InputArg), Act(A), Type(GetActionType(A)), BaseInput(_BaseInput) {
71*7330f729Sjoerg     Data.InputArg = _InputArg;
72*7330f729Sjoerg   }
73*7330f729Sjoerg 
isNothing()74*7330f729Sjoerg   bool isNothing() const { return Kind == Nothing; }
isFilename()75*7330f729Sjoerg   bool isFilename() const { return Kind == Filename; }
isInputArg()76*7330f729Sjoerg   bool isInputArg() const { return Kind == InputArg; }
getType()77*7330f729Sjoerg   types::ID getType() const { return Type; }
getBaseInput()78*7330f729Sjoerg   const char *getBaseInput() const { return BaseInput; }
79*7330f729Sjoerg   /// The action for which this InputInfo was created.  May be null.
getAction()80*7330f729Sjoerg   const Action *getAction() const { return Act; }
setAction(const Action * A)81*7330f729Sjoerg   void setAction(const Action *A) { Act = A; }
82*7330f729Sjoerg 
getFilename()83*7330f729Sjoerg   const char *getFilename() const {
84*7330f729Sjoerg     assert(isFilename() && "Invalid accessor.");
85*7330f729Sjoerg     return Data.Filename;
86*7330f729Sjoerg   }
getInputArg()87*7330f729Sjoerg   const llvm::opt::Arg &getInputArg() const {
88*7330f729Sjoerg     assert(isInputArg() && "Invalid accessor.");
89*7330f729Sjoerg     return *Data.InputArg;
90*7330f729Sjoerg   }
91*7330f729Sjoerg 
92*7330f729Sjoerg   /// getAsString - Return a string name for this input, for
93*7330f729Sjoerg   /// debugging.
getAsString()94*7330f729Sjoerg   std::string getAsString() const {
95*7330f729Sjoerg     if (isFilename())
96*7330f729Sjoerg       return std::string("\"") + getFilename() + '"';
97*7330f729Sjoerg     else if (isInputArg())
98*7330f729Sjoerg       return "(input arg)";
99*7330f729Sjoerg     else
100*7330f729Sjoerg       return "(nothing)";
101*7330f729Sjoerg   }
102*7330f729Sjoerg };
103*7330f729Sjoerg 
104*7330f729Sjoerg } // end namespace driver
105*7330f729Sjoerg } // end namespace clang
106*7330f729Sjoerg 
107*7330f729Sjoerg #endif
108