1249259Sdim//===--- Arg.h - Parsed Argument Classes ------------------------*- C++ -*-===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim///
10249259Sdim/// \file
11249259Sdim/// \brief Defines the llvm::Arg class for parsed arguments.
12249259Sdim///
13249259Sdim//===----------------------------------------------------------------------===//
14249259Sdim
15249259Sdim#ifndef LLVM_OPTION_ARG_H
16249259Sdim#define LLVM_OPTION_ARG_H
17249259Sdim
18249259Sdim#include "llvm/ADT/SmallVector.h"
19249259Sdim#include "llvm/ADT/StringRef.h"
20249259Sdim#include "llvm/Option/Option.h"
21249259Sdim#include <string>
22249259Sdim
23249259Sdimnamespace llvm {
24249259Sdimnamespace opt {
25249259Sdimclass ArgList;
26249259Sdim
27249259Sdim/// \brief A concrete instance of a particular driver option.
28249259Sdim///
29249259Sdim/// The Arg class encodes just enough information to be able to
30249259Sdim/// derive the argument values efficiently. In addition, Arg
31249259Sdim/// instances have an intrusive double linked list which is used by
32249259Sdim/// ArgList to provide efficient iteration over all instances of a
33249259Sdim/// particular option.
34249259Sdimclass Arg {
35249259Sdim  Arg(const Arg &) LLVM_DELETED_FUNCTION;
36249259Sdim  void operator=(const Arg &) LLVM_DELETED_FUNCTION;
37249259Sdim
38249259Sdimprivate:
39249259Sdim  /// \brief The option this argument is an instance of.
40249259Sdim  const Option Opt;
41249259Sdim
42249259Sdim  /// \brief The argument this argument was derived from (during tool chain
43249259Sdim  /// argument translation), if any.
44249259Sdim  const Arg *BaseArg;
45249259Sdim
46249259Sdim  /// \brief How this instance of the option was spelled.
47249259Sdim  StringRef Spelling;
48249259Sdim
49249259Sdim  /// \brief The index at which this argument appears in the containing
50249259Sdim  /// ArgList.
51249259Sdim  unsigned Index;
52249259Sdim
53249259Sdim  /// \brief Was this argument used to effect compilation?
54249259Sdim  ///
55249259Sdim  /// This is used for generating "argument unused" diagnostics.
56249259Sdim  mutable unsigned Claimed : 1;
57249259Sdim
58249259Sdim  /// \brief Does this argument own its values?
59249259Sdim  mutable unsigned OwnsValues : 1;
60249259Sdim
61249259Sdim  /// \brief The argument values, as C strings.
62249259Sdim  SmallVector<const char *, 2> Values;
63249259Sdim
64249259Sdimpublic:
65249259Sdim  Arg(const Option Opt, StringRef Spelling, unsigned Index,
66249259Sdim      const Arg *BaseArg = 0);
67249259Sdim  Arg(const Option Opt, StringRef Spelling, unsigned Index,
68249259Sdim      const char *Value0, const Arg *BaseArg = 0);
69249259Sdim  Arg(const Option Opt, StringRef Spelling, unsigned Index,
70249259Sdim      const char *Value0, const char *Value1, const Arg *BaseArg = 0);
71249259Sdim  ~Arg();
72249259Sdim
73249259Sdim  const Option getOption() const { return Opt; }
74249259Sdim  StringRef getSpelling() const { return Spelling; }
75249259Sdim  unsigned getIndex() const { return Index; }
76249259Sdim
77249259Sdim  /// \brief Return the base argument which generated this arg.
78249259Sdim  ///
79249259Sdim  /// This is either the argument itself or the argument it was
80249259Sdim  /// derived from during tool chain specific argument translation.
81249259Sdim  const Arg &getBaseArg() const {
82249259Sdim    return BaseArg ? *BaseArg : *this;
83249259Sdim  }
84249259Sdim  void setBaseArg(const Arg *_BaseArg) {
85249259Sdim    BaseArg = _BaseArg;
86249259Sdim  }
87249259Sdim
88249259Sdim  bool getOwnsValues() const { return OwnsValues; }
89249259Sdim  void setOwnsValues(bool Value) const { OwnsValues = Value; }
90249259Sdim
91249259Sdim  bool isClaimed() const { return getBaseArg().Claimed; }
92249259Sdim
93249259Sdim  /// \brief Set the Arg claimed bit.
94249259Sdim  void claim() const { getBaseArg().Claimed = true; }
95249259Sdim
96249259Sdim  unsigned getNumValues() const { return Values.size(); }
97249259Sdim  const char *getValue(unsigned N = 0) const {
98249259Sdim    return Values[N];
99249259Sdim  }
100249259Sdim
101249259Sdim  SmallVectorImpl<const char*> &getValues() {
102249259Sdim    return Values;
103249259Sdim  }
104249259Sdim
105249259Sdim  bool containsValue(StringRef Value) const {
106249259Sdim    for (unsigned i = 0, e = getNumValues(); i != e; ++i)
107249259Sdim      if (Values[i] == Value)
108249259Sdim        return true;
109249259Sdim    return false;
110249259Sdim  }
111249259Sdim
112249259Sdim  /// \brief Append the argument onto the given array as strings.
113249259Sdim  void render(const ArgList &Args, ArgStringList &Output) const;
114249259Sdim
115249259Sdim  /// \brief Append the argument, render as an input, onto the given
116249259Sdim  /// array as strings.
117249259Sdim  ///
118249259Sdim  /// The distinction is that some options only render their values
119249259Sdim  /// when rendered as a input (e.g., Xlinker).
120249259Sdim  void renderAsInput(const ArgList &Args, ArgStringList &Output) const;
121249259Sdim
122249259Sdim  void dump() const;
123249259Sdim
124249259Sdim  /// \brief Return a formatted version of the argument and
125249259Sdim  /// its values, for debugging and diagnostics.
126249259Sdim  std::string getAsString(const ArgList &Args) const;
127249259Sdim};
128249259Sdim
129249259Sdim} // end namespace opt
130249259Sdim} // end namespace llvm
131249259Sdim
132249259Sdim#endif
133