blob: 5392d5e1aa0540597198325c054acd923cfd6fe1 [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 Glushenkove0b65702009-12-23 12:49:30 +000020
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000021#include <algorithm>
22#include <cassert>
23#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000024#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000025#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000026#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000027
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000028using namespace llvm;
29
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000030namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
32//===----------------------------------------------------------------------===//
33/// Typedefs
34
35typedef std::vector<Record*> RecordVector;
36typedef std::vector<std::string> StrVector;
37
38//===----------------------------------------------------------------------===//
39/// Constants
40
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +000041// Indentation.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000042const unsigned TabWidth = 4;
43const unsigned Indent1 = TabWidth*1;
44const unsigned Indent2 = TabWidth*2;
45const unsigned Indent3 = TabWidth*3;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +000046const unsigned Indent4 = TabWidth*4;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000047
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000048// Default help string.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000049const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000050
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000051// Name for the "sink" option.
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +000052const char * const SinkOptionName = "AutoGeneratedSinkOption";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000053
54//===----------------------------------------------------------------------===//
55/// Helper functions
56
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000057/// Id - An 'identity' function object.
58struct Id {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000059 template<typename T0>
60 void operator()(const T0&) const {
61 }
62 template<typename T0, typename T1>
63 void operator()(const T0&, const T1&) const {
64 }
65 template<typename T0, typename T1, typename T2>
66 void operator()(const T0&, const T1&, const T2&) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000067 }
68};
69
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000070int InitPtrToInt(const Init* ptr) {
71 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000072 return val.getValue();
73}
74
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000075const std::string& InitPtrToString(const Init* ptr) {
76 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
77 return val.getValue();
78}
79
80const ListInit& InitPtrToList(const Init* ptr) {
81 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
82 return val;
83}
84
85const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000086 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000087 return val;
88}
89
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000090const std::string GetOperatorName(const DagInit& D) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +000091 return D.getOperator()->getAsString();
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +000092}
93
Mikhail Glushenkove0b65702009-12-23 12:49:30 +000094/// CheckBooleanConstant - Check that the provided value is a boolean constant.
95void CheckBooleanConstant(const Init* I) {
96 const DefInit& val = dynamic_cast<const DefInit&>(*I);
97 const std::string& str = val.getAsString();
98
99 if (str != "true" && str != "false") {
100 throw "Incorrect boolean value: '" + str +
101 "': must be either 'true' or 'false'";
102 }
103}
104
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000105// CheckNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +0000106// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000107void CheckNumberOfArguments (const DagInit& d, unsigned minArgs) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000108 if (d.getNumArgs() < minArgs)
109 throw GetOperatorName(d) + ": too few arguments!";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +0000110}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000111
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000112// IsDagEmpty - is this DAG marked with an empty marker?
113bool IsDagEmpty (const DagInit& d) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000114 return GetOperatorName(d) == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +0000115}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000116
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000117// EscapeVariableName - Escape commas and other symbols not allowed
118// in the C++ variable names. Makes it possible to use options named
119// like "Wa," (useful for prefix options).
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000120std::string EscapeVariableName (const std::string& Var) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000121 std::string ret;
122 for (unsigned i = 0; i != Var.size(); ++i) {
123 char cur_char = Var[i];
124 if (cur_char == ',') {
125 ret += "_comma_";
126 }
127 else if (cur_char == '+') {
128 ret += "_plus_";
129 }
130 else if (cur_char == '-') {
131 ret += "_dash_";
132 }
133 else {
134 ret.push_back(cur_char);
135 }
136 }
137 return ret;
138}
139
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000140/// EscapeQuotes - Replace '"' with '\"'.
141std::string EscapeQuotes (const std::string& Var) {
142 std::string ret;
143 for (unsigned i = 0; i != Var.size(); ++i) {
144 char cur_char = Var[i];
145 if (cur_char == '"') {
146 ret += "\\\"";
147 }
148 else {
149 ret.push_back(cur_char);
150 }
151 }
152 return ret;
153}
154
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000155/// OneOf - Does the input string contain this character?
156bool OneOf(const char* lst, char c) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000157 while (*lst) {
158 if (*lst++ == c)
159 return true;
160 }
161 return false;
162}
163
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000164template <class I, class S>
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000165void CheckedIncrement(I& P, I E, S ErrorString) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000166 ++P;
167 if (P == E)
168 throw ErrorString;
169}
170
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000171// apply is needed because C++'s syntax doesn't let us construct a function
172// object and call it in the same statement.
173template<typename F, typename T0>
174void apply(F Fun, T0& Arg0) {
175 return Fun(Arg0);
176}
177
178template<typename F, typename T0, typename T1>
179void apply(F Fun, T0& Arg0, T1& Arg1) {
180 return Fun(Arg0, Arg1);
181}
182
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000183//===----------------------------------------------------------------------===//
184/// Back-end specific code
185
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000186
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000187/// OptionType - One of six different option types. See the
188/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000189namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000190
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000191 enum OptionType { Alias, Switch, SwitchList,
192 Parameter, ParameterList, Prefix, PrefixList };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000193
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000194 bool IsAlias(OptionType t) {
195 return (t == Alias);
196 }
197
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000198 bool IsList (OptionType t) {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000199 return (t == SwitchList || t == ParameterList || t == PrefixList);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000200 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000201
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000202 bool IsSwitch (OptionType t) {
203 return (t == Switch);
204 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000205
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000206 bool IsSwitchList (OptionType t) {
207 return (t == SwitchList);
208 }
209
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000210 bool IsParameter (OptionType t) {
211 return (t == Parameter || t == Prefix);
212 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000213
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000214}
215
216OptionType::OptionType stringToOptionType(const std::string& T) {
217 if (T == "alias_option")
218 return OptionType::Alias;
219 else if (T == "switch_option")
220 return OptionType::Switch;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000221 else if (T == "switch_list_option")
222 return OptionType::SwitchList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000223 else if (T == "parameter_option")
224 return OptionType::Parameter;
225 else if (T == "parameter_list_option")
226 return OptionType::ParameterList;
227 else if (T == "prefix_option")
228 return OptionType::Prefix;
229 else if (T == "prefix_list_option")
230 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000231 else
232 throw "Unknown option type: " + T + '!';
233}
234
235namespace OptionDescriptionFlags {
236 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000237 ReallyHidden = 0x4, Extern = 0x8,
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000238 OneOrMore = 0x10, Optional = 0x20,
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000239 CommaSeparated = 0x40, ForwardNotSplit = 0x80,
240 ZeroOrMore = 0x100 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000241}
242
243/// OptionDescription - Represents data contained in a single
244/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000245struct OptionDescription {
246 OptionType::OptionType Type;
247 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000248 unsigned Flags;
249 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000250 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000251 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000252
253 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000254 const std::string& n = "",
255 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000256 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000257 {}
258
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000259 /// GenTypeDeclaration - Returns the C++ variable type of this
260 /// option.
261 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000262
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000263 /// GenVariableName - Returns the variable name used in the
264 /// generated C++ code.
265 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000266
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000267 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000268 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000269
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000270 /// CheckConsistency - Check that the flags are consistent.
271 void CheckConsistency() const;
272
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000273 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000274
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000275 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000276
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000277 bool isMultiVal() const;
278
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000279 bool isCommaSeparated() const;
280 void setCommaSeparated();
281
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000282 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000283 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000284
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000285 bool isForwardNotSplit() const;
286 void setForwardNotSplit();
287
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000288 bool isRequired() const;
289 void setRequired();
290
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000291 bool isOneOrMore() const;
292 void setOneOrMore();
293
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000294 bool isZeroOrMore() const;
295 void setZeroOrMore();
296
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000297 bool isOptional() const;
298 void setOptional();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000299
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000300 bool isHidden() const;
301 void setHidden();
302
303 bool isReallyHidden() const;
304 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000305
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000306 bool isSwitch() const
307 { return OptionType::IsSwitch(this->Type); }
308
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000309 bool isSwitchList() const
310 { return OptionType::IsSwitchList(this->Type); }
311
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000312 bool isParameter() const
313 { return OptionType::IsParameter(this->Type); }
314
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000315 bool isList() const
316 { return OptionType::IsList(this->Type); }
317
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000318 bool isParameterList() const
319 { return (OptionType::IsList(this->Type)
320 && !OptionType::IsSwitchList(this->Type)); }
321
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000322};
323
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000324void OptionDescription::CheckConsistency() const {
325 unsigned i = 0;
326
327 i += this->isRequired();
328 i += this->isOptional();
329 i += this->isOneOrMore();
330 i += this->isZeroOrMore();
331
332 if (i > 1) {
333 throw "Only one of (required), (optional), (one_or_more) or "
334 "(zero_or_more) properties is allowed!";
335 }
336}
337
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000338void OptionDescription::Merge (const OptionDescription& other)
339{
340 if (other.Type != Type)
341 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000342
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000343 if (Help == other.Help || Help == DefaultHelpString)
344 Help = other.Help;
345 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000346 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000347 " defined for option " + Name + "\n";
348 }
349
350 Flags |= other.Flags;
351}
352
353bool OptionDescription::isAlias() const {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000354 return OptionType::IsAlias(this->Type);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000355}
356
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000357bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000358 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000359}
360
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000361bool OptionDescription::isCommaSeparated() const {
362 return Flags & OptionDescriptionFlags::CommaSeparated;
363}
364void OptionDescription::setCommaSeparated() {
365 Flags |= OptionDescriptionFlags::CommaSeparated;
366}
367
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000368bool OptionDescription::isForwardNotSplit() const {
369 return Flags & OptionDescriptionFlags::ForwardNotSplit;
370}
371void OptionDescription::setForwardNotSplit() {
372 Flags |= OptionDescriptionFlags::ForwardNotSplit;
373}
374
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000375bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000376 return Flags & OptionDescriptionFlags::Extern;
377}
378void OptionDescription::setExtern() {
379 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000380}
381
382bool OptionDescription::isRequired() const {
383 return Flags & OptionDescriptionFlags::Required;
384}
385void OptionDescription::setRequired() {
386 Flags |= OptionDescriptionFlags::Required;
387}
388
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000389bool OptionDescription::isOneOrMore() const {
390 return Flags & OptionDescriptionFlags::OneOrMore;
391}
392void OptionDescription::setOneOrMore() {
393 Flags |= OptionDescriptionFlags::OneOrMore;
394}
395
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000396bool OptionDescription::isZeroOrMore() const {
397 return Flags & OptionDescriptionFlags::ZeroOrMore;
398}
399void OptionDescription::setZeroOrMore() {
400 Flags |= OptionDescriptionFlags::ZeroOrMore;
401}
402
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000403bool OptionDescription::isOptional() const {
404 return Flags & OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000405}
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000406void OptionDescription::setOptional() {
407 Flags |= OptionDescriptionFlags::Optional;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000408}
409
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000410bool OptionDescription::isHidden() const {
411 return Flags & OptionDescriptionFlags::Hidden;
412}
413void OptionDescription::setHidden() {
414 Flags |= OptionDescriptionFlags::Hidden;
415}
416
417bool OptionDescription::isReallyHidden() const {
418 return Flags & OptionDescriptionFlags::ReallyHidden;
419}
420void OptionDescription::setReallyHidden() {
421 Flags |= OptionDescriptionFlags::ReallyHidden;
422}
423
424const char* OptionDescription::GenTypeDeclaration() const {
425 switch (Type) {
426 case OptionType::Alias:
427 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000428 case OptionType::PrefixList:
429 case OptionType::ParameterList:
430 return "cl::list<std::string>";
431 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000432 return "cl::opt<bool>";
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000433 case OptionType::SwitchList:
434 return "cl::list<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000435 case OptionType::Parameter:
436 case OptionType::Prefix:
437 default:
438 return "cl::opt<std::string>";
439 }
440}
441
442std::string OptionDescription::GenVariableName() const {
443 const std::string& EscapedName = EscapeVariableName(Name);
444 switch (Type) {
445 case OptionType::Alias:
446 return "AutoGeneratedAlias_" + EscapedName;
447 case OptionType::PrefixList:
448 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000449 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000450 case OptionType::Switch:
451 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000452 case OptionType::SwitchList:
453 return "AutoGeneratedSwitchList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000454 case OptionType::Prefix:
455 case OptionType::Parameter:
456 default:
457 return "AutoGeneratedParameter_" + EscapedName;
458 }
459}
460
461/// OptionDescriptions - An OptionDescription array plus some helper
462/// functions.
463class OptionDescriptions {
464 typedef StringMap<OptionDescription> container_type;
465
466 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000467 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000468
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000469public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000470 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000471 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000472
473 // Wrappers for FindOption that throw an exception in case the option has a
474 // wrong type.
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000475 const OptionDescription& FindSwitch(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000476 const OptionDescription& FindParameter(const std::string& OptName) const;
477 const OptionDescription& FindList(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000478 const OptionDescription& FindParameterList(const std::string& OptName) const;
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000479 const OptionDescription&
480 FindListOrParameter(const std::string& OptName) const;
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000481 const OptionDescription&
482 FindParameterListOrParameter(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000483
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000484 /// insertDescription - Insert new OptionDescription into
485 /// OptionDescriptions list
486 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000487
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000488 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000489 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000490 const_iterator begin() const { return Descriptions.begin(); }
491 const_iterator end() const { return Descriptions.end(); }
492};
493
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000494const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000495OptionDescriptions::FindOption(const std::string& OptName) const {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000496 const_iterator I = Descriptions.find(OptName);
497 if (I != Descriptions.end())
498 return I->second;
499 else
500 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000501}
502
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000503const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000504OptionDescriptions::FindSwitch(const std::string& OptName) const {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000505 const OptionDescription& OptDesc = this->FindOption(OptName);
506 if (!OptDesc.isSwitch())
507 throw OptName + ": incorrect option type - should be a switch!";
508 return OptDesc;
509}
510
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000511const OptionDescription&
512OptionDescriptions::FindList(const std::string& OptName) const {
513 const OptionDescription& OptDesc = this->FindOption(OptName);
514 if (!OptDesc.isList())
515 throw OptName + ": incorrect option type - should be a list!";
516 return OptDesc;
517}
518
519const OptionDescription&
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000520OptionDescriptions::FindParameterList(const std::string& OptName) const {
521 const OptionDescription& OptDesc = this->FindOption(OptName);
522 if (!OptDesc.isList() || OptDesc.isSwitchList())
523 throw OptName + ": incorrect option type - should be a parameter list!";
524 return OptDesc;
525}
526
527const OptionDescription&
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000528OptionDescriptions::FindParameter(const std::string& OptName) const {
529 const OptionDescription& OptDesc = this->FindOption(OptName);
530 if (!OptDesc.isParameter())
531 throw OptName + ": incorrect option type - should be a parameter!";
532 return OptDesc;
533}
534
535const OptionDescription&
536OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
537 const OptionDescription& OptDesc = this->FindOption(OptName);
538 if (!OptDesc.isList() && !OptDesc.isParameter())
539 throw OptName
540 + ": incorrect option type - should be a list or parameter!";
541 return OptDesc;
542}
543
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000544const OptionDescription&
545OptionDescriptions::FindParameterListOrParameter
546(const std::string& OptName) const {
547 const OptionDescription& OptDesc = this->FindOption(OptName);
548 if ((!OptDesc.isList() && !OptDesc.isParameter()) || OptDesc.isSwitchList())
549 throw OptName
550 + ": incorrect option type - should be a parameter list or parameter!";
551 return OptDesc;
552}
553
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +0000554void OptionDescriptions::InsertDescription (const OptionDescription& o) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000555 container_type::iterator I = Descriptions.find(o.Name);
556 if (I != Descriptions.end()) {
557 OptionDescription& D = I->second;
558 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000559 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000560 else {
561 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000562 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000563}
564
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000565/// HandlerTable - A base class for function objects implemented as
566/// 'tables of handlers'.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000567template <typename Handler>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000568class HandlerTable {
569protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000570 // Implementation details.
571
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000572 /// HandlerMap - A map from property names to property handlers
573 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000574
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000575 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000576 static bool staticMembersInitialized_;
577
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000578public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000579
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000580 Handler GetHandler (const std::string& HandlerName) const {
581 typename HandlerMap::iterator method = Handlers_.find(HandlerName);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000582
583 if (method != Handlers_.end()) {
584 Handler h = method->second;
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000585 return h;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000586 }
587 else {
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000588 throw "No handler found for property " + HandlerName + "!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000589 }
590 }
591
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000592 void AddHandler(const char* Property, Handler H) {
593 Handlers_[Property] = H;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000594 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000595
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000596};
597
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000598template <class Handler, class FunctionObject>
599Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
600 const std::string& HandlerName = GetOperatorName(Dag);
601 return Obj->GetHandler(HandlerName);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000602}
603
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000604template <class FunctionObject>
605void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
606 typedef void (FunctionObject::*Handler) (const DagInit&);
607
608 const DagInit& Dag = InitPtrToDag(I);
609 Handler h = GetHandler<Handler>(Obj, Dag);
610
611 ((Obj)->*(h))(Dag);
612}
613
614template <class FunctionObject>
615void InvokeDagInitHandler(const FunctionObject* const Obj,
616 const Init* I, unsigned IndentLevel, raw_ostream& O)
617{
618 typedef void (FunctionObject::*Handler)
619 (const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
620
621 const DagInit& Dag = InitPtrToDag(I);
622 Handler h = GetHandler<Handler>(Obj, Dag);
623
624 ((Obj)->*(h))(Dag, IndentLevel, O);
625}
626
627
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000628template <typename H>
629typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
630
631template <typename H>
632bool HandlerTable<H>::staticMembersInitialized_ = false;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000633
634
635/// CollectOptionProperties - Function object for iterating over an
636/// option property list.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000637class CollectOptionProperties;
638typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000639(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000640
641class CollectOptionProperties
642: public HandlerTable<CollectOptionPropertiesHandler>
643{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000644private:
645
646 /// optDescs_ - OptionDescriptions table. This is where the
647 /// information is stored.
648 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000649
650public:
651
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000652 explicit CollectOptionProperties(OptionDescription& OD)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000653 : optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000654 {
655 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000656 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000657 AddHandler("help", &CollectOptionProperties::onHelp);
658 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000659 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000660 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
661 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000662 AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000663 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
664 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000665 AddHandler("optional", &CollectOptionProperties::onOptional);
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000666 AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000667 AddHandler("forward_not_split",
668 &CollectOptionProperties::onForwardNotSplit);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000669
670 staticMembersInitialized_ = true;
671 }
672 }
673
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000674 /// operator() - Just forwards to the corresponding property
675 /// handler.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000676 void operator() (Init* I) {
677 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000678 }
679
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000680private:
681
682 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000683 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000684
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000685 void onExtern (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000686 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000687 optDesc_.setExtern();
688 }
689
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000690 void onHelp (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000691 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovae779382010-01-26 14:55:04 +0000692 optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000693 }
694
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000695 void onHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000696 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000697 optDesc_.setHidden();
698 }
699
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000700 void onReallyHidden (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000701 CheckNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000702 optDesc_.setReallyHidden();
703 }
704
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000705 void onCommaSeparated (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000706 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000707 if (!optDesc_.isParameterList())
708 throw "'comma_separated' is valid only on parameter list options!";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000709 optDesc_.setCommaSeparated();
710 }
711
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +0000712 void onForwardNotSplit (const DagInit& d) {
713 CheckNumberOfArguments(d, 0);
714 if (!optDesc_.isParameter())
715 throw "'forward_not_split' is valid only for parameter options!";
716 optDesc_.setForwardNotSplit();
717 }
718
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000719 void onRequired (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000720 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000721
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000722 optDesc_.setRequired();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000723 optDesc_.CheckConsistency();
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000724 }
725
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000726 void onInit (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000727 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000728 Init* i = d.getArg(0);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000729 const std::string& str = i->getAsString();
730
731 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
732 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
733
734 if (!correct)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000735 throw "Incorrect usage of the 'init' option property!";
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000736
737 optDesc_.InitVal = i;
738 }
739
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000740 void onOneOrMore (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000741 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000742
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000743 optDesc_.setOneOrMore();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000744 optDesc_.CheckConsistency();
745 }
746
747 void onZeroOrMore (const DagInit& d) {
748 CheckNumberOfArguments(d, 0);
749
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000750 if (optDesc_.isList())
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000751 llvm::errs() << "Warning: specifying the 'zero_or_more' property "
752 "on a list option has no effect.\n";
753
754 optDesc_.setZeroOrMore();
755 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000756 }
757
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000758 void onOptional (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000759 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000760
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +0000761 if (!optDesc_.isList())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000762 llvm::errs() << "Warning: specifying the 'optional' property"
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000763 "on a non-list option has no effect.\n";
764
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +0000765 optDesc_.setOptional();
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +0000766 optDesc_.CheckConsistency();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000767 }
768
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000769 void onMultiVal (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000770 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000771 int val = InitPtrToInt(d.getArg(0));
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000772 if (val < 2)
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000773 throw "Error in the 'multi_val' property: "
774 "the value must be greater than 1!";
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +0000775 if (!optDesc_.isParameterList())
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +0000776 throw "The multi_val property is valid only on list options!";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000777 optDesc_.MultiVal = val;
778 }
779
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000780};
781
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000782/// AddOption - A function object that is applied to every option
783/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000784class AddOption {
785private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000786 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000787
788public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000789 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000790 {}
791
792 void operator()(const Init* i) {
793 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000794 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000795
796 const OptionType::OptionType Type =
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +0000797 stringToOptionType(GetOperatorName(d));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000798 const std::string& Name = InitPtrToString(d.getArg(0));
799
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000800 OptionDescription OD(Type, Name);
801
802 if (!OD.isExtern())
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000803 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000804
805 if (OD.isAlias()) {
806 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000807 OD.Help = InitPtrToString(d.getArg(1));
808 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000809 else if (!OD.isExtern()) {
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000810 processOptionProperties(d, OD);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000811 }
812 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000813 }
814
815private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000816 /// processOptionProperties - Go through the list of option
817 /// properties and call a corresponding handler for each.
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000818 static void processOptionProperties (const DagInit& d, OptionDescription& o) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000819 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000820 DagInit::const_arg_iterator B = d.arg_begin();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000821 // Skip the first argument: it's always the option name.
822 ++B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000823 std::for_each(B, d.arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000824 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000825
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000826};
827
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000828/// CollectOptionDescriptions - Collects option properties from all
829/// OptionLists.
830void CollectOptionDescriptions (RecordVector::const_iterator B,
831 RecordVector::const_iterator E,
832 OptionDescriptions& OptDescs)
833{
834 // For every OptionList:
835 for (; B!=E; ++B) {
836 RecordVector::value_type T = *B;
837 // Throws an exception if the value does not exist.
838 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000839
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000840 // For every option description in this list:
841 // collect the information and
842 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
843 }
844}
845
846// Tool information record
847
848namespace ToolFlags {
849 enum ToolFlags { Join = 0x1, Sink = 0x2 };
850}
851
852struct ToolDescription : public RefCountedBase<ToolDescription> {
853 std::string Name;
854 Init* CmdLine;
855 Init* Actions;
856 StrVector InLanguage;
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000857 std::string InFileOption;
858 std::string OutFileOption;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000859 std::string OutLanguage;
860 std::string OutputSuffix;
861 unsigned Flags;
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000862 const Init* OnEmpty;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000863
864 // Various boolean properties
865 void setSink() { Flags |= ToolFlags::Sink; }
866 bool isSink() const { return Flags & ToolFlags::Sink; }
867 void setJoin() { Flags |= ToolFlags::Join; }
868 bool isJoin() const { return Flags & ToolFlags::Join; }
869
870 // Default ctor here is needed because StringMap can only store
871 // DefaultConstructible objects
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000872 ToolDescription ()
Mikhail Glushenkovc4301292010-02-23 09:05:01 +0000873 : CmdLine(0), Actions(0), OutFileOption("-o"),
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000874 Flags(0), OnEmpty(0)
875 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000876 ToolDescription (const std::string& n)
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000877 : Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
878 Flags(0), OnEmpty(0)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000879 {}
880};
881
882/// ToolDescriptions - A list of Tool information records.
883typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
884
885
886/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000887/// tool property records.
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000888
889class CollectToolProperties;
890typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000891(const DagInit&);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000892
893class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
894{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000895private:
896
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000897 /// toolDesc_ - Properties of the current Tool. This is where the
898 /// information is stored.
899 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000900
901public:
902
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000903 explicit CollectToolProperties (ToolDescription& d)
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000904 : toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000905 {
906 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000907
908 AddHandler("actions", &CollectToolProperties::onActions);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000909 AddHandler("command", &CollectToolProperties::onCommand);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000910 AddHandler("in_language", &CollectToolProperties::onInLanguage);
911 AddHandler("join", &CollectToolProperties::onJoin);
912 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000913
914 AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
915 AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
916
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000917 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
918 AddHandler("sink", &CollectToolProperties::onSink);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000919 AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000920
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000921 staticMembersInitialized_ = true;
922 }
923 }
924
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000925 void operator() (Init* I) {
926 InvokeDagInitHandler(this, I);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +0000927 }
928
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000929private:
930
931 /// Property handlers --
932 /// Functions that extract information about tool properties from
933 /// DAG representation.
934
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000935 void onActions (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000936 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000937 Init* Case = d.getArg(0);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000938 if (typeid(*Case) != typeid(DagInit) ||
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000939 GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +0000940 throw "The argument to (actions) should be a 'case' construct!";
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000941 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000942 }
943
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000944 void onCommand (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000945 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000946 toolDesc_.CmdLine = d.getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000947 }
948
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000949 void onInLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000950 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000951 Init* arg = d.getArg(0);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000952
953 // Find out the argument's type.
954 if (typeid(*arg) == typeid(StringInit)) {
955 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000956 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000957 }
958 else {
959 // It's a list.
960 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000961 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000962
963 // Copy strings to the output vector.
964 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
965 B != E; ++B) {
966 out.push_back(InitPtrToString(*B));
967 }
968
969 // Remove duplicates.
970 std::sort(out.begin(), out.end());
971 StrVector::iterator newE = std::unique(out.begin(), out.end());
972 out.erase(newE, out.end());
973 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000974 }
975
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000976 void onJoin (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000977 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000978 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000979 }
980
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000981 void onOutLanguage (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000982 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000983 toolDesc_.OutLanguage = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000984 }
985
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +0000986 void onOutFileOption (const DagInit& d) {
987 CheckNumberOfArguments(d, 1);
988 toolDesc_.OutFileOption = InitPtrToString(d.getArg(0));
989 }
990
991 void onInFileOption (const DagInit& d) {
992 CheckNumberOfArguments(d, 1);
993 toolDesc_.InFileOption = InitPtrToString(d.getArg(0));
994 }
995
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000996 void onOutputSuffix (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +0000997 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov24723282009-12-17 07:48:49 +0000998 toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000999 }
1000
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001001 void onSink (const DagInit& d) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001002 CheckNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001003 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001004 }
1005
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001006 void onWorksOnEmpty (const DagInit& d) {
1007 toolDesc_.OnEmpty = d.getArg(0);
1008 }
1009
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001010};
1011
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001012/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001013/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001014/// CollectToolProperties function object).
1015void CollectToolDescriptions (RecordVector::const_iterator B,
1016 RecordVector::const_iterator E,
1017 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001018{
1019 // Iterate over a properties list of every Tool definition
1020 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001021 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001022 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001023 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001024
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001025 IntrusiveRefCntPtr<ToolDescription>
1026 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001027
1028 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001029 CollectToolProperties(*ToolDesc));
1030 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001031 }
1032}
1033
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001034/// FillInEdgeVector - Merge all compilation graph definitions into
1035/// one single edge list.
1036void FillInEdgeVector(RecordVector::const_iterator B,
1037 RecordVector::const_iterator E, RecordVector& Out) {
1038 for (; B != E; ++B) {
1039 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +00001040
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001041 for (unsigned i = 0; i < edges->size(); ++i)
1042 Out.push_back(edges->getElementAsRecord(i));
1043 }
1044}
1045
1046/// CalculatePriority - Calculate the priority of this plugin.
1047int CalculatePriority(RecordVector::const_iterator B,
1048 RecordVector::const_iterator E) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +00001049 int priority = 0;
1050
1051 if (B != E) {
1052 priority = static_cast<int>((*B)->getValueAsInt("priority"));
1053
1054 if (++B != E)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001055 throw "More than one 'PluginPriority' instance found: "
1056 "most probably an error!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001057 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +00001058
1059 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001060}
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001061
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001062/// NotInGraph - Helper function object for FilterNotInGraph.
1063struct NotInGraph {
1064private:
1065 const llvm::StringSet<>& ToolsInGraph_;
1066
1067public:
1068 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
1069 : ToolsInGraph_(ToolsInGraph)
1070 {}
1071
1072 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
1073 return (ToolsInGraph_.count(x->Name) == 0);
1074 }
1075};
1076
1077/// FilterNotInGraph - Filter out from ToolDescs all Tools not
1078/// mentioned in the compilation graph definition.
1079void FilterNotInGraph (const RecordVector& EdgeVector,
1080 ToolDescriptions& ToolDescs) {
1081
1082 // List all tools mentioned in the graph.
1083 llvm::StringSet<> ToolsInGraph;
1084
1085 for (RecordVector::const_iterator B = EdgeVector.begin(),
1086 E = EdgeVector.end(); B != E; ++B) {
1087
1088 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001089 const std::string& NodeA = Edge->getValueAsString("a");
1090 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001091
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001092 if (NodeA != "root")
1093 ToolsInGraph.insert(NodeA);
1094 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001095 }
1096
1097 // Filter ToolPropertiesList.
1098 ToolDescriptions::iterator new_end =
1099 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
1100 NotInGraph(ToolsInGraph));
1101 ToolDescs.erase(new_end, ToolDescs.end());
1102}
1103
1104/// FillInToolToLang - Fills in two tables that map tool names to
1105/// (input, output) languages. Helper function used by TypecheckGraph().
1106void FillInToolToLang (const ToolDescriptions& ToolDescs,
1107 StringMap<StringSet<> >& ToolToInLang,
1108 StringMap<std::string>& ToolToOutLang) {
1109 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1110 E = ToolDescs.end(); B != E; ++B) {
1111 const ToolDescription& D = *(*B);
1112 for (StrVector::const_iterator B = D.InLanguage.begin(),
1113 E = D.InLanguage.end(); B != E; ++B)
1114 ToolToInLang[D.Name].insert(*B);
1115 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +00001116 }
1117}
1118
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001119/// TypecheckGraph - Check that names for output and input languages
1120/// on all edges do match. This doesn't do much when the information
1121/// about the whole graph is not available (i.e. when compiling most
1122/// plugins).
1123void TypecheckGraph (const RecordVector& EdgeVector,
1124 const ToolDescriptions& ToolDescs) {
1125 StringMap<StringSet<> > ToolToInLang;
1126 StringMap<std::string> ToolToOutLang;
1127
1128 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
1129 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1130 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
1131
1132 for (RecordVector::const_iterator B = EdgeVector.begin(),
1133 E = EdgeVector.end(); B != E; ++B) {
1134 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001135 const std::string& NodeA = Edge->getValueAsString("a");
1136 const std::string& NodeB = Edge->getValueAsString("b");
1137 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
1138 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001139
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001140 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001141 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001142 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001143 + ": output->input language mismatch";
1144 }
1145
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001146 if (NodeB == "root")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001147 throw "Edges back to the root are not allowed!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001148 }
1149}
1150
1151/// WalkCase - Walks the 'case' expression DAG and invokes
1152/// TestCallback on every test, and StatementCallback on every
1153/// statement. Handles 'case' nesting, but not the 'and' and 'or'
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001154/// combinators (that is, they are passed directly to TestCallback).
1155/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
1156/// IndentLevel, bool FirstTest)'.
1157/// StatementCallback must have type 'void StatementCallback(const Init*,
1158/// unsigned IndentLevel)'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001159template <typename F1, typename F2>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001160void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
1161 unsigned IndentLevel = 0)
1162{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001163 const DagInit& d = InitPtrToDag(Case);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001164
1165 // Error checks.
1166 if (GetOperatorName(d) != "case")
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001167 throw "WalkCase should be invoked only on 'case' expressions!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001168
1169 if (d.getNumArgs() < 2)
1170 throw "There should be at least one clause in the 'case' expression:\n"
1171 + d.getAsString();
1172
1173 // Main loop.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001174 bool even = false;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001175 const unsigned numArgs = d.getNumArgs();
1176 unsigned i = 1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001177 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1178 B != E; ++B) {
1179 Init* arg = *B;
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001180
1181 if (!even)
1182 {
1183 // Handle test.
1184 const DagInit& Test = InitPtrToDag(arg);
1185
1186 if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001187 throw "The 'default' clause should be the last in the "
1188 "'case' construct!";
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001189 if (i == numArgs)
1190 throw "Case construct handler: no corresponding action "
1191 "found for the test " + Test.getAsString() + '!';
1192
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001193 TestCallback(Test, IndentLevel, (i == 1));
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001194 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001195 else
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001196 {
1197 if (dynamic_cast<DagInit*>(arg)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001198 && GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001199 // Nested 'case'.
1200 WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
1201 }
1202
1203 // Handle statement.
1204 StatementCallback(arg, IndentLevel);
1205 }
1206
1207 ++i;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001208 even = !even;
1209 }
1210}
1211
1212/// ExtractOptionNames - A helper function object used by
1213/// CheckForSuperfluousOptions() to walk the 'case' DAG.
1214class ExtractOptionNames {
1215 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001216
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001217 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001218 const DagInit& Stmt = InitPtrToDag(Statement);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001219 const std::string& ActionName = GetOperatorName(Stmt);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001220 if (ActionName == "forward" || ActionName == "forward_as" ||
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00001221 ActionName == "forward_value" ||
1222 ActionName == "forward_transformed_value" ||
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001223 ActionName == "switch_on" || ActionName == "any_switch_on" ||
1224 ActionName == "parameter_equals" ||
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00001225 ActionName == "element_in_list" || ActionName == "not_empty" ||
1226 ActionName == "empty") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001227 CheckNumberOfArguments(Stmt, 1);
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001228
1229 Init* Arg = Stmt.getArg(0);
1230 if (typeid(*Arg) == typeid(StringInit)) {
1231 const std::string& Name = InitPtrToString(Arg);
1232 OptionNames_.insert(Name);
1233 }
1234 else {
1235 // It's a list.
1236 const ListInit& List = InitPtrToList(Arg);
1237 for (ListInit::const_iterator B = List.begin(), E = List.end();
1238 B != E; ++B) {
1239 const std::string& Name = InitPtrToString(*B);
1240 OptionNames_.insert(Name);
1241 }
1242 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001243 }
Mikhail Glushenkovd64c9072010-01-01 03:51:02 +00001244 else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001245 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001246 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001247 }
1248 }
1249 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001250
1251public:
1252 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
1253 {}
1254
1255 void operator()(const Init* Statement) {
1256 if (typeid(*Statement) == typeid(ListInit)) {
1257 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1258 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1259 B != E; ++B)
1260 this->processDag(*B);
1261 }
1262 else {
1263 this->processDag(Statement);
1264 }
1265 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001266
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001267 void operator()(const DagInit& Test, unsigned, bool) {
1268 this->operator()(&Test);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001269 }
1270 void operator()(const Init* Statement, unsigned) {
1271 this->operator()(Statement);
1272 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001273};
1274
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001275/// CheckForSuperfluousOptions - Check that there are no side
1276/// effect-free options (specified only in the OptionList). Otherwise,
1277/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001278void CheckForSuperfluousOptions (const RecordVector& Edges,
1279 const ToolDescriptions& ToolDescs,
1280 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001281 llvm::StringSet<> nonSuperfluousOptions;
1282
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001283 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001284 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001285 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1286 E = ToolDescs.end(); B != E; ++B) {
1287 const ToolDescription& TD = *(*B);
1288 ExtractOptionNames Callback(nonSuperfluousOptions);
1289 if (TD.Actions)
1290 WalkCase(TD.Actions, Callback, Callback);
1291 }
1292
1293 // Add all options mentioned in the 'case' clauses of the
1294 // OptionalEdges of the compilation graph to the set of
1295 // non-superfluous options.
1296 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
1297 B != E; ++B) {
1298 const Record* Edge = *B;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001299 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001300
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001301 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001302 WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001303 }
1304
1305 // Check that all options in OptDescs belong to the set of
1306 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001307 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001308 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001309 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001310 if (!nonSuperfluousOptions.count(Val.Name)
1311 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +00001312 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +00001313 "Probable cause: this option is specified only in the OptionList.\n";
1314 }
1315}
1316
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001317/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
1318bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
1319 if (TestName == "single_input_file") {
1320 O << "InputFilenames.size() == 1";
1321 return true;
1322 }
1323 else if (TestName == "multiple_input_files") {
1324 O << "InputFilenames.size() > 1";
1325 return true;
1326 }
1327
1328 return false;
1329}
1330
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001331/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
1332template <typename F>
1333void EmitListTest(const ListInit& L, const char* LogicOp,
1334 F Callback, raw_ostream& O)
1335{
1336 // This is a lot like EmitLogicalOperationTest, but works on ListInits instead
1337 // of Dags...
1338 bool isFirst = true;
1339 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
1340 if (isFirst)
1341 isFirst = false;
1342 else
Mikhail Glushenkovb7935e02010-01-01 04:40:54 +00001343 O << ' ' << LogicOp << ' ';
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001344 Callback(InitPtrToString(*B), O);
1345 }
1346}
1347
1348// Callbacks for use with EmitListTest.
1349
1350class EmitSwitchOn {
1351 const OptionDescriptions& OptDescs_;
1352public:
1353 EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
1354 {}
1355
1356 void operator()(const std::string& OptName, raw_ostream& O) const {
1357 const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
1358 O << OptDesc.GenVariableName();
1359 }
1360};
1361
1362class EmitEmptyTest {
1363 bool EmitNegate_;
1364 const OptionDescriptions& OptDescs_;
1365public:
1366 EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
1367 : EmitNegate_(EmitNegate), OptDescs_(OptDescs)
1368 {}
1369
1370 void operator()(const std::string& OptName, raw_ostream& O) const {
1371 const char* Neg = (EmitNegate_ ? "!" : "");
1372 if (OptName == "o") {
1373 O << Neg << "OutputFilename.empty()";
1374 }
Mikhail Glushenkov97955002009-12-01 06:51:30 +00001375 else if (OptName == "save-temps") {
1376 O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
1377 }
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001378 else {
1379 const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
1380 O << Neg << OptDesc.GenVariableName() << ".empty()";
1381 }
1382 }
1383};
1384
1385
1386/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
1387bool EmitCaseTest1ArgList(const std::string& TestName,
1388 const DagInit& d,
1389 const OptionDescriptions& OptDescs,
1390 raw_ostream& O) {
Mikhail Glushenkov3a481e32010-01-01 03:50:51 +00001391 const ListInit& L = InitPtrToList(d.getArg(0));
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001392
1393 if (TestName == "any_switch_on") {
1394 EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
1395 return true;
1396 }
1397 else if (TestName == "switch_on") {
1398 EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
1399 return true;
1400 }
1401 else if (TestName == "any_not_empty") {
1402 EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
1403 return true;
1404 }
1405 else if (TestName == "any_empty") {
1406 EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
1407 return true;
1408 }
1409 else if (TestName == "not_empty") {
1410 EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
1411 return true;
1412 }
1413 else if (TestName == "empty") {
1414 EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
1415 return true;
1416 }
1417
1418 return false;
1419}
1420
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001421/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
1422bool EmitCaseTest1ArgStr(const std::string& TestName,
1423 const DagInit& d,
1424 const OptionDescriptions& OptDescs,
1425 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001426 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001427
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001428 if (TestName == "switch_on") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001429 apply(EmitSwitchOn(OptDescs), OptName, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001430 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001431 }
1432 else if (TestName == "input_languages_contain") {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001433 O << "InLangs.count(\"" << OptName << "\") != 0";
1434 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001435 }
1436 else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001437 // This works only for single-argument Tool::GenerateAction. Join
1438 // tools can process several files in different languages simultaneously.
1439
1440 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001441 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001442 return true;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001443 }
1444 else if (TestName == "not_empty" || TestName == "empty") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001445 bool EmitNegate = (TestName == "not_empty");
1446 apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001447 return true;
1448 }
1449
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001450 return false;
1451}
1452
1453/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
1454bool EmitCaseTest1Arg(const std::string& TestName,
1455 const DagInit& d,
1456 const OptionDescriptions& OptDescs,
1457 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001458 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001459 if (typeid(*d.getArg(0)) == typeid(ListInit))
1460 return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
1461 else
1462 return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
1463}
1464
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001465/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001466bool EmitCaseTest2Args(const std::string& TestName,
1467 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001468 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001469 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001470 raw_ostream& O) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001471 CheckNumberOfArguments(d, 2);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001472 const std::string& OptName = InitPtrToString(d.getArg(0));
1473 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001474
1475 if (TestName == "parameter_equals") {
Mikhail Glushenkov4858a1d2009-10-21 02:13:13 +00001476 const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001477 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1478 return true;
1479 }
1480 else if (TestName == "element_in_list") {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00001481 const OptionDescription& OptDesc = OptDescs.FindParameterList(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001482 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001483 O << "std::find(" << VarName << ".begin(),\n";
1484 O.indent(IndentLevel + Indent1)
1485 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001486 << OptArg << "\") != " << VarName << ".end()";
1487 return true;
1488 }
1489
1490 return false;
1491}
1492
1493// Forward declaration.
1494// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001495void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001496 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001497 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001498
1499/// EmitLogicalOperationTest - Helper function used by
1500/// EmitCaseConstructHandler.
1501void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001502 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001503 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001504 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001505 O << '(';
1506 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001507 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001508 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001509 if (j != NumArgs - 1) {
1510 O << ")\n";
1511 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1512 }
1513 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001514 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001515 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001516 }
1517}
1518
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001519void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001520 const OptionDescriptions& OptDescs, raw_ostream& O)
1521{
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001522 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001523 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1524 O << "! (";
1525 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1526 O << ")";
1527}
1528
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001529/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001530void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001531 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001532 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001533 const std::string& TestName = GetOperatorName(d);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001534
1535 if (TestName == "and")
1536 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1537 else if (TestName == "or")
1538 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001539 else if (TestName == "not")
1540 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001541 else if (EmitCaseTest0Args(TestName, O))
1542 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001543 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1544 return;
1545 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1546 return;
1547 else
Mikhail Glushenkov163dd592010-01-01 03:50:34 +00001548 throw "Unknown test '" + TestName + "' used in the 'case' construct!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001549}
1550
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001551
1552/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
1553class EmitCaseTestCallback {
1554 bool EmitElseIf_;
1555 const OptionDescriptions& OptDescs_;
1556 raw_ostream& O_;
1557public:
1558
1559 EmitCaseTestCallback(bool EmitElseIf,
1560 const OptionDescriptions& OptDescs, raw_ostream& O)
1561 : EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
1562 {}
1563
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001564 void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001565 {
1566 if (GetOperatorName(Test) == "default") {
1567 O_.indent(IndentLevel) << "else {\n";
1568 }
1569 else {
1570 O_.indent(IndentLevel)
1571 << ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001572 EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001573 O_ << ") {\n";
1574 }
1575 }
1576};
1577
1578/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
1579template <typename F>
1580class EmitCaseStatementCallback {
1581 F Callback_;
1582 raw_ostream& O_;
1583public:
1584
1585 EmitCaseStatementCallback(F Callback, raw_ostream& O)
1586 : Callback_(Callback), O_(O)
1587 {}
1588
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001589 void operator() (const Init* Statement, unsigned IndentLevel) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001590
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001591 // Ignore nested 'case' DAG.
1592 if (!(dynamic_cast<const DagInit*>(Statement) &&
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001593 GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001594 if (typeid(*Statement) == typeid(ListInit)) {
1595 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1596 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1597 B != E; ++B)
1598 Callback_(*B, (IndentLevel + Indent1), O_);
1599 }
1600 else {
1601 Callback_(Statement, (IndentLevel + Indent1), O_);
1602 }
1603 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001604 O_.indent(IndentLevel) << "}\n";
1605 }
1606
1607};
1608
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001609/// EmitCaseConstructHandler - Emit code that handles the 'case'
1610/// construct. Takes a function object that should emit code for every case
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001611/// clause. Implemented on top of WalkCase.
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00001612/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00001613/// raw_ostream& O).
1614/// EmitElseIf parameter controls the type of condition that is emitted ('if
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001615/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
1616/// .. else {..}').
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001617template <typename F>
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001618void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001619 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001620 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001621 raw_ostream& O) {
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00001622 WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
1623 EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001624}
1625
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001626/// TokenizeCmdLine - converts from
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001627/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
1628/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001629void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001630 const char* Delimiters = " \t\n\v\f\r";
1631 enum TokenizerState
1632 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1633 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001634
1635 if (CmdLine.empty())
1636 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001637 Out.push_back("");
1638
1639 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1640 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001641
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001642 for (; B != E; ++B) {
1643 char cur_ch = CmdLine[B];
1644
1645 switch (cur_st) {
1646 case Normal:
1647 if (cur_ch == '$') {
1648 cur_st = SpecialCommand;
1649 break;
1650 }
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001651 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001652 // Skip whitespace
1653 B = CmdLine.find_first_not_of(Delimiters, B);
1654 if (B == std::string::npos) {
1655 B = E-1;
1656 continue;
1657 }
1658 --B;
1659 Out.push_back("");
1660 continue;
1661 }
1662 break;
1663
1664
1665 case SpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001666 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001667 cur_st = Normal;
1668 Out.push_back("");
1669 continue;
1670 }
1671 if (cur_ch == '(') {
1672 Out.push_back("");
1673 cur_st = InsideSpecialCommand;
1674 continue;
1675 }
1676 break;
1677
1678 case InsideSpecialCommand:
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001679 if (OneOf(Delimiters, cur_ch)) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001680 continue;
1681 }
1682 if (cur_ch == '\'') {
1683 cur_st = InsideQuotationMarks;
1684 Out.push_back("");
1685 continue;
1686 }
1687 if (cur_ch == ')') {
1688 cur_st = Normal;
1689 Out.push_back("");
1690 }
1691 if (cur_ch == ',') {
1692 continue;
1693 }
1694
1695 break;
1696
1697 case InsideQuotationMarks:
1698 if (cur_ch == '\'') {
1699 cur_st = InsideSpecialCommand;
1700 continue;
1701 }
1702 break;
1703 }
1704
1705 Out.back().push_back(cur_ch);
1706 }
1707}
1708
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001709/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
1710/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
1711/// SubstituteSpecialCommands().
1712StrVector::const_iterator
1713SubstituteCall (StrVector::const_iterator Pos,
1714 StrVector::const_iterator End,
1715 bool IsJoin, raw_ostream& O)
1716{
1717 const char* errorMessage = "Syntax error in $CALL invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001718 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001719 const std::string& CmdName = *Pos;
1720
1721 if (CmdName == ")")
1722 throw "$CALL invocation: empty argument list!";
1723
1724 O << "hooks::";
1725 O << CmdName << "(";
1726
1727
1728 bool firstIteration = true;
1729 while (true) {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001730 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001731 const std::string& Arg = *Pos;
1732 assert(Arg.size() != 0);
1733
1734 if (Arg[0] == ')')
1735 break;
1736
1737 if (firstIteration)
1738 firstIteration = false;
1739 else
1740 O << ", ";
1741
1742 if (Arg == "$INFILE") {
1743 if (IsJoin)
1744 throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
1745 else
1746 O << "inFile.c_str()";
1747 }
1748 else {
1749 O << '"' << Arg << '"';
1750 }
1751 }
1752
1753 O << ')';
1754
1755 return Pos;
1756}
1757
1758/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
1759/// function used by SubstituteSpecialCommands().
1760StrVector::const_iterator
1761SubstituteEnv (StrVector::const_iterator Pos,
1762 StrVector::const_iterator End, raw_ostream& O)
1763{
1764 const char* errorMessage = "Syntax error in $ENV invocation!";
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001765 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001766 const std::string& EnvName = *Pos;
1767
1768 if (EnvName == ")")
1769 throw "$ENV invocation: empty argument list!";
1770
1771 O << "checkCString(std::getenv(\"";
1772 O << EnvName;
1773 O << "\"))";
1774
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001775 CheckedIncrement(Pos, End, errorMessage);
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001776
1777 return Pos;
1778}
1779
1780/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
1781/// handler code. Helper function used by EmitCmdLineVecFill().
1782StrVector::const_iterator
1783SubstituteSpecialCommands (StrVector::const_iterator Pos,
1784 StrVector::const_iterator End,
1785 bool IsJoin, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001786{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001787
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001788 const std::string& cmd = *Pos;
1789
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001790 // Perform substitution.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001791 if (cmd == "$CALL") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001792 Pos = SubstituteCall(Pos, End, IsJoin, O);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001793 }
1794 else if (cmd == "$ENV") {
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001795 Pos = SubstituteEnv(Pos, End, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001796 }
1797 else {
1798 throw "Unknown special command: " + cmd;
1799 }
1800
Mikhail Glushenkovabf2d982009-12-15 03:04:02 +00001801 // Handle '$CMD(ARG)/additional/text'.
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001802 const std::string& Leftover = *Pos;
1803 assert(Leftover.at(0) == ')');
1804 if (Leftover.size() != 1)
1805 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001806
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001807 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001808}
1809
1810/// EmitCmdLineVecFill - Emit code that fills in the command line
1811/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001812void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001813 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001814 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001815 StrVector StrVec;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00001816 TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001817
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001818 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001819 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001820
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001821 StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001822
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001823 // Emit the command itself.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001824 assert(!StrVec[0].empty());
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001825 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001826 if (StrVec[0][0] == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001827 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1828 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001829 }
1830 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001831 O << '"' << StrVec[0] << '"';
1832 ++B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001833 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001834 O << ";\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001835
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001836 // Go through the command arguments.
1837 assert(B <= E);
1838 for (; B != E; ++B) {
1839 const std::string& cmd = *B;
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001840
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001841 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001842 O.indent(IndentLevel);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001843
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001844 if (cmd.at(0) == '$') {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001845 O << "vec.push_back(std::make_pair(0, ";
1846 B = SubstituteSpecialCommands(B, E, IsJoin, O);
1847 O << "));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001848 }
1849 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001850 O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001851 }
1852 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001853
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001854}
1855
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001856/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1857/// implement EmitActionHandler. Emits code for
1858/// handling the (forward) and (forward_as) option properties.
1859void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001860 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001861 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001862 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001863 const std::string& Name = NewName.empty()
1864 ? ("-" + D.Name)
1865 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001866 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001867
1868 switch (D.Type) {
1869 case OptionType::Switch:
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001870 O.indent(IndentLevel)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001871 << "vec.push_back(std::make_pair(" << D.GenVariableName()
1872 << ".getPosition(), \"" << Name << "\"));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001873 break;
1874 case OptionType::Parameter:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001875 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1876 << D.GenVariableName()
1877 <<".getPosition(), \"" << Name;
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001878
1879 if (!D.isForwardNotSplit()) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001880 O << "\"));\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001881 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1882 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001883 << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001884 }
1885 else {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001886 O << "=\" + " << D.GenVariableName() << "));\n";
Mikhail Glushenkova04d4ed2010-02-23 09:04:13 +00001887 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001888 break;
1889 case OptionType::Prefix:
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001890 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
1891 << D.GenVariableName() << ".getPosition(), \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001892 << Name << "\" + "
1893 << D.GenVariableName() << "));\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001894 break;
1895 case OptionType::PrefixList:
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001896 // TODO: remove duplication across PrefixList / ParameterList / SwitchList
1897 // branches
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001898 O.indent(IndentLevel)
1899 << "for (" << D.GenTypeDeclaration()
1900 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1901 O.indent(IndentLevel)
1902 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001903 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1904 << ".getPosition(B - " << D.GenVariableName()
1905 << ".begin());\n";
1906 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001907 << Name << "\" + " << "*B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001908 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001909
1910 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001911 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001912 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001913 }
1914
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001915 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001916 break;
1917 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001918 O.indent(IndentLevel)
1919 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1920 << D.GenVariableName() << ".begin(),\n";
1921 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1922 << ".end() ; B != E;) {\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001923 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1924 << ".getPosition(B - " << D.GenVariableName()
1925 << ".begin());\n";
1926 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00001927 << Name << "\"));\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001928
1929 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00001930 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001931 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001932 }
1933
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001934 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001935 break;
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00001936 case OptionType::SwitchList:
1937 O.indent(IndentLevel)
1938 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1939 << D.GenVariableName() << ".begin(),\n";
1940 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1941 << ".end() ; B != E;) {\n";
1942 O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
1943 << ".getPosition(B - " << D.GenVariableName()
1944 << ".begin());\n";
1945 O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
1946 << Name << "\"));\n";
1947 O.indent(IndentLevel1) << "++B;\n";
1948 O.indent(IndentLevel) << "}\n";
1949 break;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001950 case OptionType::Alias:
1951 default:
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00001952 throw "Aliases are not allowed in tool option descriptions!";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001953 }
1954}
1955
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001956/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
1957/// EmitPreprocessOptionsCallback.
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001958struct ActionHandlingCallbackBase
1959{
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001960
1961 void onErrorDag(const DagInit& d,
1962 unsigned IndentLevel, raw_ostream& O) const
1963 {
1964 O.indent(IndentLevel)
1965 << "throw std::runtime_error(\"" <<
1966 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1967 : "Unknown error!")
1968 << "\");\n";
1969 }
1970
1971 void onWarningDag(const DagInit& d,
1972 unsigned IndentLevel, raw_ostream& O) const
1973 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00001974 CheckNumberOfArguments(d, 1);
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00001975 O.indent(IndentLevel) << "llvm::errs() << \""
1976 << InitPtrToString(d.getArg(0)) << "\";\n";
1977 }
1978
1979};
1980
1981/// EmitActionHandlersCallback - Emit code that handles actions. Used by
1982/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001983
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001984class EmitActionHandlersCallback;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001985
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001986typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
1987(const DagInit&, unsigned, raw_ostream&) const;
1988
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001989class EmitActionHandlersCallback :
1990 public ActionHandlingCallbackBase,
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001991 public HandlerTable<EmitActionHandlersCallbackHandler>
1992{
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00001993 typedef EmitActionHandlersCallbackHandler Handler;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001994
Mikhail Glushenkov24723282009-12-17 07:48:49 +00001995 const OptionDescriptions& OptDescs;
1996
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00001997 /// EmitHookInvocation - Common code for hook invocation from actions. Used by
1998 /// onAppendCmd and onOutputSuffix.
1999 void EmitHookInvocation(const std::string& Str,
2000 const char* BlockOpen, const char* BlockClose,
2001 unsigned IndentLevel, raw_ostream& O) const
2002 {
2003 StrVector Out;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002004 TokenizeCmdLine(Str, Out);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002005
2006 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
2007 B != E; ++B) {
2008 const std::string& cmd = *B;
2009
2010 O.indent(IndentLevel) << BlockOpen;
2011
2012 if (cmd.at(0) == '$')
2013 B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
2014 else
2015 O << '"' << cmd << '"';
2016
2017 O << BlockClose;
2018 }
2019 }
2020
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002021 void onAppendCmd (const DagInit& Dag,
2022 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002023 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002024 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002025 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002026 "vec.push_back(std::make_pair(65536, ", "));\n",
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002027 IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002028 }
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00002029
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002030 void onForward (const DagInit& Dag,
2031 unsigned IndentLevel, raw_ostream& O) const
2032 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002033 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002034 const std::string& Name = InitPtrToString(Dag.getArg(0));
2035 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2036 IndentLevel, "", O);
2037 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002038
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002039 void onForwardAs (const DagInit& Dag,
2040 unsigned IndentLevel, raw_ostream& O) const
2041 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002042 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002043 const std::string& Name = InitPtrToString(Dag.getArg(0));
2044 const std::string& NewName = InitPtrToString(Dag.getArg(1));
2045 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
2046 IndentLevel, NewName, O);
2047 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002048
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002049 void onForwardValue (const DagInit& Dag,
2050 unsigned IndentLevel, raw_ostream& O) const
2051 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002052 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002053 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002054 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002055
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002056 if (D.isSwitchList()) {
2057 throw std::runtime_error
2058 ("forward_value is not allowed with switch_list");
2059 }
2060
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002061 if (D.isParameter()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002062 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2063 << D.GenVariableName() << ".getPosition(), "
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002064 << D.GenVariableName() << "));\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002065 }
Mikhail Glushenkovbc39a792009-12-07 19:16:13 +00002066 else {
Mikhail Glushenkovfc97aeb2010-07-19 03:16:25 +00002067 O.indent(IndentLevel) << "for (" << D.GenTypeDeclaration()
2068 << "::iterator B = " << D.GenVariableName()
2069 << ".begin(), \n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002070 O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
2071 << ".end(); B != E; ++B)\n";
2072 O.indent(IndentLevel) << "{\n";
2073 O.indent(IndentLevel + Indent1)
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002074 << "unsigned pos = " << D.GenVariableName()
2075 << ".getPosition(B - " << D.GenVariableName()
2076 << ".begin());\n";
2077 O.indent(IndentLevel + Indent1)
2078 << "vec.push_back(std::make_pair(pos, *B));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002079 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002080 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002081 }
2082
2083 void onForwardTransformedValue (const DagInit& Dag,
2084 unsigned IndentLevel, raw_ostream& O) const
2085 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002086 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002087 const std::string& Name = InitPtrToString(Dag.getArg(0));
2088 const std::string& Hook = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002089 const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002090
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002091 O.indent(IndentLevel) << "vec.push_back(std::make_pair("
2092 << D.GenVariableName() << ".getPosition("
2093 << (D.isList() ? "0" : "") << "), "
2094 << "hooks::" << Hook << "(" << D.GenVariableName()
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002095 << (D.isParameter() ? ".c_str()" : "") << ")));\n";
2096 }
2097
2098 void onNoOutFile (const DagInit& Dag,
2099 unsigned IndentLevel, raw_ostream& O) const
2100 {
2101 CheckNumberOfArguments(Dag, 0);
2102 O.indent(IndentLevel) << "no_out_file = true;\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002103 }
2104
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002105 void onOutputSuffix (const DagInit& Dag,
2106 unsigned IndentLevel, raw_ostream& O) const
2107 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002108 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002109 this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
2110 "output_suffix = ", ";\n", IndentLevel, O);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002111 }
2112
2113 void onStopCompilation (const DagInit& Dag,
2114 unsigned IndentLevel, raw_ostream& O) const
2115 {
2116 O.indent(IndentLevel) << "stop_compilation = true;\n";
2117 }
2118
2119
2120 void onUnpackValues (const DagInit& Dag,
2121 unsigned IndentLevel, raw_ostream& O) const
2122 {
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002123 throw "'unpack_values' is deprecated. "
2124 "Use 'comma_separated' + 'forward_value' instead!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002125 }
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002126
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002127 public:
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002128
2129 explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
2130 : OptDescs(OD)
2131 {
2132 if (!staticMembersInitialized_) {
2133 AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
2134 AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
2135 AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
2136 AddHandler("forward", &EmitActionHandlersCallback::onForward);
2137 AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002138 AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
2139 AddHandler("forward_transformed_value",
2140 &EmitActionHandlersCallback::onForwardTransformedValue);
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002141 AddHandler("no_out_file",
2142 &EmitActionHandlersCallback::onNoOutFile);
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002143 AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
2144 AddHandler("stop_compilation",
2145 &EmitActionHandlersCallback::onStopCompilation);
2146 AddHandler("unpack_values",
2147 &EmitActionHandlersCallback::onUnpackValues);
2148
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002149
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002150 staticMembersInitialized_ = true;
2151 }
2152 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002153
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002154 void operator()(const Init* I,
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002155 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002156 {
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002157 InvokeDagInitHandler(this, I, IndentLevel, O);
Mikhail Glushenkov08509492008-12-07 16:42:22 +00002158 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002159};
2160
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002161void EmitGenerateActionMethodHeader(const ToolDescription& D,
2162 bool IsJoin, raw_ostream& O)
2163{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002164 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002165 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002166 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002167 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002168
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002169 O.indent(Indent2) << "bool HasChildren,\n";
2170 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2171 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2172 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2173 O.indent(Indent1) << "{\n";
2174 O.indent(Indent2) << "std::string cmd;\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002175 O.indent(Indent2) << "std::string out_file;\n";
2176 O.indent(Indent2) << "std::vector<std::pair<unsigned, std::string> > vec;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002177 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002178 O.indent(Indent2) << "bool no_out_file = false;\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002179 O.indent(Indent2) << "const char* output_suffix = \""
2180 << D.OutputSuffix << "\";\n";
Mikhail Glushenkov632eb202009-12-07 10:51:55 +00002181}
2182
2183// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
2184// Tool::GenerateAction() method.
2185void EmitGenerateActionMethod (const ToolDescription& D,
2186 const OptionDescriptions& OptDescs,
2187 bool IsJoin, raw_ostream& O) {
2188
2189 EmitGenerateActionMethodHeader(D, IsJoin, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002190
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002191 if (!D.CmdLine)
2192 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002193
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002194 // Process the 'command' property.
2195 O << '\n';
2196 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
2197 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002198
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002199 // Process the 'actions' list of this tool.
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002200 if (D.Actions)
Mikhail Glushenkovd5a72d92009-10-27 09:02:49 +00002201 EmitCaseConstructHandler(D.Actions, Indent2,
2202 EmitActionHandlersCallback(OptDescs),
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002203 false, OptDescs, O);
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002204 O << '\n';
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00002205
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002206 // Input file (s)
2207 if (!D.InFileOption.empty()) {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002208 O.indent(Indent2)
2209 << "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
2210 << D.InFileOption << "\");\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002211 }
2212
2213 if (IsJoin) {
2214 O.indent(Indent2)
2215 << "for (PathVector::const_iterator B = inFiles.begin(),\n";
2216 O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
2217 O.indent(Indent2) << "{\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002218 O.indent(Indent3) << "vec.push_back(std::make_pair("
2219 << "InputFilenames.getPosition(B - inFiles.begin()), "
2220 << "B->str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002221 O.indent(Indent2) << "}\n";
2222 }
2223 else {
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002224 O.indent(Indent2) << "vec.push_back(std::make_pair("
2225 << "InputFilenames.getPosition(0), inFile.str()));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002226 }
2227
2228 // Output file
2229 O.indent(Indent2) << "if (!no_out_file) {\n";
2230 if (!D.OutFileOption.empty())
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002231 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002232 << D.OutFileOption << "\"));\n";
2233
2234 O.indent(Indent3) << "out_file = this->OutFilename("
2235 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
2236 O.indent(Indent4) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002237 O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002238
2239 O.indent(Indent2) << "}\n\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00002240
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002241 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002242 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002243 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002244 O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
2245 << SinkOptionName << ".begin(), E = " << SinkOptionName
2246 << ".end(); B != E; ++B)\n";
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +00002247 O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOptionName
2248 << ".getPosition(B - " << SinkOptionName
2249 << ".begin()), *B));\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002250 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002251 }
2252
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00002253 O.indent(Indent2) << "return Action(cmd, this->SortArgs(vec), "
2254 << "stop_compilation, out_file);\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002255 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002256}
2257
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00002258/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
2259/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002260void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
2261 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002262 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002263 if (!ToolDesc.isJoin()) {
2264 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
2265 O.indent(Indent2) << "bool HasChildren,\n";
2266 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
2267 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
2268 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
2269 O.indent(Indent1) << "{\n";
2270 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
2271 << " is not a Join tool!\");\n";
2272 O.indent(Indent1) << "}\n\n";
2273 }
2274 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002275 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002276 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002277
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002278 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002279}
2280
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002281/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
2282/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002283void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002284 O.indent(Indent1) << "const char** InputLanguages() const {\n";
2285 O.indent(Indent2) << "return InputLanguages_;\n";
2286 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002287
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002288 if (D.OutLanguage.empty())
2289 throw "Tool " + D.Name + " has no 'out_language' property!";
2290
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002291 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
2292 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
2293 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002294}
2295
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002296/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002297void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002298 O.indent(Indent1) << "const char* Name() const {\n";
2299 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
2300 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002301}
2302
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002303/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
2304/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002305void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002306 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002307 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002308 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002309 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002310 O.indent(Indent2) << "return false;\n";
2311 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002312}
2313
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002314/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
2315/// conjunction with EmitCaseConstructHandler.
2316void EmitWorksOnEmptyCallback (const Init* Value,
2317 unsigned IndentLevel, raw_ostream& O) {
2318 CheckBooleanConstant(Value);
2319 O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
2320}
2321
2322/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
2323/// class.
2324void EmitWorksOnEmptyMethod (const ToolDescription& D,
2325 const OptionDescriptions& OptDescs,
2326 raw_ostream& O)
2327{
2328 O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
2329 if (D.OnEmpty == 0)
2330 O.indent(Indent2) << "return false;\n";
2331 else
2332 EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
2333 /*EmitElseIf = */ true, OptDescs, O);
2334 O.indent(Indent1) << "}\n\n";
2335}
2336
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002337/// EmitStaticMemberDefinitions - Emit static member definitions for a
2338/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002339void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002340 if (D.InLanguage.empty())
2341 throw "Tool " + D.Name + " has no 'in_language' property!";
2342
2343 O << "const char* " << D.Name << "::InputLanguages_[] = {";
2344 for (StrVector::const_iterator B = D.InLanguage.begin(),
2345 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002346 O << '\"' << *B << "\", ";
2347 O << "0};\n\n";
2348}
2349
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002350/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002351void EmitToolClassDefinition (const ToolDescription& D,
2352 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002353 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002354 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002355 return;
2356
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002357 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002358 O << "class " << D.Name << " : public ";
2359 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00002360 O << "JoinTool";
2361 else
2362 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002363
Mikhail Glushenkovf8bc1e42009-12-15 07:21:14 +00002364 O << " {\nprivate:\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002365 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002366
2367 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002368 EmitNameMethod(D, O);
2369 EmitInOutLanguageMethods(D, O);
2370 EmitIsJoinMethod(D, O);
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +00002371 EmitWorksOnEmptyMethod(D, OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002372 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002373
2374 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002375 O << "};\n";
2376
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002377 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00002378
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002379}
2380
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002381/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002382/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002383void EmitOptionDefinitions (const OptionDescriptions& descs,
2384 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002385 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002386{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002387 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002388
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00002389 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002390 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002391 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002392 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002393
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002394 if (val.Type == OptionType::Alias) {
2395 Aliases.push_back(val);
2396 continue;
2397 }
2398
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002399 if (val.isExtern())
2400 O << "extern ";
2401
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002402 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002403 << val.GenVariableName();
2404
2405 if (val.isExtern()) {
2406 O << ";\n";
2407 continue;
2408 }
2409
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002410 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002411
2412 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
2413 O << ", cl::Prefix";
2414
2415 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00002416 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002417 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002418 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002419 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002420 }
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002421
2422 if (val.isOptional())
Mikhail Glushenkove4ac23a2009-12-15 03:04:52 +00002423 O << ", cl::Optional";
Mikhail Glushenkovb5c42392010-03-05 04:46:39 +00002424
2425 if (val.isOneOrMore())
2426 O << ", cl::OneOrMore";
2427
2428 if (val.isZeroOrMore())
2429 O << ", cl::ZeroOrMore";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002430
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002431 if (val.isReallyHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002432 O << ", cl::ReallyHidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002433 else if (val.isHidden())
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002434 O << ", cl::Hidden";
Mikhail Glushenkov5b9b3ba2009-12-07 18:25:54 +00002435
2436 if (val.isCommaSeparated())
2437 O << ", cl::CommaSeparated";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00002438
2439 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00002440 O << ", cl::multi_val(" << val.MultiVal << ')';
2441
2442 if (val.InitVal) {
2443 const std::string& str = val.InitVal->getAsString();
2444 O << ", cl::init(" << str << ')';
2445 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00002446
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002447 if (!val.Help.empty())
2448 O << ", cl::desc(\"" << val.Help << "\")";
2449
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00002450 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002451 }
2452
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002453 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002454 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002455 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002456 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002457
2458 O << val.GenTypeDeclaration() << ' '
2459 << val.GenVariableName()
2460 << "(\"" << val.Name << '\"';
2461
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002462 const OptionDescription& D = descs.FindOption(val.Help);
2463 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00002464
2465 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
2466 }
2467
2468 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002469 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002470 O << (HasExterns ? "extern cl" : "cl")
2471 << "::list<std::string> " << SinkOptionName
2472 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002473
2474 O << '\n';
2475}
2476
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002477/// EmitPreprocessOptionsCallback - Helper function passed to
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002478/// EmitCaseConstructHandler() by EmitPreprocessOptions().
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002479
2480class EmitPreprocessOptionsCallback;
2481
2482typedef void
2483(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
2484(const DagInit&, unsigned, raw_ostream&) const;
2485
2486class EmitPreprocessOptionsCallback :
2487 public ActionHandlingCallbackBase,
2488 public HandlerTable<EmitPreprocessOptionsCallbackHandler>
2489{
2490 typedef EmitPreprocessOptionsCallbackHandler Handler;
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002491 typedef void
2492 (EmitPreprocessOptionsCallback::* HandlerImpl)
2493 (const Init*, unsigned, raw_ostream&) const;
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002494
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002495 const OptionDescriptions& OptDescs_;
2496
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002497 void onListOrDag(const DagInit& d, HandlerImpl h,
2498 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002499 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002500 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002501 const Init* I = d.getArg(0);
2502
2503 // If I is a list, apply h to each element.
2504 if (typeid(*I) == typeid(ListInit)) {
2505 const ListInit& L = *static_cast<const ListInit*>(I);
2506 for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
2507 ((this)->*(h))(*B, IndentLevel, O);
2508 }
2509 // Otherwise, apply h to I.
2510 else {
2511 ((this)->*(h))(I, IndentLevel, O);
2512 }
2513 }
2514
2515 void onUnsetOptionImpl(const Init* I,
2516 unsigned IndentLevel, raw_ostream& O) const
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002517 {
2518 const std::string& OptName = InitPtrToString(I);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002519 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002520
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002521 if (OptDesc.isSwitch()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002522 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
2523 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002524 else if (OptDesc.isParameter()) {
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002525 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
2526 }
Mikhail Glushenkovb6c34832009-10-22 04:15:07 +00002527 else if (OptDesc.isList()) {
2528 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2529 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002530 else {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002531 throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002532 }
2533 }
2534
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002535 void onUnsetOption(const DagInit& d,
2536 unsigned IndentLevel, raw_ostream& O) const
2537 {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002538 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
2539 IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002540 }
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002541
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002542 void onSetOptionImpl(const DagInit& d,
2543 unsigned IndentLevel, raw_ostream& O) const {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002544 CheckNumberOfArguments(d, 2);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002545 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002546 const Init* Value = d.getArg(1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002547 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2548
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002549 if (OptDesc.isList()) {
2550 const ListInit& List = InitPtrToList(Value);
2551
2552 O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
2553 for (ListInit::const_iterator B = List.begin(), E = List.end();
2554 B != E; ++B) {
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002555 const Init* CurElem = *B;
2556 if (OptDesc.isSwitchList())
2557 CheckBooleanConstant(CurElem);
2558
2559 O.indent(IndentLevel)
2560 << OptDesc.GenVariableName() << ".push_back(\""
2561 << (OptDesc.isSwitchList() ? CurElem->getAsString()
2562 : InitPtrToString(CurElem))
2563 << "\");\n";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002564 }
2565 }
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002566 else if (OptDesc.isSwitch()) {
2567 CheckBooleanConstant(Value);
2568 O.indent(IndentLevel) << OptDesc.GenVariableName()
2569 << " = " << Value->getAsString() << ";\n";
2570 }
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002571 else if (OptDesc.isParameter()) {
2572 const std::string& Str = InitPtrToString(Value);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002573 O.indent(IndentLevel) << OptDesc.GenVariableName()
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002574 << " = \"" << Str << "\";\n";
2575 }
2576 else {
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002577 throw "Can't apply 'set_option' to alias option -" + OptName + " !";
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002578 }
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002579 }
2580
2581 void onSetSwitch(const Init* I,
2582 unsigned IndentLevel, raw_ostream& O) const {
2583 const std::string& OptName = InitPtrToString(I);
2584 const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
2585
2586 if (OptDesc.isSwitch())
2587 O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
2588 else
Mikhail Glushenkov9503b492009-12-18 11:27:26 +00002589 throw "set_option: -" + OptName + " is not a switch option!";
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002590 }
2591
2592 void onSetOption(const DagInit& d,
2593 unsigned IndentLevel, raw_ostream& O) const
2594 {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002595 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002596
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002597 // Two arguments: (set_option "parameter", VALUE), where VALUE can be a
2598 // boolean, a string or a string list.
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002599 if (d.getNumArgs() > 1)
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002600 this->onSetOptionImpl(d, IndentLevel, O);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002601 // One argument: (set_option "switch")
2602 // or (set_option ["switch1", "switch2", ...])
2603 else
Mikhail Glushenkove0b65702009-12-23 12:49:30 +00002604 this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
2605 IndentLevel, O);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002606 }
2607
2608public:
2609
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002610 EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002611 : OptDescs_(OptDescs)
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002612 {
2613 if (!staticMembersInitialized_) {
2614 AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
2615 AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
2616 AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
Mikhail Glushenkov994dbe02009-12-17 07:49:16 +00002617 AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002618
2619 staticMembersInitialized_ = true;
2620 }
2621 }
2622
2623 void operator()(const Init* I,
2624 unsigned IndentLevel, raw_ostream& O) const
2625 {
2626 InvokeDagInitHandler(this, I, IndentLevel, O);
2627 }
2628
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002629};
2630
2631/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
2632void EmitPreprocessOptions (const RecordKeeper& Records,
2633 const OptionDescriptions& OptDecs, raw_ostream& O)
2634{
2635 O << "void PreprocessOptionsLocal() {\n";
2636
2637 const RecordVector& OptionPreprocessors =
2638 Records.getAllDerivedDefinitions("OptionPreprocessor");
2639
2640 for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
2641 E = OptionPreprocessors.end(); B!=E; ++B) {
2642 DagInit* Case = (*B)->getValueAsDag("preprocessor");
Mikhail Glushenkovccef6de2009-10-19 21:24:28 +00002643 EmitCaseConstructHandler(Case, Indent1,
2644 EmitPreprocessOptionsCallback(OptDecs),
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002645 false, OptDecs, O);
2646 }
2647
2648 O << "}\n\n";
2649}
2650
2651/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002652void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002653{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002654 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002655
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002656 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002657 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002658
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002659 // It is allowed for a plugin to have no language map.
2660 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002661
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002662 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
2663 if (!LangsToSuffixesList)
Mikhail Glushenkov06d26612009-12-07 19:15:57 +00002664 throw "Error in the language map definition!";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002665
2666 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002667 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002668
2669 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
2670 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
2671
2672 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002673 O.indent(Indent1) << "langMap[\""
2674 << InitPtrToString(Suffixes->getElement(i))
2675 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00002676 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002677 }
2678
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002679 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002680}
2681
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002682/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
2683/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002684void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002685 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00002686 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002687 const std::string& OpName = GetOperatorName(d);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002688
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002689 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002690 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002691 }
2692 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002693 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002694 }
2695 else if (OpName == "error") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002696 CheckNumberOfArguments(d, 1);
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002697 O.indent(IndentLevel) << "throw std::runtime_error(\""
2698 << InitPtrToString(d.getArg(0))
2699 << "\");\n";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00002700 return;
2701 }
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002702 else {
2703 throw "Unknown operator in edge properties list: '" + OpName + "'!"
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00002704 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002705 }
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002706
2707 if (d.getNumArgs() > 0)
2708 O << InitPtrToInt(d.getArg(0)) << ";\n";
2709 else
2710 O << "2;\n";
2711
Mikhail Glushenkov29063552008-05-06 18:18:20 +00002712}
2713
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002714/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00002715void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002716 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002717 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002718
2719 // Class constructor.
2720 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002721 << "public:\n";
2722 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
2723 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002724
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002725 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002726 O.indent(Indent1)
2727 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
2728 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002729
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002730 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002731 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00002732
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002733 O.indent(Indent2) << "return ret;\n";
Daniel Dunbar96a47822009-12-24 17:49:28 +00002734 O.indent(Indent1) << "}\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00002735}
2736
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002737/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002738void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002739 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002740 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002741 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002742 for (RecordVector::const_iterator B = EdgeVector.begin(),
2743 E = EdgeVector.end(); B != E; ++B) {
2744 const Record* Edge = *B;
2745 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002746 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002747
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002748 if (!IsDagEmpty(Weight))
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002749 EmitEdgeClass(i, NodeB, &Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002750 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00002751 }
2752}
2753
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002754/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002755/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002756void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002757 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00002758 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002759{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002760 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002761
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002762 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2763 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002764 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002765
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00002766 O << '\n';
2767
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00002768 // Insert edges.
2769
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002770 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002771 for (RecordVector::const_iterator B = EdgeVector.begin(),
2772 E = EdgeVector.end(); B != E; ++B) {
2773 const Record* Edge = *B;
2774 const std::string& NodeA = Edge->getValueAsString("a");
2775 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov24723282009-12-17 07:48:49 +00002776 DagInit& Weight = *Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002777
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002778 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002779
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002780 if (IsDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002781 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002782 else
2783 O << "new Edge" << i << "()";
2784
2785 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002786 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002787 }
2788
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002789 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002790}
2791
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002792/// HookInfo - Information about the hook type and number of arguments.
2793struct HookInfo {
2794
2795 // A hook can either have a single parameter of type std::vector<std::string>,
2796 // or NumArgs parameters of type const char*.
2797 enum HookType { ListHook, ArgHook };
2798
2799 HookType Type;
2800 unsigned NumArgs;
2801
2802 HookInfo() : Type(ArgHook), NumArgs(1)
2803 {}
2804
2805 HookInfo(HookType T) : Type(T), NumArgs(1)
2806 {}
2807
2808 HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
2809 {}
2810};
2811
2812typedef llvm::StringMap<HookInfo> HookInfoMap;
2813
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002814/// ExtractHookNames - Extract the hook names from all instances of
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002815/// $CALL(HookName) in the provided command line string/action. Helper
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002816/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002817class ExtractHookNames {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002818 HookInfoMap& HookNames_;
2819 const OptionDescriptions& OptDescs_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002820public:
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002821 ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
2822 : HookNames_(HookNames), OptDescs_(OptDescs)
2823 {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002824
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002825 void onAction (const DagInit& Dag) {
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002826 const std::string& Name = GetOperatorName(Dag);
2827
2828 if (Name == "forward_transformed_value") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002829 CheckNumberOfArguments(Dag, 2);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002830 const std::string& OptName = InitPtrToString(Dag.getArg(0));
2831 const std::string& HookName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovb32d8dd2010-07-19 17:17:10 +00002832 const OptionDescription& D =
2833 OptDescs_.FindParameterListOrParameter(OptName);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002834
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002835 HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
2836 : HookInfo::ArgHook);
2837 }
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002838 else if (Name == "append_cmd" || Name == "output_suffix") {
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002839 CheckNumberOfArguments(Dag, 1);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002840 this->onCmdLine(InitPtrToString(Dag.getArg(0)));
2841 }
2842 }
2843
2844 void onCmdLine(const std::string& Cmd) {
2845 StrVector cmds;
Mikhail Glushenkov2d366a22009-12-17 07:48:34 +00002846 TokenizeCmdLine(Cmd, cmds);
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002847
2848 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2849 B != E; ++B) {
2850 const std::string& cmd = *B;
2851
2852 if (cmd == "$CALL") {
2853 unsigned NumArgs = 0;
Mikhail Glushenkov9bef1bd2009-12-23 12:49:41 +00002854 CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002855 const std::string& HookName = *B;
2856
2857 if (HookName.at(0) == ')')
2858 throw "$CALL invoked with no arguments!";
2859
2860 while (++B != E && B->at(0) != ')') {
2861 ++NumArgs;
2862 }
2863
2864 HookInfoMap::const_iterator H = HookNames_.find(HookName);
2865
2866 if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
2867 H->second.Type != HookInfo::ArgHook)
2868 throw "Overloading of hooks is not allowed. Overloaded hook: "
2869 + HookName;
2870 else
2871 HookNames_[HookName] = HookInfo(NumArgs);
2872 }
2873 }
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002874 }
2875
2876 void operator()(const Init* Arg) {
2877
2878 // We're invoked on an action (either a dag or a dag list).
2879 if (typeid(*Arg) == typeid(DagInit)) {
2880 const DagInit& Dag = InitPtrToDag(Arg);
2881 this->onAction(Dag);
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002882 return;
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002883 }
2884 else if (typeid(*Arg) == typeid(ListInit)) {
2885 const ListInit& List = InitPtrToList(Arg);
2886 for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
2887 ++B) {
2888 const DagInit& Dag = InitPtrToDag(*B);
2889 this->onAction(Dag);
2890 }
2891 return;
2892 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002893
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002894 // We're invoked on a command line.
Mikhail Glushenkov545f9682009-12-15 07:20:50 +00002895 this->onCmdLine(InitPtrToString(Arg));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002896 }
Mikhail Glushenkov4d21ae72009-10-18 22:51:30 +00002897
2898 void operator()(const DagInit* Test, unsigned, bool) {
2899 this->operator()(Test);
2900 }
2901 void operator()(const Init* Statement, unsigned) {
2902 this->operator()(Statement);
2903 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002904};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002905
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002906/// FillInHookNames - Actually extract the hook names from all command
2907/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002908void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002909 const OptionDescriptions& OptDescs,
2910 HookInfoMap& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002911{
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002912 // For all tool descriptions:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002913 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2914 E = ToolDescs.end(); B != E; ++B) {
2915 const ToolDescription& D = *(*B);
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002916
2917 // Look for 'forward_transformed_value' in 'actions'.
2918 if (D.Actions)
2919 WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
2920
2921 // Look for hook invocations in 'cmd_line'.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002922 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002923 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002924 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002925 // This is a string.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002926 ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002927 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002928 // This is a 'case' construct.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002929 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002930 }
2931}
2932
2933/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2934/// property records and emit hook function declaration for each
2935/// instance of $CALL(HookName).
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002936void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
2937 const OptionDescriptions& OptDescs, raw_ostream& O) {
2938 HookInfoMap HookNames;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002939
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002940 FillInHookNames(ToolDescs, OptDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002941 if (HookNames.empty())
2942 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002943
2944 O << "namespace hooks {\n";
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002945 for (HookInfoMap::const_iterator B = HookNames.begin(),
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002946 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002947 const char* HookName = B->first();
2948 const HookInfo& Info = B->second;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002949
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002950 O.indent(Indent1) << "std::string " << HookName << "(";
2951
2952 if (Info.Type == HookInfo::ArgHook) {
2953 for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
2954 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2955 }
2956 }
2957 else {
2958 O << "const std::vector<std::string>& Arg";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002959 }
2960
2961 O <<");\n";
2962 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002963 O << "}\n\n";
2964}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002965
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002966/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002967void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002968 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2969 O.indent(Indent1) << "int Priority() const { return "
2970 << Priority << "; }\n\n";
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002971 O.indent(Indent1) << "void PreprocessOptions() const\n";
2972 O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002973 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2974 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2975 O.indent(Indent1)
2976 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2977 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2978 << "};\n\n"
2979 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002980}
2981
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002982/// EmitIncludes - Emit necessary #include directives and some
2983/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002984void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002985 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2986 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002987 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002988 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2989 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002990
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00002991 << "#include \"llvm/Support/CommandLine.h\"\n"
2992 << "#include \"llvm/Support/raw_ostream.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002993
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002994 << "#include <algorithm>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002995 << "#include <cstdlib>\n"
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00002996 << "#include <iterator>\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002997 << "#include <stdexcept>\n\n"
2998
2999 << "using namespace llvm;\n"
3000 << "using namespace llvmc;\n\n"
3001
Mikhail Glushenkov67665722008-11-12 12:41:18 +00003002 << "extern cl::opt<std::string> OutputFilename;\n\n"
3003
3004 << "inline const char* checkCString(const char* s)\n"
3005 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00003006}
3007
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003008
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003009/// PluginData - Holds all information about a plugin.
3010struct PluginData {
3011 OptionDescriptions OptDescs;
3012 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003013 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003014 ToolDescriptions ToolDescs;
3015 RecordVector Edges;
3016 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003017};
3018
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003019/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003020/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003021bool HasSink(const ToolDescriptions& ToolDescs) {
3022 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
3023 E = ToolDescs.end(); B != E; ++B)
3024 if ((*B)->isSink())
3025 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003026
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003027 return false;
3028}
3029
3030/// HasExterns - Go through the list of option descriptions and check
3031/// if there are any external options.
3032bool HasExterns(const OptionDescriptions& OptDescs) {
3033 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
3034 E = OptDescs.end(); B != E; ++B)
3035 if (B->second.isExtern())
3036 return true;
3037
3038 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00003039}
3040
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003041/// CollectPluginData - Collect tool and option properties,
3042/// compilation graph edges and plugin priority from the parse tree.
3043void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
3044 // Collect option properties.
3045 const RecordVector& OptionLists =
3046 Records.getAllDerivedDefinitions("OptionList");
3047 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
3048 Data.OptDescs);
3049
3050 // Collect tool properties.
3051 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
3052 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
3053 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00003054 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003055
3056 // Collect compilation graph edges.
3057 const RecordVector& CompilationGraphs =
3058 Records.getAllDerivedDefinitions("CompilationGraph");
3059 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
3060 Data.Edges);
3061
3062 // Calculate the priority of this plugin.
3063 const RecordVector& Priorities =
3064 Records.getAllDerivedDefinitions("PluginPriority");
3065 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00003066}
3067
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003068/// CheckPluginData - Perform some sanity checks on the collected data.
3069void CheckPluginData(PluginData& Data) {
3070 // Filter out all tools not mentioned in the compilation graph.
3071 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003072
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003073 // Typecheck the compilation graph.
3074 TypecheckGraph(Data.Edges, Data.ToolDescs);
3075
3076 // Check that there are no options without side effects (specified
3077 // only in the OptionList).
3078 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00003079}
3080
Daniel Dunbar1a551802009-07-03 00:10:29 +00003081void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003082 // Emit file header.
3083 EmitIncludes(O);
3084
3085 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00003086 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003087
3088 // Emit hook declarations.
Mikhail Glushenkov8245a1d2009-12-07 17:03:05 +00003089 EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003090
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00003091 O << "namespace {\n\n";
3092
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00003093 // Emit PreprocessOptionsLocal() function.
3094 EmitPreprocessOptions(Records, Data.OptDescs, O);
3095
3096 // Emit PopulateLanguageMapLocal() function
3097 // (language map maps from file extensions to language names).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003098 EmitPopulateLanguageMap(Records, O);
3099
3100 // Emit Tool classes.
3101 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
3102 E = Data.ToolDescs.end(); B!=E; ++B)
3103 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
3104
3105 // Emit Edge# classes.
3106 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
3107
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +00003108 // Emit PopulateCompilationGraphLocal() function.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003109 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
3110
3111 // Emit code for plugin registration.
3112 EmitRegisterPlugin(Data.Priority, O);
3113
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00003114 O << "} // End anonymous namespace.\n\n";
3115
3116 // Force linkage magic.
3117 O << "namespace llvmc {\n";
3118 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
3119 O << "}\n";
3120
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003121 // EOF
3122}
3123
3124
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003125// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00003126}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003127
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003128/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00003129void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003130 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003131 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00003132
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003133 CollectPluginData(Records, Data);
3134 CheckPluginData(Data);
3135
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +00003136 this->EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00003137 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003138
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00003139 } catch (std::exception& Error) {
3140 throw Error.what() + std::string(" - usually this means a syntax error.");
3141 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00003142}