blob: 69cb85b7fc34e81bafc9df671b8318ad3895fa67 [file] [log] [blame]
Daniel Dunbar6ac1e222009-03-04 17:10:42 +00001//===--- ArgList.cpp - Argument List Management -------------------------*-===//
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/ArgList.h"
11#include "clang/Driver/Arg.h"
Daniel Dunbar9358dc82009-03-04 22:40:08 +000012#include "clang/Driver/Option.h"
Daniel Dunbar6ac1e222009-03-04 17:10:42 +000013
14using namespace clang::driver;
15
Daniel Dunbarf3cad362009-03-25 04:13:45 +000016ArgList::ArgList(arglist_type &_Args) : Args(_Args) {
Daniel Dunbar6ac1e222009-03-04 17:10:42 +000017}
18
19ArgList::~ArgList() {
Daniel Dunbar6ac1e222009-03-04 17:10:42 +000020}
Daniel Dunbar9358dc82009-03-04 22:40:08 +000021
22void ArgList::append(Arg *A) {
Daniel Dunbar9358dc82009-03-04 22:40:08 +000023 Args.push_back(A);
24}
Daniel Dunbard8cadd42009-03-12 01:36:44 +000025
Daniel Dunbar8022fd42009-03-15 00:48:16 +000026Arg *ArgList::getLastArg(options::ID Id, bool Claim) const {
Daniel Dunbard8cadd42009-03-12 01:36:44 +000027 // FIXME: Make search efficient?
Daniel Dunbarfe2e04a2009-03-24 17:31:30 +000028 for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) {
Daniel Dunbar8022fd42009-03-15 00:48:16 +000029 if ((*it)->getOption().matches(Id)) {
30 if (Claim) (*it)->claim();
Daniel Dunbar0c562a22009-03-12 16:03:38 +000031 return *it;
Daniel Dunbar8022fd42009-03-15 00:48:16 +000032 }
33 }
Daniel Dunbard8cadd42009-03-12 01:36:44 +000034
Daniel Dunbar0c562a22009-03-12 16:03:38 +000035 return 0;
Daniel Dunbard8cadd42009-03-12 01:36:44 +000036}
Daniel Dunbarbca58cb2009-03-12 18:20:18 +000037
Daniel Dunbarcd4e1862009-03-17 18:51:42 +000038Arg *ArgList::getLastArg(options::ID Id0, options::ID Id1, bool Claim) const {
39 Arg *Res, *A0 = getLastArg(Id0, false), *A1 = getLastArg(Id1, false);
40
41 if (A0 && A1)
42 Res = A0->getIndex() > A1->getIndex() ? A0 : A1;
43 else
44 Res = A0 ? A0 : A1;
45
46 if (Claim && Res)
47 Res->claim();
48
49 return Res;
50}
51
Daniel Dunbar18a7f332009-03-18 09:29:36 +000052bool ArgList::hasFlag(options::ID Pos, options::ID Neg, bool Default) const {
53 Arg *PosA = getLastArg(Pos);
Daniel Dunbar776dbd82009-03-30 18:13:26 +000054 Arg *NegA = getLastArg(Neg);
Daniel Dunbar18a7f332009-03-18 09:29:36 +000055 if (PosA && NegA)
56 return NegA->getIndex() < PosA->getIndex();
57 if (PosA) return true;
58 if (NegA) return false;
59 return Default;
60}
61
Daniel Dunbar18a7f332009-03-18 09:29:36 +000062void ArgList::AddLastArg(ArgStringList &Output, options::ID Id) const {
63 if (Arg *A = getLastArg(Id)) {
64 A->claim();
65 A->render(*this, Output);
66 }
67}
68
69void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0) const {
70 // FIXME: Make fast.
71 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
72 const Arg *A = *it;
73 if (A->getOption().matches(Id0)) {
74 A->claim();
75 A->render(*this, Output);
76 }
77 }
78}
79
80void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
81 options::ID Id1) const {
82 // FIXME: Make fast.
83 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
84 const Arg *A = *it;
85 if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
86 A->claim();
87 A->render(*this, Output);
88 }
89 }
90}
91
92void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0,
93 options::ID Id1, options::ID Id2) const {
94 // FIXME: Make fast.
95 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
96 const Arg *A = *it;
97 if (A->getOption().matches(Id0) || A->getOption().matches(Id1) ||
98 A->getOption().matches(Id2)) {
99 A->claim();
100 A->render(*this, Output);
101 }
102 }
103}
104
105void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0) const {
106 // FIXME: Make fast.
107 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
108 const Arg *A = *it;
109 if (A->getOption().matches(Id0)) {
110 A->claim();
111 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
112 Output.push_back(A->getValue(*this, i));
113 }
114 }
115}
Daniel Dunbaree510312009-03-20 15:59:01 +0000116
117void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0,
118 options::ID Id1) const {
119 // FIXME: Make fast.
120 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
121 const Arg *A = *it;
122 if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
123 A->claim();
124 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
125 Output.push_back(A->getValue(*this, i));
126 }
127 }
128}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000129
Daniel Dunbar524b9fb2009-03-26 15:39:22 +0000130void ArgList::AddAllArgsTranslated(ArgStringList &Output, options::ID Id0,
131 const char *Translation) const {
132 // FIXME: Make fast.
133 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
134 const Arg *A = *it;
135 if (A->getOption().matches(Id0)) {
136 A->claim();
137 Output.push_back(Translation);
138 Output.push_back(A->getValue(*this, 0));
139 }
140 }
141}
142
Daniel Dunbar68fb4692009-04-03 20:51:31 +0000143void ArgList::ClaimAllArgs(options::ID Id0) const {
144 // FIXME: Make fast.
145 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
146 const Arg *A = *it;
147 if (A->getOption().matches(Id0))
148 A->claim();
149 }
150}
151
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000152//
153
154InputArgList::InputArgList(const char **ArgBegin, const char **ArgEnd)
155 : ArgList(ActualArgs), NumInputArgStrings(ArgEnd - ArgBegin)
156{
157 ArgStrings.append(ArgBegin, ArgEnd);
158}
159
160InputArgList::~InputArgList() {
161 // An InputArgList always owns its arguments.
162 for (iterator it = begin(), ie = end(); it != ie; ++it)
163 delete *it;
164}
165
166unsigned InputArgList::MakeIndex(const char *String0) const {
167 unsigned Index = ArgStrings.size();
168
169 // Tuck away so we have a reliable const char *.
170 SynthesizedStrings.push_back(String0);
171 ArgStrings.push_back(SynthesizedStrings.back().c_str());
172
173 return Index;
174}
175
176unsigned InputArgList::MakeIndex(const char *String0,
177 const char *String1) const {
178 unsigned Index0 = MakeIndex(String0);
179 unsigned Index1 = MakeIndex(String1);
180 assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
181 (void) Index1;
182 return Index0;
183}
184
185const char *InputArgList::MakeArgString(const char *Str) const {
186 return getArgString(MakeIndex(Str));
187}
188
189//
190
191DerivedArgList::DerivedArgList(InputArgList &_BaseArgs, bool _OnlyProxy)
192 : ArgList(_OnlyProxy ? _BaseArgs.getArgs() : ActualArgs),
193 BaseArgs(_BaseArgs), OnlyProxy(_OnlyProxy)
194{
195}
196
197DerivedArgList::~DerivedArgList() {
198 // We only own the arguments we explicitly synthesized.
199 for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
200 it != ie; ++it)
201 delete *it;
202}
203
204const char *DerivedArgList::MakeArgString(const char *Str) const {
205 return BaseArgs.MakeArgString(Str);
206}
207
Daniel Dunbar478edc22009-03-29 22:29:05 +0000208Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const {
209 return new FlagArg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg);
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000210}
211
Daniel Dunbar478edc22009-03-29 22:29:05 +0000212Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt,
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000213 const char *Value) const {
Daniel Dunbar478edc22009-03-29 22:29:05 +0000214 return new PositionalArg(Opt, BaseArgs.MakeIndex(Value), BaseArg);
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000215}
216
Daniel Dunbar478edc22009-03-29 22:29:05 +0000217Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000218 const char *Value) const {
Daniel Dunbar478edc22009-03-29 22:29:05 +0000219 return new SeparateArg(Opt, BaseArgs.MakeIndex(Opt->getName(), Value), 1,
220 BaseArg);
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000221}
222
Daniel Dunbar478edc22009-03-29 22:29:05 +0000223Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000224 const char *Value) const {
225 std::string Joined(Opt->getName());
226 Joined += Value;
Daniel Dunbar478edc22009-03-29 22:29:05 +0000227 return new JoinedArg(Opt, BaseArgs.MakeIndex(Joined.c_str()), BaseArg);
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000228}