blob: 52437e7772bdda078fcc7ba41e7891379fc5ab44 [file] [log] [blame]
Daniel Dunbarfb36cae2009-03-04 22:37:52 +00001//===--- Arg.cpp - Argument Implementations -----------------------------*-===//
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#include "clang/Driver/Arg.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000011#include "clang/Driver/ArgList.h"
Daniel Dunbarf781c412009-03-04 23:02:50 +000012#include "clang/Driver/Option.h"
13#include "llvm/Support/raw_ostream.h"
Daniel Dunbarfb36cae2009-03-04 22:37:52 +000014
15using namespace clang::driver;
16
17Arg::Arg(ArgClass _Kind, const Option *_Opt, unsigned _Index)
18 : Kind(_Kind),
19 Opt(_Opt),
20 Index(_Index)
21{
22}
23
24Arg::~Arg() { }
25
Daniel Dunbarf781c412009-03-04 23:02:50 +000026void Arg::dump() const {
27 llvm::errs() << "<";
28 switch (Kind) {
29 default:
30 assert(0 && "Invalid kind");
31#define P(N) case N: llvm::errs() << #N; break
Daniel Dunbar06482622009-03-05 06:38:47 +000032 P(FlagClass);
Daniel Dunbarf781c412009-03-04 23:02:50 +000033 P(PositionalClass);
34 P(JoinedClass);
35 P(SeparateClass);
36 P(CommaJoinedClass);
37 P(JoinedAndSeparateClass);
38#undef P
39 }
40
41 llvm::errs() << " Opt:";
42 Opt->dump();
43
44 llvm::errs() << " Index:" << Index;
45
Daniel Dunbarbbf842b2009-03-04 23:22:02 +000046 if (isa<CommaJoinedArg>(this) || isa<SeparateArg>(this))
47 llvm::errs() << " NumValues:" << getNumValues();
Daniel Dunbarf781c412009-03-04 23:02:50 +000048
49 llvm::errs() << ">\n";
50
51 llvm::errs().flush(); // FIXME
52}
53
Daniel Dunbar06482622009-03-05 06:38:47 +000054FlagArg::FlagArg(const Option *Opt, unsigned Index)
55 : Arg(FlagClass, Opt, Index) {
56}
57
58void FlagArg::render(const ArgList &Args, ArgStringList &Output) const {
59 assert(0 && "FIXME: Implement");
60}
61
62const char *FlagArg::getValue(const ArgList &Args, unsigned N) const {
63 assert(0 && "Invalid index.");
64 return 0;
65}
66
Daniel Dunbarfb36cae2009-03-04 22:37:52 +000067PositionalArg::PositionalArg(const Option *Opt, unsigned Index)
68 : Arg(PositionalClass, Opt, Index) {
69}
70
71void PositionalArg::render(const ArgList &Args, ArgStringList &Output) const {
72 assert(0 && "FIXME: Implement");
73}
74
75const char *PositionalArg::getValue(const ArgList &Args, unsigned N) const {
Daniel Dunbar06482622009-03-05 06:38:47 +000076 assert(N < getNumValues() && "Invalid index.");
77 return Args.getArgString(getIndex());
Daniel Dunbarfb36cae2009-03-04 22:37:52 +000078}
79
80JoinedArg::JoinedArg(const Option *Opt, unsigned Index)
81 : Arg(JoinedClass, Opt, Index) {
82}
83
84void JoinedArg::render(const ArgList &Args, ArgStringList &Output) const {
85 assert(0 && "FIXME: Implement");
86}
87
88const char *JoinedArg::getValue(const ArgList &Args, unsigned N) const {
Daniel Dunbar06482622009-03-05 06:38:47 +000089 assert(N < getNumValues() && "Invalid index.");
90 // FIXME: Avoid strlen.
91 return Args.getArgString(getIndex()) + strlen(getOption().getName());
Daniel Dunbarfb36cae2009-03-04 22:37:52 +000092}
93
94CommaJoinedArg::CommaJoinedArg(const Option *Opt, unsigned Index,
Daniel Dunbar06482622009-03-05 06:38:47 +000095 const char *Str)
96 : Arg(CommaJoinedClass, Opt, Index) {
97 const char *Prev = Str;
98 for (;; ++Str) {
99 char c = *Str;
100
101 if (!c) {
102 if (Prev != Str)
103 Values.push_back(std::string(Prev, Str));
104 break;
105 } else if (c == ',') {
106 if (Prev != Str)
107 Values.push_back(std::string(Prev, Str));
108 Prev = Str + 1;
109 }
110 }
Daniel Dunbarfb36cae2009-03-04 22:37:52 +0000111}
112
113void CommaJoinedArg::render(const ArgList &Args, ArgStringList &Output) const {
114 assert(0 && "FIXME: Implement");
115}
116
117const char *CommaJoinedArg::getValue(const ArgList &Args, unsigned N) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000118 assert(N < getNumValues() && "Invalid index.");
119 return Values[N].c_str();
Daniel Dunbarfb36cae2009-03-04 22:37:52 +0000120}
121
Daniel Dunbarf781c412009-03-04 23:02:50 +0000122SeparateArg::SeparateArg(const Option *Opt, unsigned Index, unsigned _NumValues)
123 : Arg(SeparateClass, Opt, Index), NumValues(_NumValues) {
124}
125
126void SeparateArg::render(const ArgList &Args, ArgStringList &Output) const {
127 assert(0 && "FIXME: Implement");
128}
129
130const char *SeparateArg::getValue(const ArgList &Args, unsigned N) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000131 assert(N < getNumValues() && "Invalid index.");
132 return Args.getArgString(getIndex() + 1 + N);
Daniel Dunbarf781c412009-03-04 23:02:50 +0000133}
134
Daniel Dunbarfb36cae2009-03-04 22:37:52 +0000135JoinedAndSeparateArg::JoinedAndSeparateArg(const Option *Opt, unsigned Index)
136 : Arg(JoinedAndSeparateClass, Opt, Index) {
137}
138
139void JoinedAndSeparateArg::render(const ArgList &Args,
140 ArgStringList &Output) const {
141 assert(0 && "FIXME: Implement");
142}
143
144const char *JoinedAndSeparateArg::getValue(const ArgList &Args,
145 unsigned N) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000146 assert(N < getNumValues() && "Invalid index.");
Daniel Dunbarfb36cae2009-03-04 22:37:52 +0000147 assert(0 && "FIXME: Implement");
148}