blob: 97dca8c6aa3b0613827f26a429a714e9169a5172 [file] [log] [blame]
Mikhail Glushenkovfb37f392008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000018#include "llvm/ADT/StringMap.h"
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000019#include "llvm/ADT/StringSet.h"
Mikhail Glushenkovaa1a3732010-08-13 06:02:45 +000020#include "llvm/Support/CommandLine.h"
21#include "llvm/System/Path.h"
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000022
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000023#include <algorithm>
24#include <cassert>
25#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000026#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000027#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000028#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000029
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000030using namespace llvm;
31
Mikhail Glushenkovaa1a3732010-08-13 06:02:45 +000032extern cl::opt<std::string> InputFilename;
33
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000034namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000035
36//===----------------------------------------------------------------------===//
37/// Typedefs
38
39typedef std::vector<Record*> RecordVector;
40typedef std::vector<std::string> StrVector;
41
42//===----------------------------------------------------------------------===//
43/// Constants
44
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000045// Indentation.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000046const unsigned TabWidth = 4;
47const unsigned Indent1 = TabWidth*1;
48const unsigned Indent2 = TabWidth*2;
49const unsigned Indent3 = TabWidth*3;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +000050const unsigned Indent4 = TabWidth*4;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000051
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000052// Default help string.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000053const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000054
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000055// Name for the "sink" option.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000056const char * const SinkOptionName = "AutoGeneratedSinkOption";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000057
58//===----------------------------------------------------------------------===//
59/// Helper functions
60
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000061/// Id - An 'identity' function object.
62struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000063 template<typename T0>
64 void operator()(const T0&) const {
65 }
66 template<typename T0, typename T1>
67 void operator()(const T0&, const T1&) const {
68 }
69 template<typename T0, typename T1, typename T2>
70 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000071 }
72};
73
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000074int InitPtrToInt(const Init* ptr) {
75 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000076 return val.getValue();
77}
78
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000079const std::string& InitPtrToString(const Init* ptr) {
80 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
81 return val.getValue();
82}
83
84const ListInit& InitPtrToList(const Init* ptr) {
85 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
86 return val;
87}
88
89const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000090 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000091 return val;
92}
93
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000094const std::string GetOperatorName(const DagInit& D) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +000095 return D.getOperator()->getAsString();
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000096}
97
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000098/// CheckBooleanConstant - Check that the provided value is a boolean constant.
99void CheckBooleanConstant(const Init* I) {
100 const DefInit& val = dynamic_cast<const DefInit&>(*I);
101 const std::string& str = val.getAsString();
102
103 if (str != "true" && str != "false") {
104 throw "Incorrect boolean value: '" + str +
105 "': must be either 'true' or 'false'";
106 }
107}
108
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000109// CheckNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000110// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000111void CheckNumberOfArguments (const DagInit& d, unsigned minArgs) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000112 if (d.getNumArgs() < minArgs)
113 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000114}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000115
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000116// IsDagEmpty - is this DAG marked with an empty marker?
117bool IsDagEmpty (const DagInit& d) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000118 return GetOperatorName(d) == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000119}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000120
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000121// EscapeVariableName - Escape commas and other symbols not allowed
122// in the C++ variable names. Makes it possible to use options named
123// like "Wa," (useful for prefix options).
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000124std::string EscapeVariableName (const std::string& Var) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000125 std::string ret;
126 for (unsigned i = 0; i != Var.size(); ++i) {
127 char cur_char = Var[i];
128 if (cur_char == ',') {
129 ret += "_comma_";
130 }
131 else if (cur_char == '+') {
132 ret += "_plus_";
133 }
134 else if (cur_char == '-') {
135 ret += "_dash_";
136 }
137 else {
138 ret.push_back(cur_char);
139 }
140 }
141 return ret;
142}
143
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000144/// EscapeQuotes - Replace '"' with '\"'.
145std::string EscapeQuotes (const std::string& Var) {
146 std::string ret;
147 for (unsigned i = 0; i != Var.size(); ++i) {
148 char cur_char = Var[i];
149 if (cur_char == '"') {
150 ret += "\\\"";
151 }
152 else {
153 ret.push_back(cur_char);
154 }
155 }
156 return ret;
157}
158
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000159/// OneOf - Does the input string contain this character?
160bool OneOf(const char* lst, char c) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000161 while (*lst) {
162 if (*lst++ == c)
163 return true;
164 }
165 return false;
166}
167
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000168template <class I, class S>
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000169void CheckedIncrement(I& P, I E, S ErrorString) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000170 ++P;
171 if (P == E)
172 throw ErrorString;
173}
174
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000175// apply is needed because C++'s syntax doesn't let us construct a function
176// object and call it in the same statement.
177template<typename F, typename T0>
178void apply(F Fun, T0& Arg0) {
179 return Fun(Arg0);
180}
181
182template<typename F, typename T0, typename T1>
183void apply(F Fun, T0& Arg0, T1& Arg1) {
184 return Fun(Arg0, Arg1);
185}
186
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000187//===----------------------------------------------------------------------===//
188/// Back-end specific code
189
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000190
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000191/// OptionType - One of six different option types. See the
192/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000193namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000194
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000195 enum OptionType { Alias, Switch, SwitchList,
196 Parameter, ParameterList, Prefix, PrefixList };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000197
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000198 bool IsAlias(OptionType t) {
199 return (t == Alias);
200 }
201
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000202 bool IsList (OptionType t) {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000203 return (t == SwitchList || t == ParameterList || t == PrefixList);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000204 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000205
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000206 bool IsSwitch (OptionType t) {
207 return (t == Switch);
208 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000209
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000210 bool IsSwitchList (OptionType t) {
211 return (t == SwitchList);
212 }
213
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000214 bool IsParameter (OptionType t) {
215 return (t == Parameter || t == Prefix);
216 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000217
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000218}
219
220OptionType::OptionType stringToOptionType(const std::string& T) {
221 if (T == "alias_option")
222 return OptionType::Alias;
223 else if (T == "switch_option")
224 return OptionType::Switch;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000225 else if (T == "switch_list_option")
226 return OptionType::SwitchList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000227 else if (T == "parameter_option")
228 return OptionType::Parameter;
229 else if (T == "parameter_list_option")
230 return OptionType::ParameterList;
231 else if (T == "prefix_option")
232 return OptionType::Prefix;
233 else if (T == "prefix_list_option")
234 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000235 else
236 throw "Unknown option type: " + T + '!';
237}
238
239namespace OptionDescriptionFlags {
240 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000241 ReallyHidden = 0x4, Extern = 0x8,
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000242 OneOrMore = 0x10, Optional = 0x20,
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000243 CommaSeparated = 0x40, ForwardNotSplit = 0x80,
244 ZeroOrMore = 0x100 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000245}
246
247/// OptionDescription - Represents data contained in a single
248/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000249struct OptionDescription {
250 OptionType::OptionType Type;
251 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000252 unsigned Flags;
253 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000254 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000255 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000256
257 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000258 const std::string& n = "",
259 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000260 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000261 {}
262
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000263 /// GenTypeDeclaration - Returns the C++ variable type of this
264 /// option.
265 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000266
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000267 /// GenVariableName - Returns the variable name used in the
268 /// generated C++ code.
269 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000270
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000271 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000272 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000273
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000274 /// CheckConsistency - Check that the flags are consistent.
275 void CheckConsistency() const;
276
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000277 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000278
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000279 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000280
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000281 bool isMultiVal() const;
282
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000283 bool isCommaSeparated() const;
284 void setCommaSeparated();
285
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000286 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000287 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000288
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000289 bool isForwardNotSplit() const;
290 void setForwardNotSplit();
291
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000292 bool isRequired() const;
293 void setRequired();
294
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000295 bool isOneOrMore() const;
296 void setOneOrMore();
297
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000298 bool isZeroOrMore() const;
299 void setZeroOrMore();
300
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000301 bool isOptional() const;
302 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000303
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000304 bool isHidden() const;
305 void setHidden();
306
307 bool isReallyHidden() const;
308 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000309
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000310 bool isSwitch() const
311 { return OptionType::IsSwitch(this->Type); }
312
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000313 bool isSwitchList() const
314 { return OptionType::IsSwitchList(this->Type); }
315
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000316 bool isParameter() const
317 { return OptionType::IsParameter(this->Type); }
318
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000319 bool isList() const
320 { return OptionType::IsList(this->Type); }
321
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000322 bool isParameterList() const
323 { return (OptionType::IsList(this->Type)
324 && !OptionType::IsSwitchList(this->Type)); }
325
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000326};
327
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000328void OptionDescription::CheckConsistency() const {
329 unsigned i = 0;
330
331 i += this->isRequired();
332 i += this->isOptional();
333 i += this->isOneOrMore();
334 i += this->isZeroOrMore();
335
336 if (i > 1) {
337 throw "Only one of (required), (optional), (one_or_more) or "
338 "(zero_or_more) properties is allowed!";
339 }
340}
341
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000342void OptionDescription::Merge (const OptionDescription& other)
343{
344 if (other.Type != Type)
345 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000346
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000347 if (Help == other.Help || Help == DefaultHelpString)
348 Help = other.Help;
349 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000350 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000351 " defined for option " + Name + "\n";
352 }
353
354 Flags |= other.Flags;
355}
356
357bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000358 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000359}
360
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000361bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000362 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000363}
364
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000365bool OptionDescription::isCommaSeparated() const {
366 return Flags & OptionDescriptionFlags::CommaSeparated;
367}
368void OptionDescription::setCommaSeparated() {
369 Flags |= OptionDescriptionFlags::CommaSeparated;
370}
371
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000372bool OptionDescription::isForwardNotSplit() const {
373 return Flags & OptionDescriptionFlags::ForwardNotSplit;
374}
375void OptionDescription::setForwardNotSplit() {
376 Flags |= OptionDescriptionFlags::ForwardNotSplit;
377}
378
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000379bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000380 return Flags & OptionDescriptionFlags::Extern;
381}
382void OptionDescription::setExtern() {
383 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000384}
385
386bool OptionDescription::isRequired() const {
387 return Flags & OptionDescriptionFlags::Required;
388}
389void OptionDescription::setRequired() {
390 Flags |= OptionDescriptionFlags::Required;
391}
392
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000393bool OptionDescription::isOneOrMore() const {
394 return Flags & OptionDescriptionFlags::OneOrMore;
395}
396void OptionDescription::setOneOrMore() {
397 Flags |= OptionDescriptionFlags::OneOrMore;
398}
399
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000400bool OptionDescription::isZeroOrMore() const {
401 return Flags & OptionDescriptionFlags::ZeroOrMore;
402}
403void OptionDescription::setZeroOrMore() {
404 Flags |= OptionDescriptionFlags::ZeroOrMore;
405}
406
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000407bool OptionDescription::isOptional() const {
408 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000409}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000410void OptionDescription::setOptional() {
411 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000412}
413
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000414bool OptionDescription::isHidden() const {
415 return Flags & OptionDescriptionFlags::Hidden;
416}
417void OptionDescription::setHidden() {
418 Flags |= OptionDescriptionFlags::Hidden;
419}
420
421bool OptionDescription::isReallyHidden() const {
422 return Flags & OptionDescriptionFlags::ReallyHidden;
423}
424void OptionDescription::setReallyHidden() {
425 Flags |= OptionDescriptionFlags::ReallyHidden;
426}
427
428const char* OptionDescription::GenTypeDeclaration() const {
429 switch (Type) {
430 case OptionType::Alias:
431 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000432 case OptionType::PrefixList:
433 case OptionType::ParameterList:
434 return "cl::list<std::string>";
435 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000436 return "cl::opt<bool>";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000437 case OptionType::SwitchList:
438 return "cl::list<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000439 case OptionType::Parameter:
440 case OptionType::Prefix:
441 default:
442 return "cl::opt<std::string>";
443 }
444}
445
446std::string OptionDescription::GenVariableName() const {
447 const std::string& EscapedName = EscapeVariableName(Name);
448 switch (Type) {
449 case OptionType::Alias:
450 return "AutoGeneratedAlias_" + EscapedName;
451 case OptionType::PrefixList:
452 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000453 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000454 case OptionType::Switch:
455 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000456 case OptionType::SwitchList:
457 return "AutoGeneratedSwitchList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000458 case OptionType::Prefix:
459 case OptionType::Parameter:
460 default:
461 return "AutoGeneratedParameter_" + EscapedName;
462 }
463}
464
465/// OptionDescriptions - An OptionDescription array plus some helper
466/// functions.
467class OptionDescriptions {
468 typedef StringMap<OptionDescription> container_type;
469
470 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000471 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000472
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000473public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000474 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000475 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000476
477 // Wrappers for FindOption that throw an exception in case the option has a
478 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000479 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000480 const OptionDescription& FindParameter(const std::string& OptName) const;
481 const OptionDescription& FindList(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000482 const OptionDescription& FindParameterList(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000483 const OptionDescription&
484 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000485 const OptionDescription&
486 FindParameterListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000487
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000488 /// insertDescription - Insert new OptionDescription into
489 /// OptionDescriptions list
490 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000491
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000492 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000493 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000494 const_iterator begin() const { return Descriptions.begin(); }
495 const_iterator end() const { return Descriptions.end(); }
496};
497
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000498const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000499OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000500 const_iterator I = Descriptions.find(OptName);
501 if (I != Descriptions.end())
502 return I->second;
503 else
504 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000505}
506
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000507const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000508OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000509 const OptionDescription& OptDesc = this->FindOption(OptName);
510 if (!OptDesc.isSwitch())
511 throw OptName + ": incorrect option type - should be a switch!";
512 return OptDesc;
513}
514
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000515const OptionDescription&
516OptionDescriptions::FindList(const std::string& OptName) const {
517 const OptionDescription& OptDesc = this->FindOption(OptName);
518 if (!OptDesc.isList())
519 throw OptName + ": incorrect option type - should be a list!";
520 return OptDesc;
521}
522
523const OptionDescription&
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000524OptionDescriptions::FindParameterList(const std::string& OptName) const {
525 const OptionDescription& OptDesc = this->FindOption(OptName);
526 if (!OptDesc.isList() || OptDesc.isSwitchList())
527 throw OptName + ": incorrect option type - should be a parameter list!";
528 return OptDesc;
529}
530
531const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000532OptionDescriptions::FindParameter(const std::string& OptName) const {
533 const OptionDescription& OptDesc = this->FindOption(OptName);
534 if (!OptDesc.isParameter())
535 throw OptName + ": incorrect option type - should be a parameter!";
536 return OptDesc;
537}
538
539const OptionDescription&
540OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
541 const OptionDescription& OptDesc = this->FindOption(OptName);
542 if (!OptDesc.isList() && !OptDesc.isParameter())
543 throw OptName
544 + ": incorrect option type - should be a list or parameter!";
545 return OptDesc;
546}
547
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000548const OptionDescription&
549OptionDescriptions::FindParameterListOrParameter
550(const std::string& OptName) const {
551 const OptionDescription& OptDesc = this->FindOption(OptName);
552 if ((!OptDesc.isList() && !OptDesc.isParameter()) || OptDesc.isSwitchList())
553 throw OptName
554 + ": incorrect option type - should be a parameter list or parameter!";
555 return OptDesc;
556}
557
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000558void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000559 container_type::iterator I = Descriptions.find(o.Name);
560 if (I != Descriptions.end()) {
561 OptionDescription& D = I->second;
562 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000563 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000564 else {
565 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000566 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000567}
568
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000569/// HandlerTable - A base class for function objects implemented as
570/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000571template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000572class HandlerTable {
573protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000574 // Implementation details.
575
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000576 /// HandlerMap - A map from property names to property handlers
577 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000578
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000579 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000580 static bool staticMembersInitialized_;
581
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000582public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000583
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000584 Handler GetHandler (const std::string& HandlerName) const {
585 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000586
587 if (method != Handlers_.end()) {
588 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000589 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000590 }
591 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000592 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000593 }
594 }
595
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000596 void AddHandler(const char* Property, Handler H) {
597 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000598 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000599
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000600};
601
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000602template <class Handler, class FunctionObject>
603Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
604 const std::string& HandlerName = GetOperatorName(Dag);
605 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000606}
607
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000608template <class FunctionObject>
609void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
610 typedef void (FunctionObject::*Handler) (const DagInit&);
611
612 const DagInit& Dag = InitPtrToDag(I);
613 Handler h = GetHandler<Handler>(Obj, Dag);
614
615 ((Obj)->*(h))(Dag);
616}
617
618template <class FunctionObject>
619void InvokeDagInitHandler(const FunctionObject* const Obj,
620 const Init* I, unsigned IndentLevel, raw_ostream& O)
621{
622 typedef void (FunctionObject::*Handler)
623 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
624
625 const DagInit& Dag = InitPtrToDag(I);
626 Handler h = GetHandler<Handler>(Obj, Dag);
627
628 ((Obj)->*(h))(Dag, IndentLevel, O);
629}
630
631
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000632template <typename H>
633typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
634
635template <typename H>
636bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000637
638
639/// CollectOptionProperties - Function object for iterating over an
640/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000641class CollectOptionProperties;
642typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000643(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000644
645class CollectOptionProperties
646: public HandlerTable<CollectOptionPropertiesHandler>
647{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000648private:
649
650 /// optDescs_ - OptionDescriptions table. This is where the
651 /// information is stored.
652 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000653
654public:
655
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000656 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000657 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000658 {
659 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000660 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000661 AddHandler("help", &CollectOptionProperties::onHelp);
662 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000663 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000664 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
665 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000666 AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000667 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
668 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000669 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000670 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000671 AddHandler("forward_not_split",
672 &CollectOptionProperties::onForwardNotSplit);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000673
674 staticMembersInitialized_ = true;
675 }
676 }
677
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000678 /// operator() - Just forwards to the corresponding property
679 /// handler.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000680 void operator() (Init* I) {
681 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000682 }
683
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000684private:
685
686 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000687 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000688
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000689 void onExtern (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000690 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000691 optDesc_.setExtern();
692 }
693
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000694 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000695 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000696 optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000697 }
698
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000699 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000700 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000701 optDesc_.setHidden();
702 }
703
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000704 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000705 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000706 optDesc_.setReallyHidden();
707 }
708
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000709 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000710 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000711 if (!optDesc_.isParameterList())
712 throw "'comma_separated' is valid only on parameter list options!";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000713 optDesc_.setCommaSeparated();
714 }
715
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000716 void onForwardNotSplit (const DagInit& d) {
717 CheckNumberOfArguments(d, 0);
718 if (!optDesc_.isParameter())
719 throw "'forward_not_split' is valid only for parameter options!";
720 optDesc_.setForwardNotSplit();
721 }
722
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000723 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000724 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000725
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000726 optDesc_.setRequired();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000727 optDesc_.CheckConsistency();
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000728 }
729
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000730 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000731 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000732 Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000733 const std::string& str = i->getAsString();
734
735 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
736 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
737
738 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000739 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000740
741 optDesc_.InitVal = i;
742 }
743
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000744 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000745 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000746
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000747 optDesc_.setOneOrMore();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000748 optDesc_.CheckConsistency();
749 }
750
751 void onZeroOrMore (const DagInit& d) {
752 CheckNumberOfArguments(d, 0);
753
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000754 if (optDesc_.isList())
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000755 llvm::errs() << "Warning: specifying the 'zero_or_more' property "
756 "on a list option has no effect.\n";
757
758 optDesc_.setZeroOrMore();
759 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000760 }
761
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000762 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000763 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000764
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000765 if (!optDesc_.isList())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000766 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000767 "on a non-list option has no effect.\n";
768
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000769 optDesc_.setOptional();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000770 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000771 }
772
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000773 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000774 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000775 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000776 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000777 throw "Error in the 'multi_val' property: "
778 "the value must be greater than 1!";
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000779 if (!optDesc_.isParameterList())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000780 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000781 optDesc_.MultiVal = val;
782 }
783
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000784};
785
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000786/// AddOption - A function object that is applied to every option
787/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000788class AddOption {
789private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000790 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000791
792public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000793 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000794 {}
795
796 void operator()(const Init* i) {
797 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000798 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000799
800 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000801 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000802 const std::string& Name = InitPtrToString(d.getArg(0));
803
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000804 OptionDescription OD(Type, Name);
805
806 if (!OD.isExtern())
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000807 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000808
809 if (OD.isAlias()) {
810 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000811 OD.Help = InitPtrToString(d.getArg(1));
812 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000813 else if (!OD.isExtern()) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000814 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000815 }
816 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000817 }
818
819private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000820 /// processOptionProperties - Go through the list of option
821 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000822 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000823 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000824 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000825 // Skip the first argument: it's always the option name.
826 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000827 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000828 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000829
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000830};
831
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000832/// CollectOptionDescriptions - Collects option properties from all
833/// OptionLists.
834void CollectOptionDescriptions (RecordVector::const_iterator B,
835 RecordVector::const_iterator E,
836 OptionDescriptions& OptDescs)
837{
838 // For every OptionList:
839 for (; B!=E; ++B) {
840 RecordVector::value_type T = *B;
841 // Throws an exception if the value does not exist.
842 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000843
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000844 // For every option description in this list:
845 // collect the information and
846 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
847 }
848}
849
850// Tool information record
851
852namespace ToolFlags {
853 enum ToolFlags { Join = 0x1, Sink = 0x2 };
854}
855
856struct ToolDescription : public RefCountedBase<ToolDescription> {
857 std::string Name;
858 Init* CmdLine;
859 Init* Actions;
860 StrVector InLanguage;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000861 std::string InFileOption;
862 std::string OutFileOption;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000863 std::string OutLanguage;
864 std::string OutputSuffix;
865 unsigned Flags;
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000866 const Init* OnEmpty;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000867
868 // Various boolean properties
869 void setSink() { Flags |= ToolFlags::Sink; }
870 bool isSink() const { return Flags & ToolFlags::Sink; }
871 void setJoin() { Flags |= ToolFlags::Join; }
872 bool isJoin() const { return Flags & ToolFlags::Join; }
873
874 // Default ctor here is needed because StringMap can only store
875 // DefaultConstructible objects
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000876 ToolDescription ()
Mikhail Glushenkovc4301292010-02-23 09:05:01 +0000877 : CmdLine(0), Actions(0), OutFileOption("-o"),
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000878 Flags(0), OnEmpty(0)
879 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000880 ToolDescription (const std::string& n)
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000881 : Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
882 Flags(0), OnEmpty(0)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000883 {}
884};
885
886/// ToolDescriptions - A list of Tool information records.
887typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
888
889
890/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000891/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000892
893class CollectToolProperties;
894typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000895(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000896
897class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
898{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000899private:
900
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000901 /// toolDesc_ - Properties of the current Tool. This is where the
902 /// information is stored.
903 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000904
905public:
906
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000907 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000908 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000909 {
910 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000911
912 AddHandler("actions", &CollectToolProperties::onActions);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000913 AddHandler("command", &CollectToolProperties::onCommand);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000914 AddHandler("in_language", &CollectToolProperties::onInLanguage);
915 AddHandler("join", &CollectToolProperties::onJoin);
916 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000917
918 AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
919 AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
920
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000921 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
922 AddHandler("sink", &CollectToolProperties::onSink);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000923 AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000924
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000925 staticMembersInitialized_ = true;
926 }
927 }
928
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000929 void operator() (Init* I) {
930 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000931 }
932
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000933private:
934
935 /// Property handlers --
936 /// Functions that extract information about tool properties from
937 /// DAG representation.
938
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000939 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000940 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000941 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000942 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000943 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000944 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000945 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000946 }
947
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000948 void onCommand (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000949 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000950 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000951 }
952
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000953 void onInLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000954 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000955 Init* arg = d.getArg(0);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000956
957 // Find out the argument's type.
958 if (typeid(*arg) == typeid(StringInit)) {
959 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000960 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000961 }
962 else {
963 // It's a list.
964 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000965 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000966
967 // Copy strings to the output vector.
968 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
969 B != E; ++B) {
970 out.push_back(InitPtrToString(*B));
971 }
972
973 // Remove duplicates.
974 std::sort(out.begin(), out.end());
975 StrVector::iterator newE = std::unique(out.begin(), out.end());
976 out.erase(newE, out.end());
977 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000978 }
979
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000980 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000981 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000982 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000983 }
984
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000985 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000986 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000987 toolDesc_.OutLanguage = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000988 }
989
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000990 void onOutFileOption (const DagInit& d) {
991 CheckNumberOfArguments(d, 1);
992 toolDesc_.OutFileOption = InitPtrToString(d.getArg(0));
993 }
994
995 void onInFileOption (const DagInit& d) {
996 CheckNumberOfArguments(d, 1);
997 toolDesc_.InFileOption = InitPtrToString(d.getArg(0));
998 }
999
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001000 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001001 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001002 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001003 }
1004
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001005 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001006 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001007 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001008 }
1009
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001010 void onWorksOnEmpty (const DagInit& d) {
1011 toolDesc_.OnEmpty = d.getArg(0);
1012 }
1013
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001014};
1015
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001016/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001017/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001018/// CollectToolProperties function object).
1019void CollectToolDescriptions (RecordVector::const_iterator B,
1020 RecordVector::const_iterator E,
1021 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001022{
1023 // Iterate over a properties list of every Tool definition
1024 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001025 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001026 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001027 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001028
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001029 IntrusiveRefCntPtr<ToolDescription>
1030 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001031
1032 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001033 CollectToolProperties(*ToolDesc));
1034 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001035 }
1036}
1037
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001038/// FillInEdgeVector - Merge all compilation graph definitions into
1039/// one single edge list.
1040void FillInEdgeVector(RecordVector::const_iterator B,
1041 RecordVector::const_iterator E, RecordVector& Out) {
1042 for (; B != E; ++B) {
1043 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +00001044
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001045 for (unsigned i = 0; i < edges->size(); ++i)
1046 Out.push_back(edges->getElementAsRecord(i));
1047 }
1048}
1049
1050/// CalculatePriority - Calculate the priority of this plugin.
1051int CalculatePriority(RecordVector::const_iterator B,
1052 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +00001053 int priority = 0;
1054
1055 if (B != E) {
1056 priority = static_cast<int>((*B)->getValueAsInt("priority"));
1057
1058 if (++B != E)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001059 throw "More than one 'PluginPriority' instance found: "
1060 "most probably an error!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001061 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +00001062
1063 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001064}
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001065
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001066/// NotInGraph - Helper function object for FilterNotInGraph.
1067struct NotInGraph {
1068private:
1069 const llvm::StringSet<>& ToolsInGraph_;
1070
1071public:
1072 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
1073 : ToolsInGraph_(ToolsInGraph)
1074 {}
1075
1076 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
1077 return (ToolsInGraph_.count(x->Name) == 0);
1078 }
1079};
1080
1081/// FilterNotInGraph - Filter out from ToolDescs all Tools not
1082/// mentioned in the compilation graph definition.
1083void FilterNotInGraph (const RecordVector& EdgeVector,
1084 ToolDescriptions& ToolDescs) {
1085
1086 // List all tools mentioned in the graph.
1087 llvm::StringSet<> ToolsInGraph;
1088
1089 for (RecordVector::const_iterator B = EdgeVector.begin(),
1090 E = EdgeVector.end(); B != E; ++B) {
1091
1092 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001093 const std::string& NodeA = Edge->getValueAsString("a");
1094 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001095
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001096 if (NodeA != "root")
1097 ToolsInGraph.insert(NodeA);
1098 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001099 }
1100
1101 // Filter ToolPropertiesList.
1102 ToolDescriptions::iterator new_end =
1103 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1104 NotInGraph(ToolsInGraph));
1105 ToolDescs.erase(new_end, ToolDescs.end());
1106}
1107
1108/// FillInToolToLang - Fills in two tables that map tool names to
1109/// (input, output) languages. Helper function used by TypecheckGraph().
1110void FillInToolToLang (const ToolDescriptions& ToolDescs,
1111 StringMap<StringSet<> >& ToolToInLang,
1112 StringMap<std::string>& ToolToOutLang) {
1113 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1114 E = ToolDescs.end(); B != E; ++B) {
1115 const ToolDescription& D = *(*B);
1116 for (StrVector::const_iterator B = D.InLanguage.begin(),
1117 E = D.InLanguage.end(); B != E; ++B)
1118 ToolToInLang[D.Name].insert(*B);
1119 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001120 }
1121}
1122
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001123/// TypecheckGraph - Check that names for output and input languages
1124/// on all edges do match. This doesn't do much when the information
1125/// about the whole graph is not available (i.e. when compiling most
1126/// plugins).
1127void TypecheckGraph (const RecordVector& EdgeVector,
1128 const ToolDescriptions& ToolDescs) {
1129 StringMap<StringSet<> > ToolToInLang;
1130 StringMap<std::string> ToolToOutLang;
1131
1132 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
1133 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1134 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
1135
1136 for (RecordVector::const_iterator B = EdgeVector.begin(),
1137 E = EdgeVector.end(); B != E; ++B) {
1138 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001139 const std::string& NodeA = Edge->getValueAsString("a");
1140 const std::string& NodeB = Edge->getValueAsString("b");
1141 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
1142 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001143
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001144 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001145 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001146 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001147 + ": output->input language mismatch";
1148 }
1149
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001150 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001151 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001152 }
1153}
1154
1155/// WalkCase - Walks the 'case' expression DAG and invokes
1156/// TestCallback on every test, and StatementCallback on every
1157/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001158/// combinators (that is, they are passed directly to TestCallback).
1159/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1160/// IndentLevel, bool FirstTest)'.
1161/// StatementCallback must have type 'void StatementCallback(const Init*,
1162/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001163template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001164void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1165 unsigned IndentLevel = 0)
1166{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001167 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001168
1169 // Error checks.
1170 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001171 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001172
1173 if (d.getNumArgs() < 2)
1174 throw "There should be at least one clause in the 'case' expression:\n"
1175 + d.getAsString();
1176
1177 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001178 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001179 const unsigned numArgs = d.getNumArgs();
1180 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001181 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1182 B != E; ++B) {
1183 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001184
1185 if (!even)
1186 {
1187 // Handle test.
1188 const DagInit& Test = InitPtrToDag(arg);
1189
1190 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001191 throw "The 'default' clause should be the last in the "
1192 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001193 if (i == numArgs)
1194 throw "Case construct handler: no corresponding action "
1195 "found for the test " + Test.getAsString() + '!';
1196
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001197 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001198 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001199 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001200 {
1201 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001202 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001203 // Nested 'case'.
1204 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1205 }
1206
1207 // Handle statement.
1208 StatementCallback(arg, IndentLevel);
1209 }
1210
1211 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001212 even = !even;
1213 }
1214}
1215
1216/// ExtractOptionNames - A helper function object used by
1217/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1218class ExtractOptionNames {
1219 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001220
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001221 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001222 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001223 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001224 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001225 ActionName == "forward_value" ||
1226 ActionName == "forward_transformed_value" ||
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001227 ActionName == "switch_on" || ActionName == "any_switch_on" ||
1228 ActionName == "parameter_equals" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001229 ActionName == "element_in_list" || ActionName == "not_empty" ||
1230 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001231 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001232
1233 Init* Arg = Stmt.getArg(0);
1234 if (typeid(*Arg) == typeid(StringInit)) {
1235 const std::string& Name = InitPtrToString(Arg);
1236 OptionNames_.insert(Name);
1237 }
1238 else {
1239 // It's a list.
1240 const ListInit& List = InitPtrToList(Arg);
1241 for (ListInit::const_iterator B = List.begin(), E = List.end();
1242 B != E; ++B) {
1243 const std::string& Name = InitPtrToString(*B);
1244 OptionNames_.insert(Name);
1245 }
1246 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001247 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001248 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001249 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001250 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001251 }
1252 }
1253 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001254
1255public:
1256 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1257 {}
1258
1259 void operator()(const Init* Statement) {
1260 if (typeid(*Statement) == typeid(ListInit)) {
1261 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1262 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1263 B != E; ++B)
1264 this->processDag(*B);
1265 }
1266 else {
1267 this->processDag(Statement);
1268 }
1269 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001270
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001271 void operator()(const DagInit& Test, unsigned, bool) {
1272 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001273 }
1274 void operator()(const Init* Statement, unsigned) {
1275 this->operator()(Statement);
1276 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001277};
1278
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001279/// CheckForSuperfluousOptions - Check that there are no side
1280/// effect-free options (specified only in the OptionList). Otherwise,
1281/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001282void CheckForSuperfluousOptions (const RecordVector& Edges,
1283 const ToolDescriptions& ToolDescs,
1284 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001285 llvm::StringSet<> nonSuperfluousOptions;
1286
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001287 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001288 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001289 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1290 E = ToolDescs.end(); B != E; ++B) {
1291 const ToolDescription& TD = *(*B);
1292 ExtractOptionNames Callback(nonSuperfluousOptions);
1293 if (TD.Actions)
1294 WalkCase(TD.Actions, Callback, Callback);
1295 }
1296
1297 // Add all options mentioned in the 'case' clauses of the
1298 // OptionalEdges of the compilation graph to the set of
1299 // non-superfluous options.
1300 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1301 B != E; ++B) {
1302 const Record* Edge = *B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001303 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001304
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001305 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001306 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001307 }
1308
1309 // Check that all options in OptDescs belong to the set of
1310 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001311 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001312 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001313 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001314 if (!nonSuperfluousOptions.count(Val.Name)
1315 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001316 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001317 "Probable cause: this option is specified only in the OptionList.\n";
1318 }
1319}
1320
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001321/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1322bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1323 if (TestName == "single_input_file") {
1324 O << "InputFilenames.size() == 1";
1325 return true;
1326 }
1327 else if (TestName == "multiple_input_files") {
1328 O << "InputFilenames.size() > 1";
1329 return true;
1330 }
1331
1332 return false;
1333}
1334
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001335/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1336template <typename F>
1337void EmitListTest(const ListInit& L, const char* LogicOp,
1338 F Callback, raw_ostream& O)
1339{
1340 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1341 // of Dags...
1342 bool isFirst = true;
1343 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1344 if (isFirst)
1345 isFirst = false;
1346 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001347 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001348 Callback(InitPtrToString(*B), O);
1349 }
1350}
1351
1352// Callbacks for use with EmitListTest.
1353
1354class EmitSwitchOn {
1355 const OptionDescriptions& OptDescs_;
1356public:
1357 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1358 {}
1359
1360 void operator()(const std::string& OptName, raw_ostream& O) const {
1361 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1362 O << OptDesc.GenVariableName();
1363 }
1364};
1365
1366class EmitEmptyTest {
1367 bool EmitNegate_;
1368 const OptionDescriptions& OptDescs_;
1369public:
1370 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1371 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1372 {}
1373
1374 void operator()(const std::string& OptName, raw_ostream& O) const {
1375 const char* Neg = (EmitNegate_ ? "!" : "");
1376 if (OptName == "o") {
1377 O << Neg << "OutputFilename.empty()";
1378 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001379 else if (OptName == "save-temps") {
1380 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1381 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001382 else {
1383 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1384 O << Neg << OptDesc.GenVariableName() << ".empty()";
1385 }
1386 }
1387};
1388
1389
1390/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1391bool EmitCaseTest1ArgList(const std::string& TestName,
1392 const DagInit& d,
1393 const OptionDescriptions& OptDescs,
1394 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001395 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001396
1397 if (TestName == "any_switch_on") {
1398 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1399 return true;
1400 }
1401 else if (TestName == "switch_on") {
1402 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1403 return true;
1404 }
1405 else if (TestName == "any_not_empty") {
1406 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1407 return true;
1408 }
1409 else if (TestName == "any_empty") {
1410 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1411 return true;
1412 }
1413 else if (TestName == "not_empty") {
1414 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1415 return true;
1416 }
1417 else if (TestName == "empty") {
1418 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1419 return true;
1420 }
1421
1422 return false;
1423}
1424
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001425/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1426bool EmitCaseTest1ArgStr(const std::string& TestName,
1427 const DagInit& d,
1428 const OptionDescriptions& OptDescs,
1429 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001430 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001431
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001432 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001433 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001434 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001435 }
1436 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001437 O << "InLangs.count(\"" << OptName << "\") != 0";
1438 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001439 }
1440 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001441 // This works only for single-argument Tool::GenerateAction. Join
1442 // tools can process several files in different languages simultaneously.
1443
1444 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001445 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001446 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001447 }
1448 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001449 bool EmitNegate = (TestName == "not_empty");
1450 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001451 return true;
1452 }
1453
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001454 return false;
1455}
1456
1457/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1458bool EmitCaseTest1Arg(const std::string& TestName,
1459 const DagInit& d,
1460 const OptionDescriptions& OptDescs,
1461 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001462 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001463 if (typeid(*d.getArg(0)) == typeid(ListInit))
1464 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1465 else
1466 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1467}
1468
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001469/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001470bool EmitCaseTest2Args(const std::string& TestName,
1471 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001472 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001473 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001474 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001475 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001476 const std::string& OptName = InitPtrToString(d.getArg(0));
1477 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001478
1479 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001480 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001481 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1482 return true;
1483 }
1484 else if (TestName == "element_in_list") {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001485 const OptionDescription& OptDesc = OptDescs.FindParameterList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001486 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001487 O << "std::find(" << VarName << ".begin(),\n";
1488 O.indent(IndentLevel + Indent1)
1489 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001490 << OptArg << "\") != " << VarName << ".end()";
1491 return true;
1492 }
1493
1494 return false;
1495}
1496
1497// Forward declaration.
1498// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001499void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001500 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001501 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001502
1503/// EmitLogicalOperationTest - Helper function used by
1504/// EmitCaseConstructHandler.
1505void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001506 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001507 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001508 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001509 O << '(';
1510 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001511 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001512 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001513 if (j != NumArgs - 1) {
1514 O << ")\n";
1515 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1516 }
1517 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001518 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001519 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001520 }
1521}
1522
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001523void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001524 const OptionDescriptions& OptDescs, raw_ostream& O)
1525{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001526 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001527 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1528 O << "! (";
1529 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1530 O << ")";
1531}
1532
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001533/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001534void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001535 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001536 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001537 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001538
1539 if (TestName == "and")
1540 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1541 else if (TestName == "or")
1542 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001543 else if (TestName == "not")
1544 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001545 else if (EmitCaseTest0Args(TestName, O))
1546 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001547 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1548 return;
1549 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1550 return;
1551 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001552 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001553}
1554
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001555
1556/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1557class EmitCaseTestCallback {
1558 bool EmitElseIf_;
1559 const OptionDescriptions& OptDescs_;
1560 raw_ostream& O_;
1561public:
1562
1563 EmitCaseTestCallback(bool EmitElseIf,
1564 const OptionDescriptions& OptDescs, raw_ostream& O)
1565 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1566 {}
1567
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001568 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001569 {
1570 if (GetOperatorName(Test) == "default") {
1571 O_.indent(IndentLevel) << "else {\n";
1572 }
1573 else {
1574 O_.indent(IndentLevel)
1575 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001576 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001577 O_ << ") {\n";
1578 }
1579 }
1580};
1581
1582/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1583template <typename F>
1584class EmitCaseStatementCallback {
1585 F Callback_;
1586 raw_ostream& O_;
1587public:
1588
1589 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1590 : Callback_(Callback), O_(O)
1591 {}
1592
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001593 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001594
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001595 // Ignore nested 'case' DAG.
1596 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001597 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001598 if (typeid(*Statement) == typeid(ListInit)) {
1599 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1600 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1601 B != E; ++B)
1602 Callback_(*B, (IndentLevel + Indent1), O_);
1603 }
1604 else {
1605 Callback_(Statement, (IndentLevel + Indent1), O_);
1606 }
1607 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001608 O_.indent(IndentLevel) << "}\n";
1609 }
1610
1611};
1612
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001613/// EmitCaseConstructHandler - Emit code that handles the 'case'
1614/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001615/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001616/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001617/// raw_ostream& O).
1618/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001619/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1620/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001621template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001622void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001623 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001624 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001625 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001626 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1627 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001628}
1629
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001630/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001631/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1632/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001633void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001634 const char* Delimiters = " \t\n\v\f\r";
1635 enum TokenizerState
1636 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1637 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001638
1639 if (CmdLine.empty())
1640 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001641 Out.push_back("");
1642
1643 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1644 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001645
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001646 for (; B != E; ++B) {
1647 char cur_ch = CmdLine[B];
1648
1649 switch (cur_st) {
1650 case Normal:
1651 if (cur_ch == '$') {
1652 cur_st = SpecialCommand;
1653 break;
1654 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001655 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001656 // Skip whitespace
1657 B = CmdLine.find_first_not_of(Delimiters, B);
1658 if (B == std::string::npos) {
1659 B = E-1;
1660 continue;
1661 }
1662 --B;
1663 Out.push_back("");
1664 continue;
1665 }
1666 break;
1667
1668
1669 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001670 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001671 cur_st = Normal;
1672 Out.push_back("");
1673 continue;
1674 }
1675 if (cur_ch == '(') {
1676 Out.push_back("");
1677 cur_st = InsideSpecialCommand;
1678 continue;
1679 }
1680 break;
1681
1682 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001683 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001684 continue;
1685 }
1686 if (cur_ch == '\'') {
1687 cur_st = InsideQuotationMarks;
1688 Out.push_back("");
1689 continue;
1690 }
1691 if (cur_ch == ')') {
1692 cur_st = Normal;
1693 Out.push_back("");
1694 }
1695 if (cur_ch == ',') {
1696 continue;
1697 }
1698
1699 break;
1700
1701 case InsideQuotationMarks:
1702 if (cur_ch == '\'') {
1703 cur_st = InsideSpecialCommand;
1704 continue;
1705 }
1706 break;
1707 }
1708
1709 Out.back().push_back(cur_ch);
1710 }
1711}
1712
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001713/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1714/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1715/// SubstituteSpecialCommands().
1716StrVector::const_iterator
1717SubstituteCall (StrVector::const_iterator Pos,
1718 StrVector::const_iterator End,
1719 bool IsJoin, raw_ostream& O)
1720{
1721 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001722 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001723 const std::string& CmdName = *Pos;
1724
1725 if (CmdName == ")")
1726 throw "$CALL invocation: empty argument list!";
1727
1728 O << "hooks::";
1729 O << CmdName << "(";
1730
1731
1732 bool firstIteration = true;
1733 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001734 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001735 const std::string& Arg = *Pos;
1736 assert(Arg.size() != 0);
1737
1738 if (Arg[0] == ')')
1739 break;
1740
1741 if (firstIteration)
1742 firstIteration = false;
1743 else
1744 O << ", ";
1745
1746 if (Arg == "$INFILE") {
1747 if (IsJoin)
1748 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1749 else
1750 O << "inFile.c_str()";
1751 }
1752 else {
1753 O << '"' << Arg << '"';
1754 }
1755 }
1756
1757 O << ')';
1758
1759 return Pos;
1760}
1761
1762/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1763/// function used by SubstituteSpecialCommands().
1764StrVector::const_iterator
1765SubstituteEnv (StrVector::const_iterator Pos,
1766 StrVector::const_iterator End, raw_ostream& O)
1767{
1768 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001769 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001770 const std::string& EnvName = *Pos;
1771
1772 if (EnvName == ")")
1773 throw "$ENV invocation: empty argument list!";
1774
1775 O << "checkCString(std::getenv(\"";
1776 O << EnvName;
1777 O << "\"))";
1778
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001779 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001780
1781 return Pos;
1782}
1783
1784/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1785/// handler code. Helper function used by EmitCmdLineVecFill().
1786StrVector::const_iterator
1787SubstituteSpecialCommands (StrVector::const_iterator Pos,
1788 StrVector::const_iterator End,
1789 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001790{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001791
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001792 const std::string& cmd = *Pos;
1793
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001794 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001795 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001796 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001797 }
1798 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001799 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001800 }
1801 else {
1802 throw "Unknown special command: " + cmd;
1803 }
1804
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001805 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001806 const std::string& Leftover = *Pos;
1807 assert(Leftover.at(0) == ')');
1808 if (Leftover.size() != 1)
1809 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001810
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001811 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001812}
1813
1814/// EmitCmdLineVecFill - Emit code that fills in the command line
1815/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001816void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001817 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001818 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001819 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001820 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001821
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001822 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001823 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001824
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001825 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001826
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001827 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001828 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001829 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001830 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001831 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1832 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001833 }
1834 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001835 O << '"' << StrVec[0] << '"';
1836 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001837 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001838 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001839
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001840 // Go through the command arguments.
1841 assert(B <= E);
1842 for (; B != E; ++B) {
1843 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001844
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001845 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001846 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001847
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001848 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001849 O << "vec.push_back(std::make_pair(0, ";
1850 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1851 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001852 }
1853 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001854 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001855 }
1856 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001857
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001858}
1859
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001860/// EmitForEachListElementCycleHeader - Emit common code for iterating through
1861/// all elements of a list. Helper function used by
1862/// EmitForwardOptionPropertyHandlingCode.
1863void EmitForEachListElementCycleHeader (const OptionDescription& D,
1864 unsigned IndentLevel,
1865 raw_ostream& O) {
1866 unsigned IndentLevel1 = IndentLevel + Indent1;
1867
1868 O.indent(IndentLevel)
1869 << "for (" << D.GenTypeDeclaration()
1870 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1871 O.indent(IndentLevel)
1872 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1873 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1874 << ".getPosition(B - " << D.GenVariableName()
1875 << ".begin());\n";
1876}
1877
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001878/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1879/// implement EmitActionHandler. Emits code for
1880/// handling the (forward) and (forward_as) option properties.
1881void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001882 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001883 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001884 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001885 const std::string& Name = NewName.empty()
1886 ? ("-" + D.Name)
1887 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001888 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001889
1890 switch (D.Type) {
1891 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001892 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001893 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1894 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001895 break;
1896 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001897 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1898 << D.GenVariableName()
1899 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001900
1901 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001902 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001903 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1904 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001905 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001906 }
1907 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001908 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001909 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001910 break;
1911 case OptionType::Prefix:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001912 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1913 << D.GenVariableName() << ".getPosition(), \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001914 << Name << "\" + "
1915 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001916 break;
1917 case OptionType::PrefixList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001918 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001919 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001920 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001921 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001922
1923 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001924 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001925 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001926 }
1927
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001928 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001929 break;
1930 case OptionType::ParameterList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001931 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001932 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001933 << Name << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001934
1935 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001936 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001937 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001938 }
1939
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001940 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001941 break;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001942 case OptionType::SwitchList:
Mikhail Glushenkovcf95ecc2010-07-19 17:17:22 +00001943 EmitForEachListElementCycleHeader(D, IndentLevel, O);
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001944 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
1945 << Name << "\"));\n";
1946 O.indent(IndentLevel1) << "++B;\n";
1947 O.indent(IndentLevel) << "}\n";
1948 break;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001949 case OptionType::Alias:
1950 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001951 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001952 }
1953}
1954
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001955/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1956/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001957struct ActionHandlingCallbackBase
1958{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001959
1960 void onErrorDag(const DagInit& d,
1961 unsigned IndentLevel, raw_ostream& O) const
1962 {
1963 O.indent(IndentLevel)
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00001964 << "PrintError(\""
1965 << (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0)) : "Unknown error!")
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001966 << "\");\n";
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +00001967 O.indent(IndentLevel) << "return 1;\n";
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001968 }
1969
1970 void onWarningDag(const DagInit& d,
1971 unsigned IndentLevel, raw_ostream& O) const
1972 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001973 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001974 O.indent(IndentLevel) << "llvm::errs() << \""
1975 << InitPtrToString(d.getArg(0)) << "\";\n";
1976 }
1977
1978};
1979
1980/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1981/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001982
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001983class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001984
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001985typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1986(const DagInit&, unsigned, raw_ostream&) const;
1987
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001988class EmitActionHandlersCallback :
1989 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001990 public HandlerTable<EmitActionHandlersCallbackHandler>
1991{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001992 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001993
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001994 const OptionDescriptions& OptDescs;
1995
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001996 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1997 /// onAppendCmd and onOutputSuffix.
1998 void EmitHookInvocation(const std::string& Str,
1999 const char* BlockOpen, const char* BlockClose,
2000 unsigned IndentLevel, raw_ostream& O) const
2001 {
2002 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002003 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002004
2005 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
2006 B != E; ++B) {
2007 const std::string& cmd = *B;
2008
2009 O.indent(IndentLevel) << BlockOpen;
2010
2011 if (cmd.at(0) == '$')
2012 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
2013 else
2014 O << '"' << cmd << '"';
2015
2016 O << BlockClose;
2017 }
2018 }
2019
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002020 void onAppendCmd (const DagInit& Dag,
2021 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002022 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002023 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002024 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002025 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002026 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002027 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00002028
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002029 void onForward (const DagInit& Dag,
2030 unsigned IndentLevel, raw_ostream& O) const
2031 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002032 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002033 const std::string& Name = InitPtrToString(Dag.getArg(0));
2034 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2035 IndentLevel, "", O);
2036 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002037
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002038 void onForwardAs (const DagInit& Dag,
2039 unsigned IndentLevel, raw_ostream& O) const
2040 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002041 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002042 const std::string& Name = InitPtrToString(Dag.getArg(0));
2043 const std::string& NewName = InitPtrToString(Dag.getArg(1));
2044 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2045 IndentLevel, NewName, O);
2046 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002047
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002048 void onForwardValue (const DagInit& Dag,
2049 unsigned IndentLevel, raw_ostream& O) const
2050 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002051 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002052 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002053 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002054
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002055 if (D.isSwitchList()) {
2056 throw std::runtime_error
2057 ("forward_value is not allowed with switch_list");
2058 }
2059
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002060 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002061 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2062 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002063 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002064 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002065 else {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002066 O.indent(IndentLevel) << "for (" << D.GenTypeDeclaration()
2067 << "::iterator B = " << D.GenVariableName()
2068 << ".begin(), \n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002069 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
2070 << ".end(); B != E; ++B)\n";
2071 O.indent(IndentLevel) << "{\n";
2072 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002073 << "unsigned pos = " << D.GenVariableName()
2074 << ".getPosition(B - " << D.GenVariableName()
2075 << ".begin());\n";
2076 O.indent(IndentLevel + Indent1)
2077 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002078 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002079 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002080 }
2081
2082 void onForwardTransformedValue (const DagInit& Dag,
2083 unsigned IndentLevel, raw_ostream& O) const
2084 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002085 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002086 const std::string& Name = InitPtrToString(Dag.getArg(0));
2087 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002088 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002089
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002090 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2091 << D.GenVariableName() << ".getPosition("
2092 << (D.isList() ? "0" : "") << "), "
2093 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002094 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2095 }
2096
2097 void onNoOutFile (const DagInit& Dag,
2098 unsigned IndentLevel, raw_ostream& O) const
2099 {
2100 CheckNumberOfArguments(Dag, 0);
2101 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002102 }
2103
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002104 void onOutputSuffix (const DagInit& Dag,
2105 unsigned IndentLevel, raw_ostream& O) const
2106 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002107 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002108 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2109 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002110 }
2111
2112 void onStopCompilation (const DagInit& Dag,
2113 unsigned IndentLevel, raw_ostream& O) const
2114 {
2115 O.indent(IndentLevel) << "stop_compilation = true;\n";
2116 }
2117
2118
2119 void onUnpackValues (const DagInit& Dag,
2120 unsigned IndentLevel, raw_ostream& O) const
2121 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002122 throw "'unpack_values' is deprecated. "
2123 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002124 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002125
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002126 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002127
2128 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2129 : OptDescs(OD)
2130 {
2131 if (!staticMembersInitialized_) {
2132 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2133 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2134 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2135 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2136 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002137 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2138 AddHandler("forward_transformed_value",
2139 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002140 AddHandler("no_out_file",
2141 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002142 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2143 AddHandler("stop_compilation",
2144 &EmitActionHandlersCallback::onStopCompilation);
2145 AddHandler("unpack_values",
2146 &EmitActionHandlersCallback::onUnpackValues);
2147
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002148
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002149 staticMembersInitialized_ = true;
2150 }
2151 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002152
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002153 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002154 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002155 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002156 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002157 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002158};
2159
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002160void EmitGenerateActionMethodHeader(const ToolDescription& D,
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002161 bool IsJoin, bool Naked,
2162 raw_ostream& O)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002163{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002164 O.indent(Indent1) << "int GenerateAction(Action& Out,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002165
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002166 if (IsJoin)
2167 O.indent(Indent2) << "const PathVector& inFiles,\n";
2168 else
2169 O.indent(Indent2) << "const sys::Path& inFile,\n";
2170
2171 O.indent(Indent2) << "const bool HasChildren,\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002172 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2173 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2174 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2175 O.indent(Indent1) << "{\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002176
2177 if (!Naked) {
2178 O.indent(Indent2) << "std::string cmd;\n";
2179 O.indent(Indent2) << "std::string out_file;\n";
2180 O.indent(Indent2)
2181 << "std::vector<std::pair<unsigned, std::string> > vec;\n";
2182 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
2183 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002184 O.indent(Indent2) << "std::string output_suffix(\""
2185 << D.OutputSuffix << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002186 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002187}
2188
2189// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2190// Tool::GenerateAction() method.
2191void EmitGenerateActionMethod (const ToolDescription& D,
2192 const OptionDescriptions& OptDescs,
2193 bool IsJoin, raw_ostream& O) {
2194
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002195 EmitGenerateActionMethodHeader(D, IsJoin, /* Naked = */ false, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002196
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002197 if (!D.CmdLine)
2198 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002199
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002200 // Process the 'command' property.
2201 O << '\n';
2202 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2203 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002204
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002205 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002206 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002207 EmitCaseConstructHandler(D.Actions, Indent2,
2208 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002209 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002210 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002211
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002212 // Input file (s)
2213 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002214 O.indent(Indent2)
2215 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2216 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002217 }
2218
2219 if (IsJoin) {
2220 O.indent(Indent2)
2221 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2222 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2223 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002224 O.indent(Indent3) << "vec.push_back(std::make_pair("
2225 << "InputFilenames.getPosition(B - inFiles.begin()), "
2226 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002227 O.indent(Indent2) << "}\n";
2228 }
2229 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002230 O.indent(Indent2) << "vec.push_back(std::make_pair("
2231 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002232 }
2233
2234 // Output file
2235 O.indent(Indent2) << "if (!no_out_file) {\n";
2236 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002237 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002238 << D.OutFileOption << "\"));\n";
2239
2240 O.indent(Indent3) << "out_file = this->OutFilename("
2241 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
Mikhail Glushenkov2e027cb2010-08-13 02:29:24 +00002242 O.indent(Indent4) <<
2243 "TempDir, stop_compilation, output_suffix.c_str()).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002244 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002245
2246 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002247
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002248 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002249 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002250 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002251 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
2252 << SinkOptionName << ".begin(), E = " << SinkOptionName
2253 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002254 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOptionName
2255 << ".getPosition(B - " << SinkOptionName
2256 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002257 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002258 }
2259
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002260 O.indent(Indent2) << "Out.Construct(cmd, this->SortArgs(vec), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002261 << "stop_compilation, out_file);\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002262 O.indent(Indent2) << "return 0;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002263 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002264}
2265
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002266/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2267/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002268void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2269 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002270 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002271 if (!ToolDesc.isJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002272 EmitGenerateActionMethodHeader(ToolDesc, /* IsJoin = */ true,
2273 /* Naked = */ true, O);
2274 O.indent(Indent2) << "PrintError(\"" << ToolDesc.Name
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002275 << " is not a Join tool!\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002276 O.indent(Indent2) << "return -1;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002277 O.indent(Indent1) << "}\n\n";
2278 }
2279 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002280 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002281 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002282
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002283 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002284}
2285
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002286/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2287/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002288void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002289 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2290 O.indent(Indent2) << "return InputLanguages_;\n";
2291 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002292
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002293 if (D.OutLanguage.empty())
2294 throw "Tool " + D.Name + " has no 'out_language' property!";
2295
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002296 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2297 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2298 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002299}
2300
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002301/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002302void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002303 O.indent(Indent1) << "const char* Name() const {\n";
2304 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2305 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002306}
2307
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002308/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2309/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002310void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002311 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002312 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002313 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002314 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002315 O.indent(Indent2) << "return false;\n";
2316 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002317}
2318
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002319/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2320/// conjunction with EmitCaseConstructHandler.
2321void EmitWorksOnEmptyCallback (const Init* Value,
2322 unsigned IndentLevel, raw_ostream& O) {
2323 CheckBooleanConstant(Value);
2324 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2325}
2326
2327/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2328/// class.
2329void EmitWorksOnEmptyMethod (const ToolDescription& D,
2330 const OptionDescriptions& OptDescs,
2331 raw_ostream& O)
2332{
2333 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2334 if (D.OnEmpty == 0)
2335 O.indent(Indent2) << "return false;\n";
2336 else
2337 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2338 /*EmitElseIf = */ true, OptDescs, O);
2339 O.indent(Indent1) << "}\n\n";
2340}
2341
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002342/// EmitStaticMemberDefinitions - Emit static member definitions for a
2343/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002344void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002345 if (D.InLanguage.empty())
2346 throw "Tool " + D.Name + " has no 'in_language' property!";
2347
2348 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2349 for (StrVector::const_iterator B = D.InLanguage.begin(),
2350 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002351 O << '\"' << *B << "\", ";
2352 O << "0};\n\n";
2353}
2354
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002355/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002356void EmitToolClassDefinition (const ToolDescription& D,
2357 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002358 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002359 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002360 return;
2361
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002362 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002363 O << "class " << D.Name << " : public ";
2364 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002365 O << "JoinTool";
2366 else
2367 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002368
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002369 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002370 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002371
2372 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002373 EmitNameMethod(D, O);
2374 EmitInOutLanguageMethods(D, O);
2375 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002376 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002377 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002378
2379 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002380 O << "};\n";
2381
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002382 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002383
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002384}
2385
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002386/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002387/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002388void EmitOptionDefinitions (const OptionDescriptions& descs,
2389 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002390 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002391{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002392 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002393
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002394 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002395 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002396 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002397 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002398
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002399 if (val.Type == OptionType::Alias) {
2400 Aliases.push_back(val);
2401 continue;
2402 }
2403
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002404 if (val.isExtern())
2405 O << "extern ";
2406
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002407 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002408 << val.GenVariableName();
2409
2410 if (val.isExtern()) {
2411 O << ";\n";
2412 continue;
2413 }
2414
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002415 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002416
2417 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2418 O << ", cl::Prefix";
2419
2420 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002421 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002422 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002423 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002424 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002425 }
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002426
2427 if (val.isOptional())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002428 O << ", cl::Optional";
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002429
2430 if (val.isOneOrMore())
2431 O << ", cl::OneOrMore";
2432
2433 if (val.isZeroOrMore())
2434 O << ", cl::ZeroOrMore";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002435
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002436 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002437 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002438 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002439 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002440
2441 if (val.isCommaSeparated())
2442 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002443
2444 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002445 O << ", cl::multi_val(" << val.MultiVal << ')';
2446
2447 if (val.InitVal) {
2448 const std::string& str = val.InitVal->getAsString();
2449 O << ", cl::init(" << str << ')';
2450 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002451
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002452 if (!val.Help.empty())
2453 O << ", cl::desc(\"" << val.Help << "\")";
2454
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002455 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002456 }
2457
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002458 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002459 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002460 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002461 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002462
2463 O << val.GenTypeDeclaration() << ' '
2464 << val.GenVariableName()
2465 << "(\"" << val.Name << '\"';
2466
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002467 const OptionDescription& D = descs.FindOption(val.Help);
2468 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002469
2470 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2471 }
2472
2473 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002474 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002475 O << (HasExterns ? "extern cl" : "cl")
2476 << "::list<std::string> " << SinkOptionName
2477 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002478
2479 O << '\n';
2480}
2481
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002482/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002483/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002484
2485class EmitPreprocessOptionsCallback;
2486
2487typedef void
2488(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2489(const DagInit&, unsigned, raw_ostream&) const;
2490
2491class EmitPreprocessOptionsCallback :
2492 public ActionHandlingCallbackBase,
2493 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2494{
2495 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002496 typedef void
2497 (EmitPreprocessOptionsCallback::* HandlerImpl)
2498 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002499
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002500 const OptionDescriptions& OptDescs_;
2501
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002502 void onListOrDag(const DagInit& d, HandlerImpl h,
2503 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002504 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002505 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002506 const Init* I = d.getArg(0);
2507
2508 // If I is a list, apply h to each element.
2509 if (typeid(*I) == typeid(ListInit)) {
2510 const ListInit& L = *static_cast<const ListInit*>(I);
2511 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2512 ((this)->*(h))(*B, IndentLevel, O);
2513 }
2514 // Otherwise, apply h to I.
2515 else {
2516 ((this)->*(h))(I, IndentLevel, O);
2517 }
2518 }
2519
2520 void onUnsetOptionImpl(const Init* I,
2521 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002522 {
2523 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002524 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002525
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002526 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002527 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2528 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002529 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002530 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2531 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002532 else if (OptDesc.isList()) {
2533 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2534 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002535 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002536 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002537 }
2538 }
2539
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002540 void onUnsetOption(const DagInit& d,
2541 unsigned IndentLevel, raw_ostream& O) const
2542 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002543 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2544 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002545 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002546
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002547 void onSetOptionImpl(const DagInit& d,
2548 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002549 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002550 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002551 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002552 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2553
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002554 if (OptDesc.isList()) {
2555 const ListInit& List = InitPtrToList(Value);
2556
2557 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2558 for (ListInit::const_iterator B = List.begin(), E = List.end();
2559 B != E; ++B) {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002560 const Init* CurElem = *B;
2561 if (OptDesc.isSwitchList())
2562 CheckBooleanConstant(CurElem);
2563
2564 O.indent(IndentLevel)
2565 << OptDesc.GenVariableName() << ".push_back(\""
2566 << (OptDesc.isSwitchList() ? CurElem->getAsString()
2567 : InitPtrToString(CurElem))
2568 << "\");\n";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002569 }
2570 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002571 else if (OptDesc.isSwitch()) {
2572 CheckBooleanConstant(Value);
2573 O.indent(IndentLevel) << OptDesc.GenVariableName()
2574 << " = " << Value->getAsString() << ";\n";
2575 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002576 else if (OptDesc.isParameter()) {
2577 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002578 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002579 << " = \"" << Str << "\";\n";
2580 }
2581 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002582 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002583 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002584 }
2585
2586 void onSetSwitch(const Init* I,
2587 unsigned IndentLevel, raw_ostream& O) const {
2588 const std::string& OptName = InitPtrToString(I);
2589 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2590
2591 if (OptDesc.isSwitch())
2592 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2593 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002594 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002595 }
2596
2597 void onSetOption(const DagInit& d,
2598 unsigned IndentLevel, raw_ostream& O) const
2599 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002600 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002601
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002602 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2603 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002604 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002605 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002606 // One argument: (set_option "switch")
2607 // or (set_option ["switch1", "switch2", ...])
2608 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002609 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2610 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002611 }
2612
2613public:
2614
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002615 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002616 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002617 {
2618 if (!staticMembersInitialized_) {
2619 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2620 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2621 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002622 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002623
2624 staticMembersInitialized_ = true;
2625 }
2626 }
2627
2628 void operator()(const Init* I,
2629 unsigned IndentLevel, raw_ostream& O) const
2630 {
2631 InvokeDagInitHandler(this, I, IndentLevel, O);
2632 }
2633
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002634};
2635
2636/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2637void EmitPreprocessOptions (const RecordKeeper& Records,
2638 const OptionDescriptions& OptDecs, raw_ostream& O)
2639{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002640 O << "int PreprocessOptionsLocal() {\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002641
2642 const RecordVector& OptionPreprocessors =
2643 Records.getAllDerivedDefinitions("OptionPreprocessor");
2644
2645 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2646 E = OptionPreprocessors.end(); B!=E; ++B) {
2647 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002648 EmitCaseConstructHandler(Case, Indent1,
2649 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002650 false, OptDecs, O);
2651 }
2652
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002653 O << '\n';
2654 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002655 O << "}\n\n";
2656}
2657
2658/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002659void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002660{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002661 O << "int PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002662
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002663 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002664 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002665
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002666 // It is allowed for a plugin to have no language map.
2667 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002668
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002669 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2670 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002671 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002672
2673 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002674 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002675
2676 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2677 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2678
2679 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002680 O.indent(Indent1) << "langMap[\""
2681 << InitPtrToString(Suffixes->getElement(i))
2682 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002683 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002684 }
2685
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002686 O << '\n';
2687 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002688 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002689}
2690
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002691/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2692/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002693void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002694 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002695 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002696 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002697
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002698 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002699 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002700 }
2701 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002702 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002703 }
2704 else if (OpName == "error") {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002705 // TODO: fix this
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002706 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002707 O.indent(IndentLevel) << "PrintError(\""
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002708 << InitPtrToString(d.getArg(0))
2709 << "\");\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002710 O.indent(IndentLevel) << "return -1;\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002711 return;
2712 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002713 else {
2714 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002715 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002716 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002717
2718 if (d.getNumArgs() > 0)
2719 O << InitPtrToInt(d.getArg(0)) << ";\n";
2720 else
2721 O << "2;\n";
2722
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002723}
2724
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002725/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002726void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002727 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002728 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002729
2730 // Class constructor.
2731 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002732 << "public:\n";
2733 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2734 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002735
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002736 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002737 O.indent(Indent1)
2738 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2739 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002740
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002741 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002742 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002743
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002744 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002745 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002746}
2747
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002748/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002749void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002750 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002751 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002752 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002753 for (RecordVector::const_iterator B = EdgeVector.begin(),
2754 E = EdgeVector.end(); B != E; ++B) {
2755 const Record* Edge = *B;
2756 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002757 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002758
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002759 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002760 EmitEdgeClass(i, NodeB, &Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002761 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002762 }
2763}
2764
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002765/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002766/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002767void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002768 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002769 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002770{
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002771 O << "int PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002772
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002773 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2774 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002775 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002776
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002777 O << '\n';
2778
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002779 // Insert edges.
2780
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002781 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002782 for (RecordVector::const_iterator B = EdgeVector.begin(),
2783 E = EdgeVector.end(); B != E; ++B) {
2784 const Record* Edge = *B;
2785 const std::string& NodeA = Edge->getValueAsString("a");
2786 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002787 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002788
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002789 O.indent(Indent1) << "if (int ret = G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002790
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002791 if (IsDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002792 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002793 else
2794 O << "new Edge" << i << "()";
2795
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002796 O << "))\n";
2797 O.indent(Indent2) << "return ret;\n";
2798
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002799 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002800 }
2801
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002802 O << '\n';
2803 O.indent(Indent1) << "return 0;\n";
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002804 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002805}
2806
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002807/// HookInfo - Information about the hook type and number of arguments.
2808struct HookInfo {
2809
2810 // A hook can either have a single parameter of type std::vector<std::string>,
2811 // or NumArgs parameters of type const char*.
2812 enum HookType { ListHook, ArgHook };
2813
2814 HookType Type;
2815 unsigned NumArgs;
2816
2817 HookInfo() : Type(ArgHook), NumArgs(1)
2818 {}
2819
2820 HookInfo(HookType T) : Type(T), NumArgs(1)
2821 {}
2822
2823 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2824 {}
2825};
2826
2827typedef llvm::StringMap<HookInfo> HookInfoMap;
2828
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002829/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002830/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002831/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002832class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002833 HookInfoMap& HookNames_;
2834 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002835public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002836 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2837 : HookNames_(HookNames), OptDescs_(OptDescs)
2838 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002839
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002840 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002841 const std::string& Name = GetOperatorName(Dag);
2842
2843 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002844 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002845 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2846 const std::string& HookName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002847 const OptionDescription& D =
2848 OptDescs_.FindParameterListOrParameter(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002849
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002850 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2851 : HookInfo::ArgHook);
2852 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002853 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002854 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002855 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2856 }
2857 }
2858
2859 void onCmdLine(const std::string& Cmd) {
2860 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002861 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002862
2863 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2864 B != E; ++B) {
2865 const std::string& cmd = *B;
2866
2867 if (cmd == "$CALL") {
2868 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002869 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002870 const std::string& HookName = *B;
2871
2872 if (HookName.at(0) == ')')
2873 throw "$CALL invoked with no arguments!";
2874
2875 while (++B != E && B->at(0) != ')') {
2876 ++NumArgs;
2877 }
2878
2879 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2880
2881 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2882 H->second.Type != HookInfo::ArgHook)
2883 throw "Overloading of hooks is not allowed. Overloaded hook: "
2884 + HookName;
2885 else
2886 HookNames_[HookName] = HookInfo(NumArgs);
2887 }
2888 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002889 }
2890
2891 void operator()(const Init* Arg) {
2892
2893 // We're invoked on an action (either a dag or a dag list).
2894 if (typeid(*Arg) == typeid(DagInit)) {
2895 const DagInit& Dag = InitPtrToDag(Arg);
2896 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002897 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002898 }
2899 else if (typeid(*Arg) == typeid(ListInit)) {
2900 const ListInit& List = InitPtrToList(Arg);
2901 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2902 ++B) {
2903 const DagInit& Dag = InitPtrToDag(*B);
2904 this->onAction(Dag);
2905 }
2906 return;
2907 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002908
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002909 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002910 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002911 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002912
2913 void operator()(const DagInit* Test, unsigned, bool) {
2914 this->operator()(Test);
2915 }
2916 void operator()(const Init* Statement, unsigned) {
2917 this->operator()(Statement);
2918 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002919};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002920
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002921/// FillInHookNames - Actually extract the hook names from all command
2922/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002923void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002924 const OptionDescriptions& OptDescs,
2925 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002926{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002927 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002928 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2929 E = ToolDescs.end(); B != E; ++B) {
2930 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002931
2932 // Look for 'forward_transformed_value' in 'actions'.
2933 if (D.Actions)
2934 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2935
2936 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002937 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002938 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002939 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002940 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002941 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002942 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002943 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002944 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002945 }
2946}
2947
2948/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2949/// property records and emit hook function declaration for each
2950/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002951void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2952 const OptionDescriptions& OptDescs, raw_ostream& O) {
2953 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002954
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002955 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002956 if (HookNames.empty())
2957 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002958
2959 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002960 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002961 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002962 const char* HookName = B->first();
2963 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002964
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002965 O.indent(Indent1) << "std::string " << HookName << "(";
2966
2967 if (Info.Type == HookInfo::ArgHook) {
2968 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2969 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2970 }
2971 }
2972 else {
2973 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002974 }
2975
2976 O <<");\n";
2977 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002978 O << "}\n\n";
2979}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002980
Mikhail Glushenkovaa1a3732010-08-13 06:02:45 +00002981std::string GetPluginName() {
2982 if (!InputFilename.empty()) {
2983 return sys::Path(InputFilename).getBasename();
2984 }
2985
2986 return "";
2987}
2988
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002989/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002990void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovaa1a3732010-08-13 06:02:45 +00002991 O << "struct Plugin" << GetPluginName()
2992 << " : public llvmc::BasePlugin {\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002993 O.indent(Indent1) << "int Priority() const { return "
2994 << Priority << "; }\n\n";
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00002995 O.indent(Indent1) << "int PreprocessOptions() const\n";
2996 O.indent(Indent1) << "{ return PreprocessOptionsLocal(); }\n\n";
2997 O.indent(Indent1) << "int PopulateLanguageMap(LanguageMap& langMap) const\n";
2998 O.indent(Indent1) << "{ return PopulateLanguageMapLocal(langMap); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002999 O.indent(Indent1)
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00003000 << "int PopulateCompilationGraph(CompilationGraph& graph) const\n";
3001 O.indent(Indent1) << "{ return PopulateCompilationGraphLocal(graph); }\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00003002 << "};\n\n"
Mikhail Glushenkovaa1a3732010-08-13 06:02:45 +00003003 << "static llvmc::RegisterPlugin<Plugin"
3004 << GetPluginName()<< "> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003005}
3006
Mikhail Glushenkov67665722008-11-12 12:41:18 +00003007/// EmitIncludes - Emit necessary #include directives and some
3008/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003009void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00003010 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
3011 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +00003012 << "#include \"llvm/CompilerDriver/Error.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003013 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00003014 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
3015 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003016
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00003017 << "#include \"llvm/Support/CommandLine.h\"\n"
3018 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003019
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003020 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003021 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003022 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003023 << "#include <stdexcept>\n\n"
3024
3025 << "using namespace llvm;\n"
3026 << "using namespace llvmc;\n\n"
3027
Mikhail Glushenkov67665722008-11-12 12:41:18 +00003028 << "extern cl::opt<std::string> OutputFilename;\n\n"
3029
3030 << "inline const char* checkCString(const char* s)\n"
3031 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003032}
3033
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003034
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003035/// PluginData - Holds all information about a plugin.
3036struct PluginData {
3037 OptionDescriptions OptDescs;
3038 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003039 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003040 ToolDescriptions ToolDescs;
3041 RecordVector Edges;
3042 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003043};
3044
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003045/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003046/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003047bool HasSink(const ToolDescriptions& ToolDescs) {
3048 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
3049 E = ToolDescs.end(); B != E; ++B)
3050 if ((*B)->isSink())
3051 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003052
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003053 return false;
3054}
3055
3056/// HasExterns - Go through the list of option descriptions and check
3057/// if there are any external options.
3058bool HasExterns(const OptionDescriptions& OptDescs) {
3059 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
3060 E = OptDescs.end(); B != E; ++B)
3061 if (B->second.isExtern())
3062 return true;
3063
3064 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003065}
3066
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003067/// CollectPluginData - Collect tool and option properties,
3068/// compilation graph edges and plugin priority from the parse tree.
3069void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
3070 // Collect option properties.
3071 const RecordVector& OptionLists =
3072 Records.getAllDerivedDefinitions("OptionList");
3073 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
3074 Data.OptDescs);
3075
3076 // Collect tool properties.
3077 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
3078 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
3079 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003080 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003081
3082 // Collect compilation graph edges.
3083 const RecordVector& CompilationGraphs =
3084 Records.getAllDerivedDefinitions("CompilationGraph");
3085 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
3086 Data.Edges);
3087
3088 // Calculate the priority of this plugin.
3089 const RecordVector& Priorities =
3090 Records.getAllDerivedDefinitions("PluginPriority");
3091 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00003092}
3093
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003094/// CheckPluginData - Perform some sanity checks on the collected data.
3095void CheckPluginData(PluginData& Data) {
3096 // Filter out all tools not mentioned in the compilation graph.
3097 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003098
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003099 // Typecheck the compilation graph.
3100 TypecheckGraph(Data.Edges, Data.ToolDescs);
3101
3102 // Check that there are no options without side effects (specified
3103 // only in the OptionList).
3104 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003105}
3106
Daniel Dunbar1a551802009-07-03 00:10:29 +00003107void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003108 // Emit file header.
3109 EmitIncludes(O);
3110
3111 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00003112 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003113
3114 // Emit hook declarations.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003115 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003116
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003117 O << "namespace {\n\n";
3118
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00003119 // Emit PreprocessOptionsLocal() function.
3120 EmitPreprocessOptions(Records, Data.OptDescs, O);
3121
3122 // Emit PopulateLanguageMapLocal() function
3123 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003124 EmitPopulateLanguageMap(Records, O);
3125
3126 // Emit Tool classes.
3127 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3128 E = Data.ToolDescs.end(); B!=E; ++B)
3129 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3130
3131 // Emit Edge# classes.
3132 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3133
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00003134 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003135 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3136
3137 // Emit code for plugin registration.
3138 EmitRegisterPlugin(Data.Priority, O);
3139
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003140 O << "} // End anonymous namespace.\n\n";
3141
3142 // Force linkage magic.
3143 O << "namespace llvmc {\n";
3144 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
3145 O << "}\n";
3146
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003147 // EOF
3148}
3149
3150
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003151// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003152}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003153
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003154/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003155void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003156 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003157 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003158
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003159 CollectPluginData(Records, Data);
3160 CheckPluginData(Data);
3161
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00003162 this->EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003163 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003164
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003165 } catch (std::exception& Error) {
3166 throw Error.what() + std::string(" - usually this means a syntax error.");
3167 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003168}