Mikhail Glushenkov | fb37f39 | 2008-05-30 06:20:54 +0000 | [diff] [blame] | 1 | //===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===// |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 2 | // |
| 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 Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 10 | // This tablegen backend is responsible for emitting LLVMC configuration code. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mikhail Glushenkov | ecbdcf2 | 2008-05-06 18:09:29 +0000 | [diff] [blame] | 14 | #include "LLVMCConfigurationEmitter.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 15 | #include "Record.h" |
| 16 | |
| 17 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/StringExtras.h" |
| 20 | #include "llvm/ADT/StringMap.h" |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringSet.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | #include <cassert> |
| 24 | #include <functional> |
Mikhail Glushenkov | 4fb71ea | 2008-05-30 06:21:48 +0000 | [diff] [blame] | 25 | #include <stdexcept> |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 26 | #include <string> |
Chris Lattner | 32a9e7a | 2008-06-04 04:46:14 +0000 | [diff] [blame] | 27 | #include <typeinfo> |
Mikhail Glushenkov | aa4774c | 2008-11-12 00:04:46 +0000 | [diff] [blame] | 28 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Mikhail Glushenkov | 895820d | 2008-05-06 18:12:03 +0000 | [diff] [blame] | 31 | namespace { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 32 | |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | /// Typedefs |
| 35 | |
| 36 | typedef std::vector<Record*> RecordVector; |
| 37 | typedef std::vector<std::string> StrVector; |
| 38 | |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | /// Constants |
| 41 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 42 | // Indentation strings. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 43 | const char * Indent1 = " "; |
| 44 | const char * Indent2 = " "; |
| 45 | const char * Indent3 = " "; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 46 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 47 | // Default help string. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 48 | const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED"; |
| 49 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 50 | // Name for the "sink" option. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 51 | const char * SinkOptionName = "AutoGeneratedSinkOption"; |
| 52 | |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | /// Helper functions |
| 55 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 56 | /// Id - An 'identity' function object. |
| 57 | struct Id { |
| 58 | template<typename T> |
| 59 | void operator()(const T&) const { |
| 60 | } |
| 61 | }; |
| 62 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 63 | int InitPtrToInt(const Init* ptr) { |
| 64 | const IntInit& val = dynamic_cast<const IntInit&>(*ptr); |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 65 | return val.getValue(); |
| 66 | } |
| 67 | |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 68 | const std::string& InitPtrToString(const Init* ptr) { |
| 69 | const StringInit& val = dynamic_cast<const StringInit&>(*ptr); |
| 70 | return val.getValue(); |
| 71 | } |
| 72 | |
| 73 | const ListInit& InitPtrToList(const Init* ptr) { |
| 74 | const ListInit& val = dynamic_cast<const ListInit&>(*ptr); |
| 75 | return val; |
| 76 | } |
| 77 | |
| 78 | const DagInit& InitPtrToDag(const Init* ptr) { |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 79 | const DagInit& val = dynamic_cast<const DagInit&>(*ptr); |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 80 | return val; |
| 81 | } |
| 82 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 83 | // checkNumberOfArguments - Ensure that the number of args in d is |
Mikhail Glushenkov | b797000 | 2009-07-07 16:07:36 +0000 | [diff] [blame] | 84 | // greater than or equal to min_arguments, otherwise throw an exception. |
Mikhail Glushenkov | 581936a | 2008-05-06 17:22:03 +0000 | [diff] [blame] | 85 | void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) { |
Mikhail Glushenkov | b4ced5a | 2008-12-07 16:47:12 +0000 | [diff] [blame] | 86 | if (!d || d->getNumArgs() < min_arguments) |
Mikhail Glushenkov | b797000 | 2009-07-07 16:07:36 +0000 | [diff] [blame] | 87 | throw d->getOperator()->getAsString() + ": too few arguments!"; |
Mikhail Glushenkov | 581936a | 2008-05-06 17:22:03 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 90 | // isDagEmpty - is this DAG marked with an empty marker? |
| 91 | bool isDagEmpty (const DagInit* d) { |
| 92 | return d->getOperator()->getAsString() == "empty"; |
| 93 | } |
Mikhail Glushenkov | 581936a | 2008-05-06 17:22:03 +0000 | [diff] [blame] | 94 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 95 | // EscapeVariableName - Escape commas and other symbols not allowed |
| 96 | // in the C++ variable names. Makes it possible to use options named |
| 97 | // like "Wa," (useful for prefix options). |
| 98 | std::string EscapeVariableName(const std::string& Var) { |
| 99 | std::string ret; |
| 100 | for (unsigned i = 0; i != Var.size(); ++i) { |
| 101 | char cur_char = Var[i]; |
| 102 | if (cur_char == ',') { |
| 103 | ret += "_comma_"; |
| 104 | } |
| 105 | else if (cur_char == '+') { |
| 106 | ret += "_plus_"; |
| 107 | } |
| 108 | else if (cur_char == '-') { |
| 109 | ret += "_dash_"; |
| 110 | } |
| 111 | else { |
| 112 | ret.push_back(cur_char); |
| 113 | } |
| 114 | } |
| 115 | return ret; |
| 116 | } |
| 117 | |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 118 | /// oneOf - Does the input string contain this character? |
| 119 | bool oneOf(const char* lst, char c) { |
| 120 | while (*lst) { |
| 121 | if (*lst++ == c) |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 127 | template <class I, class S> |
| 128 | void checkedIncrement(I& P, I E, S ErrorString) { |
| 129 | ++P; |
| 130 | if (P == E) |
| 131 | throw ErrorString; |
| 132 | } |
| 133 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 134 | //===----------------------------------------------------------------------===// |
| 135 | /// Back-end specific code |
| 136 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 137 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 138 | /// OptionType - One of six different option types. See the |
| 139 | /// documentation for detailed description of differences. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 140 | namespace OptionType { |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 141 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 142 | enum OptionType { Alias, Switch, Parameter, ParameterList, |
Mikhail Glushenkov | b4ced5a | 2008-12-07 16:47:12 +0000 | [diff] [blame] | 143 | Prefix, PrefixList}; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 144 | |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 145 | bool IsList (OptionType t) { |
| 146 | return (t == ParameterList || t == PrefixList); |
| 147 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 148 | |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 149 | bool IsSwitch (OptionType t) { |
| 150 | return (t == Switch); |
| 151 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 152 | |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 153 | bool IsParameter (OptionType t) { |
| 154 | return (t == Parameter || t == Prefix); |
| 155 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 156 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | OptionType::OptionType stringToOptionType(const std::string& T) { |
| 160 | if (T == "alias_option") |
| 161 | return OptionType::Alias; |
| 162 | else if (T == "switch_option") |
| 163 | return OptionType::Switch; |
| 164 | else if (T == "parameter_option") |
| 165 | return OptionType::Parameter; |
| 166 | else if (T == "parameter_list_option") |
| 167 | return OptionType::ParameterList; |
| 168 | else if (T == "prefix_option") |
| 169 | return OptionType::Prefix; |
| 170 | else if (T == "prefix_list_option") |
| 171 | return OptionType::PrefixList; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 172 | else |
| 173 | throw "Unknown option type: " + T + '!'; |
| 174 | } |
| 175 | |
| 176 | namespace OptionDescriptionFlags { |
| 177 | enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2, |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 178 | ReallyHidden = 0x4, Extern = 0x8, |
| 179 | OneOrMore = 0x10, ZeroOrOne = 0x20 }; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /// OptionDescription - Represents data contained in a single |
| 183 | /// OptionList entry. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 184 | struct OptionDescription { |
| 185 | OptionType::OptionType Type; |
| 186 | std::string Name; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 187 | unsigned Flags; |
| 188 | std::string Help; |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 189 | unsigned MultiVal; |
Mikhail Glushenkov | 8fe4447 | 2009-07-07 16:08:41 +0000 | [diff] [blame] | 190 | Init* InitVal; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 191 | |
| 192 | OptionDescription(OptionType::OptionType t = OptionType::Switch, |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 193 | const std::string& n = "", |
| 194 | const std::string& h = DefaultHelpString) |
Mikhail Glushenkov | 8fe4447 | 2009-07-07 16:08:41 +0000 | [diff] [blame] | 195 | : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0) |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 196 | {} |
| 197 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 198 | /// GenTypeDeclaration - Returns the C++ variable type of this |
| 199 | /// option. |
| 200 | const char* GenTypeDeclaration() const; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 201 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 202 | /// GenVariableName - Returns the variable name used in the |
| 203 | /// generated C++ code. |
| 204 | std::string GenVariableName() const; |
Mikhail Glushenkov | 739c720 | 2008-11-28 00:13:25 +0000 | [diff] [blame] | 205 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 206 | /// Merge - Merge two option descriptions. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 207 | void Merge (const OptionDescription& other); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 208 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 209 | // Misc convenient getters/setters. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 210 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 211 | bool isAlias() const; |
Mikhail Glushenkov | b4ced5a | 2008-12-07 16:47:12 +0000 | [diff] [blame] | 212 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 213 | bool isMultiVal() const; |
| 214 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 215 | bool isExtern() const; |
Mikhail Glushenkov | b4ced5a | 2008-12-07 16:47:12 +0000 | [diff] [blame] | 216 | void setExtern(); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 217 | |
| 218 | bool isRequired() const; |
| 219 | void setRequired(); |
| 220 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 221 | bool isOneOrMore() const; |
| 222 | void setOneOrMore(); |
| 223 | |
| 224 | bool isZeroOrOne() const; |
| 225 | void setZeroOrOne(); |
| 226 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 227 | bool isHidden() const; |
| 228 | void setHidden(); |
| 229 | |
| 230 | bool isReallyHidden() const; |
| 231 | void setReallyHidden(); |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 232 | |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 233 | bool isParameter() const |
| 234 | { return OptionType::IsParameter(this->Type); } |
| 235 | |
| 236 | bool isSwitch() const |
| 237 | { return OptionType::IsSwitch(this->Type); } |
| 238 | |
| 239 | bool isList() const |
| 240 | { return OptionType::IsList(this->Type); } |
| 241 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 242 | }; |
| 243 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 244 | void OptionDescription::Merge (const OptionDescription& other) |
| 245 | { |
| 246 | if (other.Type != Type) |
| 247 | throw "Conflicting definitions for the option " + Name + "!"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 248 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 249 | if (Help == other.Help || Help == DefaultHelpString) |
| 250 | Help = other.Help; |
| 251 | else if (other.Help != DefaultHelpString) { |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 252 | llvm::errs() << "Warning: several different help strings" |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 253 | " defined for option " + Name + "\n"; |
| 254 | } |
| 255 | |
| 256 | Flags |= other.Flags; |
| 257 | } |
| 258 | |
| 259 | bool OptionDescription::isAlias() const { |
| 260 | return Type == OptionType::Alias; |
| 261 | } |
| 262 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 263 | bool OptionDescription::isMultiVal() const { |
Mikhail Glushenkov | 57cd67f | 2009-01-28 03:47:58 +0000 | [diff] [blame] | 264 | return MultiVal > 1; |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 267 | bool OptionDescription::isExtern() const { |
Mikhail Glushenkov | b4ced5a | 2008-12-07 16:47:12 +0000 | [diff] [blame] | 268 | return Flags & OptionDescriptionFlags::Extern; |
| 269 | } |
| 270 | void OptionDescription::setExtern() { |
| 271 | Flags |= OptionDescriptionFlags::Extern; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | bool OptionDescription::isRequired() const { |
| 275 | return Flags & OptionDescriptionFlags::Required; |
| 276 | } |
| 277 | void OptionDescription::setRequired() { |
| 278 | Flags |= OptionDescriptionFlags::Required; |
| 279 | } |
| 280 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 281 | bool OptionDescription::isOneOrMore() const { |
| 282 | return Flags & OptionDescriptionFlags::OneOrMore; |
| 283 | } |
| 284 | void OptionDescription::setOneOrMore() { |
| 285 | Flags |= OptionDescriptionFlags::OneOrMore; |
| 286 | } |
| 287 | |
| 288 | bool OptionDescription::isZeroOrOne() const { |
| 289 | return Flags & OptionDescriptionFlags::ZeroOrOne; |
| 290 | } |
| 291 | void OptionDescription::setZeroOrOne() { |
| 292 | Flags |= OptionDescriptionFlags::ZeroOrOne; |
| 293 | } |
| 294 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 295 | bool OptionDescription::isHidden() const { |
| 296 | return Flags & OptionDescriptionFlags::Hidden; |
| 297 | } |
| 298 | void OptionDescription::setHidden() { |
| 299 | Flags |= OptionDescriptionFlags::Hidden; |
| 300 | } |
| 301 | |
| 302 | bool OptionDescription::isReallyHidden() const { |
| 303 | return Flags & OptionDescriptionFlags::ReallyHidden; |
| 304 | } |
| 305 | void OptionDescription::setReallyHidden() { |
| 306 | Flags |= OptionDescriptionFlags::ReallyHidden; |
| 307 | } |
| 308 | |
| 309 | const char* OptionDescription::GenTypeDeclaration() const { |
| 310 | switch (Type) { |
| 311 | case OptionType::Alias: |
| 312 | return "cl::alias"; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 313 | case OptionType::PrefixList: |
| 314 | case OptionType::ParameterList: |
| 315 | return "cl::list<std::string>"; |
| 316 | case OptionType::Switch: |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 317 | return "cl::opt<bool>"; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 318 | case OptionType::Parameter: |
| 319 | case OptionType::Prefix: |
| 320 | default: |
| 321 | return "cl::opt<std::string>"; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | std::string OptionDescription::GenVariableName() const { |
| 326 | const std::string& EscapedName = EscapeVariableName(Name); |
| 327 | switch (Type) { |
| 328 | case OptionType::Alias: |
| 329 | return "AutoGeneratedAlias_" + EscapedName; |
| 330 | case OptionType::PrefixList: |
| 331 | case OptionType::ParameterList: |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 332 | return "AutoGeneratedList_" + EscapedName; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 333 | case OptionType::Switch: |
| 334 | return "AutoGeneratedSwitch_" + EscapedName; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 335 | case OptionType::Prefix: |
| 336 | case OptionType::Parameter: |
| 337 | default: |
| 338 | return "AutoGeneratedParameter_" + EscapedName; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /// OptionDescriptions - An OptionDescription array plus some helper |
| 343 | /// functions. |
| 344 | class OptionDescriptions { |
| 345 | typedef StringMap<OptionDescription> container_type; |
| 346 | |
| 347 | /// Descriptions - A list of OptionDescriptions. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 348 | container_type Descriptions; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 349 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 350 | public: |
Mikhail Glushenkov | a7d0ae3 | 2008-05-30 06:28:37 +0000 | [diff] [blame] | 351 | /// FindOption - exception-throwing wrapper for find(). |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 352 | const OptionDescription& FindOption(const std::string& OptName) const; |
Mikhail Glushenkov | 581936a | 2008-05-06 17:22:03 +0000 | [diff] [blame] | 353 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 354 | /// insertDescription - Insert new OptionDescription into |
| 355 | /// OptionDescriptions list |
| 356 | void InsertDescription (const OptionDescription& o); |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 357 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 358 | // Support for STL-style iteration |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 359 | typedef container_type::const_iterator const_iterator; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 360 | const_iterator begin() const { return Descriptions.begin(); } |
| 361 | const_iterator end() const { return Descriptions.end(); } |
| 362 | }; |
| 363 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 364 | const OptionDescription& |
| 365 | OptionDescriptions::FindOption(const std::string& OptName) const |
| 366 | { |
| 367 | const_iterator I = Descriptions.find(OptName); |
| 368 | if (I != Descriptions.end()) |
| 369 | return I->second; |
| 370 | else |
| 371 | throw OptName + ": no such option!"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 374 | void OptionDescriptions::InsertDescription (const OptionDescription& o) |
| 375 | { |
| 376 | container_type::iterator I = Descriptions.find(o.Name); |
| 377 | if (I != Descriptions.end()) { |
| 378 | OptionDescription& D = I->second; |
| 379 | D.Merge(o); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 380 | } |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 381 | else { |
| 382 | Descriptions[o.Name] = o; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 383 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 386 | /// HandlerTable - A base class for function objects implemented as |
| 387 | /// 'tables of handlers'. |
| 388 | template <class T> |
| 389 | class HandlerTable { |
| 390 | protected: |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 391 | // Implementation details. |
| 392 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 393 | /// Handler - |
| 394 | typedef void (T::* Handler) (const DagInit*); |
| 395 | /// HandlerMap - A map from property names to property handlers |
| 396 | typedef StringMap<Handler> HandlerMap; |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 397 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 398 | static HandlerMap Handlers_; |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 399 | static bool staticMembersInitialized_; |
| 400 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 401 | T* childPtr; |
| 402 | public: |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 403 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 404 | HandlerTable(T* cp) : childPtr(cp) |
| 405 | {} |
| 406 | |
| 407 | /// operator() - Just forwards to the corresponding property |
| 408 | /// handler. |
| 409 | void operator() (Init* i) { |
| 410 | const DagInit& property = InitPtrToDag(i); |
| 411 | const std::string& property_name = property.getOperator()->getAsString(); |
| 412 | typename HandlerMap::iterator method = Handlers_.find(property_name); |
| 413 | |
| 414 | if (method != Handlers_.end()) { |
| 415 | Handler h = method->second; |
| 416 | (childPtr->*h)(&property); |
| 417 | } |
| 418 | else { |
| 419 | throw "No handler found for property " + property_name + "!"; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | void AddHandler(const char* Property, Handler Handl) { |
| 424 | Handlers_[Property] = Handl; |
| 425 | } |
| 426 | }; |
| 427 | |
| 428 | template <class T> typename HandlerTable<T>::HandlerMap |
| 429 | HandlerTable<T>::Handlers_; |
| 430 | template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false; |
| 431 | |
| 432 | |
| 433 | /// CollectOptionProperties - Function object for iterating over an |
| 434 | /// option property list. |
| 435 | class CollectOptionProperties : public HandlerTable<CollectOptionProperties> { |
| 436 | private: |
| 437 | |
| 438 | /// optDescs_ - OptionDescriptions table. This is where the |
| 439 | /// information is stored. |
| 440 | OptionDescription& optDesc_; |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 441 | |
| 442 | public: |
| 443 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 444 | explicit CollectOptionProperties(OptionDescription& OD) |
| 445 | : HandlerTable<CollectOptionProperties>(this), optDesc_(OD) |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 446 | { |
| 447 | if (!staticMembersInitialized_) { |
Mikhail Glushenkov | b4ced5a | 2008-12-07 16:47:12 +0000 | [diff] [blame] | 448 | AddHandler("extern", &CollectOptionProperties::onExtern); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 449 | AddHandler("help", &CollectOptionProperties::onHelp); |
| 450 | AddHandler("hidden", &CollectOptionProperties::onHidden); |
Mikhail Glushenkov | 8fe4447 | 2009-07-07 16:08:41 +0000 | [diff] [blame] | 451 | AddHandler("init", &CollectOptionProperties::onInit); |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 452 | AddHandler("multi_val", &CollectOptionProperties::onMultiVal); |
| 453 | AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 454 | AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden); |
| 455 | AddHandler("required", &CollectOptionProperties::onRequired); |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 456 | AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne); |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 457 | |
| 458 | staticMembersInitialized_ = true; |
| 459 | } |
| 460 | } |
| 461 | |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 462 | private: |
| 463 | |
| 464 | /// Option property handlers -- |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 465 | /// Methods that handle option properties such as (help) or (hidden). |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 466 | |
Mikhail Glushenkov | b4ced5a | 2008-12-07 16:47:12 +0000 | [diff] [blame] | 467 | void onExtern (const DagInit* d) { |
| 468 | checkNumberOfArguments(d, 0); |
| 469 | optDesc_.setExtern(); |
| 470 | } |
| 471 | |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 472 | void onHelp (const DagInit* d) { |
| 473 | checkNumberOfArguments(d, 1); |
Mikhail Glushenkov | b4ced5a | 2008-12-07 16:47:12 +0000 | [diff] [blame] | 474 | optDesc_.Help = InitPtrToString(d->getArg(0)); |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Mikhail Glushenkov | 739c720 | 2008-11-28 00:13:25 +0000 | [diff] [blame] | 477 | void onHidden (const DagInit* d) { |
| 478 | checkNumberOfArguments(d, 0); |
Mikhail Glushenkov | 739c720 | 2008-11-28 00:13:25 +0000 | [diff] [blame] | 479 | optDesc_.setHidden(); |
| 480 | } |
| 481 | |
| 482 | void onReallyHidden (const DagInit* d) { |
| 483 | checkNumberOfArguments(d, 0); |
Mikhail Glushenkov | 739c720 | 2008-11-28 00:13:25 +0000 | [diff] [blame] | 484 | optDesc_.setReallyHidden(); |
| 485 | } |
| 486 | |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 487 | void onRequired (const DagInit* d) { |
| 488 | checkNumberOfArguments(d, 0); |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 489 | if (optDesc_.isOneOrMore()) |
| 490 | throw std::string("An option can't have both (required) " |
| 491 | "and (one_or_more) properties!"); |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 492 | optDesc_.setRequired(); |
| 493 | } |
| 494 | |
Mikhail Glushenkov | 8fe4447 | 2009-07-07 16:08:41 +0000 | [diff] [blame] | 495 | void onInit (const DagInit* d) { |
| 496 | checkNumberOfArguments(d, 1); |
| 497 | Init* i = d->getArg(0); |
| 498 | const std::string& str = i->getAsString(); |
| 499 | |
| 500 | bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i); |
| 501 | correct |= (optDesc_.isSwitch() && (str == "true" || str == "false")); |
| 502 | |
| 503 | if (!correct) |
| 504 | throw std::string("Incorrect usage of the 'init' option property!"); |
| 505 | |
| 506 | optDesc_.InitVal = i; |
| 507 | } |
| 508 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 509 | void onOneOrMore (const DagInit* d) { |
| 510 | checkNumberOfArguments(d, 0); |
| 511 | if (optDesc_.isRequired() || optDesc_.isZeroOrOne()) |
| 512 | throw std::string("Only one of (required), (zero_or_one) or " |
| 513 | "(one_or_more) properties is allowed!"); |
| 514 | if (!OptionType::IsList(optDesc_.Type)) |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 515 | llvm::errs() << "Warning: specifying the 'one_or_more' property " |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 516 | "on a non-list option will have no effect.\n"; |
| 517 | optDesc_.setOneOrMore(); |
| 518 | } |
| 519 | |
| 520 | void onZeroOrOne (const DagInit* d) { |
| 521 | checkNumberOfArguments(d, 0); |
| 522 | if (optDesc_.isRequired() || optDesc_.isOneOrMore()) |
| 523 | throw std::string("Only one of (required), (zero_or_one) or " |
| 524 | "(one_or_more) properties is allowed!"); |
| 525 | if (!OptionType::IsList(optDesc_.Type)) |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 526 | llvm::errs() << "Warning: specifying the 'zero_or_one' property" |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 527 | "on a non-list option will have no effect.\n"; |
| 528 | optDesc_.setZeroOrOne(); |
| 529 | } |
| 530 | |
| 531 | void onMultiVal (const DagInit* d) { |
| 532 | checkNumberOfArguments(d, 1); |
| 533 | int val = InitPtrToInt(d->getArg(0)); |
| 534 | if (val < 2) |
| 535 | throw std::string("Error in the 'multi_val' property: " |
| 536 | "the value must be greater than 1!"); |
| 537 | if (!OptionType::IsList(optDesc_.Type)) |
| 538 | throw std::string("The multi_val property is valid only " |
| 539 | "on list options!"); |
| 540 | optDesc_.MultiVal = val; |
| 541 | } |
| 542 | |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 543 | }; |
| 544 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 545 | /// AddOption - A function object that is applied to every option |
| 546 | /// description. Used by CollectOptionDescriptions. |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 547 | class AddOption { |
| 548 | private: |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 549 | OptionDescriptions& OptDescs_; |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 550 | |
| 551 | public: |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 552 | explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD) |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 553 | {} |
| 554 | |
| 555 | void operator()(const Init* i) { |
| 556 | const DagInit& d = InitPtrToDag(i); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 557 | checkNumberOfArguments(&d, 1); |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 558 | |
| 559 | const OptionType::OptionType Type = |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 560 | stringToOptionType(d.getOperator()->getAsString()); |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 561 | const std::string& Name = InitPtrToString(d.getArg(0)); |
| 562 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 563 | OptionDescription OD(Type, Name); |
| 564 | |
| 565 | if (!OD.isExtern()) |
| 566 | checkNumberOfArguments(&d, 2); |
| 567 | |
| 568 | if (OD.isAlias()) { |
| 569 | // Aliases store the aliased option name in the 'Help' field. |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 570 | OD.Help = InitPtrToString(d.getArg(1)); |
| 571 | } |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 572 | else if (!OD.isExtern()) { |
| 573 | processOptionProperties(&d, OD); |
| 574 | } |
| 575 | OptDescs_.InsertDescription(OD); |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | private: |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 579 | /// processOptionProperties - Go through the list of option |
| 580 | /// properties and call a corresponding handler for each. |
| 581 | static void processOptionProperties (const DagInit* d, OptionDescription& o) { |
| 582 | checkNumberOfArguments(d, 2); |
| 583 | DagInit::const_arg_iterator B = d->arg_begin(); |
| 584 | // Skip the first argument: it's always the option name. |
| 585 | ++B; |
| 586 | std::for_each(B, d->arg_end(), CollectOptionProperties(o)); |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 587 | } |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 588 | |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 589 | }; |
| 590 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 591 | /// CollectOptionDescriptions - Collects option properties from all |
| 592 | /// OptionLists. |
| 593 | void CollectOptionDescriptions (RecordVector::const_iterator B, |
| 594 | RecordVector::const_iterator E, |
| 595 | OptionDescriptions& OptDescs) |
| 596 | { |
| 597 | // For every OptionList: |
| 598 | for (; B!=E; ++B) { |
| 599 | RecordVector::value_type T = *B; |
| 600 | // Throws an exception if the value does not exist. |
| 601 | ListInit* PropList = T->getValueAsListInit("options"); |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 602 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 603 | // For every option description in this list: |
| 604 | // collect the information and |
| 605 | std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs)); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | // Tool information record |
| 610 | |
| 611 | namespace ToolFlags { |
| 612 | enum ToolFlags { Join = 0x1, Sink = 0x2 }; |
| 613 | } |
| 614 | |
| 615 | struct ToolDescription : public RefCountedBase<ToolDescription> { |
| 616 | std::string Name; |
| 617 | Init* CmdLine; |
| 618 | Init* Actions; |
| 619 | StrVector InLanguage; |
| 620 | std::string OutLanguage; |
| 621 | std::string OutputSuffix; |
| 622 | unsigned Flags; |
| 623 | |
| 624 | // Various boolean properties |
| 625 | void setSink() { Flags |= ToolFlags::Sink; } |
| 626 | bool isSink() const { return Flags & ToolFlags::Sink; } |
| 627 | void setJoin() { Flags |= ToolFlags::Join; } |
| 628 | bool isJoin() const { return Flags & ToolFlags::Join; } |
| 629 | |
| 630 | // Default ctor here is needed because StringMap can only store |
| 631 | // DefaultConstructible objects |
| 632 | ToolDescription() : CmdLine(0), Actions(0), Flags(0) {} |
| 633 | ToolDescription (const std::string& n) |
| 634 | : Name(n), CmdLine(0), Actions(0), Flags(0) |
| 635 | {} |
| 636 | }; |
| 637 | |
| 638 | /// ToolDescriptions - A list of Tool information records. |
| 639 | typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions; |
| 640 | |
| 641 | |
| 642 | /// CollectToolProperties - Function object for iterating over a list of |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 643 | /// tool property records. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 644 | class CollectToolProperties : public HandlerTable<CollectToolProperties> { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 645 | private: |
| 646 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 647 | /// toolDesc_ - Properties of the current Tool. This is where the |
| 648 | /// information is stored. |
| 649 | ToolDescription& toolDesc_; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 650 | |
| 651 | public: |
| 652 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 653 | explicit CollectToolProperties (ToolDescription& d) |
| 654 | : HandlerTable<CollectToolProperties>(this) , toolDesc_(d) |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 655 | { |
| 656 | if (!staticMembersInitialized_) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 657 | |
| 658 | AddHandler("actions", &CollectToolProperties::onActions); |
| 659 | AddHandler("cmd_line", &CollectToolProperties::onCmdLine); |
| 660 | AddHandler("in_language", &CollectToolProperties::onInLanguage); |
| 661 | AddHandler("join", &CollectToolProperties::onJoin); |
| 662 | AddHandler("out_language", &CollectToolProperties::onOutLanguage); |
| 663 | AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix); |
| 664 | AddHandler("sink", &CollectToolProperties::onSink); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 665 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 666 | staticMembersInitialized_ = true; |
| 667 | } |
| 668 | } |
| 669 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 670 | private: |
| 671 | |
| 672 | /// Property handlers -- |
| 673 | /// Functions that extract information about tool properties from |
| 674 | /// DAG representation. |
| 675 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 676 | void onActions (const DagInit* d) { |
| 677 | checkNumberOfArguments(d, 1); |
Mikhail Glushenkov | ad889a7 | 2008-12-07 16:45:12 +0000 | [diff] [blame] | 678 | Init* Case = d->getArg(0); |
| 679 | if (typeid(*Case) != typeid(DagInit) || |
| 680 | static_cast<DagInit*>(Case)->getOperator()->getAsString() != "case") |
| 681 | throw |
| 682 | std::string("The argument to (actions) should be a 'case' construct!"); |
| 683 | toolDesc_.Actions = Case; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 686 | void onCmdLine (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 687 | checkNumberOfArguments(d, 1); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 688 | toolDesc_.CmdLine = d->getArg(0); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 691 | void onInLanguage (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 692 | checkNumberOfArguments(d, 1); |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 693 | Init* arg = d->getArg(0); |
| 694 | |
| 695 | // Find out the argument's type. |
| 696 | if (typeid(*arg) == typeid(StringInit)) { |
| 697 | // It's a string. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 698 | toolDesc_.InLanguage.push_back(InitPtrToString(arg)); |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 699 | } |
| 700 | else { |
| 701 | // It's a list. |
| 702 | const ListInit& lst = InitPtrToList(arg); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 703 | StrVector& out = toolDesc_.InLanguage; |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 704 | |
| 705 | // Copy strings to the output vector. |
| 706 | for (ListInit::const_iterator B = lst.begin(), E = lst.end(); |
| 707 | B != E; ++B) { |
| 708 | out.push_back(InitPtrToString(*B)); |
| 709 | } |
| 710 | |
| 711 | // Remove duplicates. |
| 712 | std::sort(out.begin(), out.end()); |
| 713 | StrVector::iterator newE = std::unique(out.begin(), out.end()); |
| 714 | out.erase(newE, out.end()); |
| 715 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 718 | void onJoin (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 719 | checkNumberOfArguments(d, 0); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 720 | toolDesc_.setJoin(); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 723 | void onOutLanguage (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 724 | checkNumberOfArguments(d, 1); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 725 | toolDesc_.OutLanguage = InitPtrToString(d->getArg(0)); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 728 | void onOutputSuffix (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 729 | checkNumberOfArguments(d, 1); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 730 | toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0)); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 733 | void onSink (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 734 | checkNumberOfArguments(d, 0); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 735 | toolDesc_.setSink(); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 738 | }; |
| 739 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 740 | /// CollectToolDescriptions - Gather information about tool properties |
Mikhail Glushenkov | e4322895 | 2008-05-30 06:26:08 +0000 | [diff] [blame] | 741 | /// from the parsed TableGen data (basically a wrapper for the |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 742 | /// CollectToolProperties function object). |
| 743 | void CollectToolDescriptions (RecordVector::const_iterator B, |
| 744 | RecordVector::const_iterator E, |
| 745 | ToolDescriptions& ToolDescs) |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 746 | { |
| 747 | // Iterate over a properties list of every Tool definition |
| 748 | for (;B!=E;++B) { |
Mikhail Glushenkov | fa27077 | 2008-11-17 17:29:42 +0000 | [diff] [blame] | 749 | const Record* T = *B; |
Mikhail Glushenkov | e4322895 | 2008-05-30 06:26:08 +0000 | [diff] [blame] | 750 | // Throws an exception if the value does not exist. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 751 | ListInit* PropList = T->getValueAsListInit("properties"); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 752 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 753 | IntrusiveRefCntPtr<ToolDescription> |
| 754 | ToolDesc(new ToolDescription(T->getName())); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 755 | |
| 756 | std::for_each(PropList->begin(), PropList->end(), |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 757 | CollectToolProperties(*ToolDesc)); |
| 758 | ToolDescs.push_back(ToolDesc); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 762 | /// FillInEdgeVector - Merge all compilation graph definitions into |
| 763 | /// one single edge list. |
| 764 | void FillInEdgeVector(RecordVector::const_iterator B, |
| 765 | RecordVector::const_iterator E, RecordVector& Out) { |
| 766 | for (; B != E; ++B) { |
| 767 | const ListInit* edges = (*B)->getValueAsListInit("edges"); |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 768 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 769 | for (unsigned i = 0; i < edges->size(); ++i) |
| 770 | Out.push_back(edges->getElementAsRecord(i)); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | /// CalculatePriority - Calculate the priority of this plugin. |
| 775 | int CalculatePriority(RecordVector::const_iterator B, |
| 776 | RecordVector::const_iterator E) { |
| 777 | int total = 0; |
Mikhail Glushenkov | 35fde15 | 2008-11-17 17:30:25 +0000 | [diff] [blame] | 778 | for (; B!=E; ++B) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 779 | total += static_cast<int>((*B)->getValueAsInt("priority")); |
| 780 | } |
| 781 | return total; |
| 782 | } |
Mikhail Glushenkov | e4322895 | 2008-05-30 06:26:08 +0000 | [diff] [blame] | 783 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 784 | /// NotInGraph - Helper function object for FilterNotInGraph. |
| 785 | struct NotInGraph { |
| 786 | private: |
| 787 | const llvm::StringSet<>& ToolsInGraph_; |
| 788 | |
| 789 | public: |
| 790 | NotInGraph(const llvm::StringSet<>& ToolsInGraph) |
| 791 | : ToolsInGraph_(ToolsInGraph) |
| 792 | {} |
| 793 | |
| 794 | bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) { |
| 795 | return (ToolsInGraph_.count(x->Name) == 0); |
| 796 | } |
| 797 | }; |
| 798 | |
| 799 | /// FilterNotInGraph - Filter out from ToolDescs all Tools not |
| 800 | /// mentioned in the compilation graph definition. |
| 801 | void FilterNotInGraph (const RecordVector& EdgeVector, |
| 802 | ToolDescriptions& ToolDescs) { |
| 803 | |
| 804 | // List all tools mentioned in the graph. |
| 805 | llvm::StringSet<> ToolsInGraph; |
| 806 | |
| 807 | for (RecordVector::const_iterator B = EdgeVector.begin(), |
| 808 | E = EdgeVector.end(); B != E; ++B) { |
| 809 | |
| 810 | const Record* Edge = *B; |
Mikhail Glushenkov | 15b71ba | 2008-12-07 16:44:47 +0000 | [diff] [blame] | 811 | const std::string& NodeA = Edge->getValueAsString("a"); |
| 812 | const std::string& NodeB = Edge->getValueAsString("b"); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 813 | |
Mikhail Glushenkov | 15b71ba | 2008-12-07 16:44:47 +0000 | [diff] [blame] | 814 | if (NodeA != "root") |
| 815 | ToolsInGraph.insert(NodeA); |
| 816 | ToolsInGraph.insert(NodeB); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | // Filter ToolPropertiesList. |
| 820 | ToolDescriptions::iterator new_end = |
| 821 | std::remove_if(ToolDescs.begin(), ToolDescs.end(), |
| 822 | NotInGraph(ToolsInGraph)); |
| 823 | ToolDescs.erase(new_end, ToolDescs.end()); |
| 824 | } |
| 825 | |
| 826 | /// FillInToolToLang - Fills in two tables that map tool names to |
| 827 | /// (input, output) languages. Helper function used by TypecheckGraph(). |
| 828 | void FillInToolToLang (const ToolDescriptions& ToolDescs, |
| 829 | StringMap<StringSet<> >& ToolToInLang, |
| 830 | StringMap<std::string>& ToolToOutLang) { |
| 831 | for (ToolDescriptions::const_iterator B = ToolDescs.begin(), |
| 832 | E = ToolDescs.end(); B != E; ++B) { |
| 833 | const ToolDescription& D = *(*B); |
| 834 | for (StrVector::const_iterator B = D.InLanguage.begin(), |
| 835 | E = D.InLanguage.end(); B != E; ++B) |
| 836 | ToolToInLang[D.Name].insert(*B); |
| 837 | ToolToOutLang[D.Name] = D.OutLanguage; |
Mikhail Glushenkov | e4322895 | 2008-05-30 06:26:08 +0000 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 841 | /// TypecheckGraph - Check that names for output and input languages |
| 842 | /// on all edges do match. This doesn't do much when the information |
| 843 | /// about the whole graph is not available (i.e. when compiling most |
| 844 | /// plugins). |
| 845 | void TypecheckGraph (const RecordVector& EdgeVector, |
| 846 | const ToolDescriptions& ToolDescs) { |
| 847 | StringMap<StringSet<> > ToolToInLang; |
| 848 | StringMap<std::string> ToolToOutLang; |
| 849 | |
| 850 | FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang); |
| 851 | StringMap<std::string>::iterator IAE = ToolToOutLang.end(); |
| 852 | StringMap<StringSet<> >::iterator IBE = ToolToInLang.end(); |
| 853 | |
| 854 | for (RecordVector::const_iterator B = EdgeVector.begin(), |
| 855 | E = EdgeVector.end(); B != E; ++B) { |
| 856 | const Record* Edge = *B; |
Mikhail Glushenkov | 15b71ba | 2008-12-07 16:44:47 +0000 | [diff] [blame] | 857 | const std::string& NodeA = Edge->getValueAsString("a"); |
| 858 | const std::string& NodeB = Edge->getValueAsString("b"); |
| 859 | StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA); |
| 860 | StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 861 | |
Mikhail Glushenkov | 15b71ba | 2008-12-07 16:44:47 +0000 | [diff] [blame] | 862 | if (NodeA != "root") { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 863 | if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0) |
Mikhail Glushenkov | 15b71ba | 2008-12-07 16:44:47 +0000 | [diff] [blame] | 864 | throw "Edge " + NodeA + "->" + NodeB |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 865 | + ": output->input language mismatch"; |
| 866 | } |
| 867 | |
Mikhail Glushenkov | 15b71ba | 2008-12-07 16:44:47 +0000 | [diff] [blame] | 868 | if (NodeB == "root") |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 869 | throw std::string("Edges back to the root are not allowed!"); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | /// WalkCase - Walks the 'case' expression DAG and invokes |
| 874 | /// TestCallback on every test, and StatementCallback on every |
| 875 | /// statement. Handles 'case' nesting, but not the 'and' and 'or' |
| 876 | /// combinators. |
| 877 | // TODO: Re-implement EmitCaseConstructHandler on top of this function? |
| 878 | template <typename F1, typename F2> |
| 879 | void WalkCase(Init* Case, F1 TestCallback, F2 StatementCallback) { |
| 880 | const DagInit& d = InitPtrToDag(Case); |
| 881 | bool even = false; |
| 882 | for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end(); |
| 883 | B != E; ++B) { |
| 884 | Init* arg = *B; |
| 885 | if (even && dynamic_cast<DagInit*>(arg) |
| 886 | && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case") |
| 887 | WalkCase(arg, TestCallback, StatementCallback); |
| 888 | else if (!even) |
| 889 | TestCallback(arg); |
| 890 | else |
| 891 | StatementCallback(arg); |
| 892 | even = !even; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | /// ExtractOptionNames - A helper function object used by |
| 897 | /// CheckForSuperfluousOptions() to walk the 'case' DAG. |
| 898 | class ExtractOptionNames { |
| 899 | llvm::StringSet<>& OptionNames_; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 900 | |
Mikhail Glushenkov | 0850949 | 2008-12-07 16:42:22 +0000 | [diff] [blame] | 901 | void processDag(const Init* Statement) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 902 | const DagInit& Stmt = InitPtrToDag(Statement); |
| 903 | const std::string& ActionName = Stmt.getOperator()->getAsString(); |
| 904 | if (ActionName == "forward" || ActionName == "forward_as" || |
| 905 | ActionName == "unpack_values" || ActionName == "switch_on" || |
| 906 | ActionName == "parameter_equals" || ActionName == "element_in_list" || |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 907 | ActionName == "not_empty" || ActionName == "empty") { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 908 | checkNumberOfArguments(&Stmt, 1); |
| 909 | const std::string& Name = InitPtrToString(Stmt.getArg(0)); |
| 910 | OptionNames_.insert(Name); |
| 911 | } |
| 912 | else if (ActionName == "and" || ActionName == "or") { |
| 913 | for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) { |
Mikhail Glushenkov | 0850949 | 2008-12-07 16:42:22 +0000 | [diff] [blame] | 914 | this->processDag(Stmt.getArg(i)); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | } |
Mikhail Glushenkov | 0850949 | 2008-12-07 16:42:22 +0000 | [diff] [blame] | 918 | |
| 919 | public: |
| 920 | ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames) |
| 921 | {} |
| 922 | |
| 923 | void operator()(const Init* Statement) { |
| 924 | if (typeid(*Statement) == typeid(ListInit)) { |
| 925 | const ListInit& DagList = *static_cast<const ListInit*>(Statement); |
| 926 | for (ListInit::const_iterator B = DagList.begin(), E = DagList.end(); |
| 927 | B != E; ++B) |
| 928 | this->processDag(*B); |
| 929 | } |
| 930 | else { |
| 931 | this->processDag(Statement); |
| 932 | } |
| 933 | } |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 934 | }; |
| 935 | |
Mikhail Glushenkov | a7d0ae3 | 2008-05-30 06:28:37 +0000 | [diff] [blame] | 936 | /// CheckForSuperfluousOptions - Check that there are no side |
| 937 | /// effect-free options (specified only in the OptionList). Otherwise, |
| 938 | /// output a warning. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 939 | void CheckForSuperfluousOptions (const RecordVector& Edges, |
| 940 | const ToolDescriptions& ToolDescs, |
| 941 | const OptionDescriptions& OptDescs) { |
Mikhail Glushenkov | a7d0ae3 | 2008-05-30 06:28:37 +0000 | [diff] [blame] | 942 | llvm::StringSet<> nonSuperfluousOptions; |
| 943 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 944 | // Add all options mentioned in the ToolDesc.Actions to the set of |
Mikhail Glushenkov | a7d0ae3 | 2008-05-30 06:28:37 +0000 | [diff] [blame] | 945 | // non-superfluous options. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 946 | for (ToolDescriptions::const_iterator B = ToolDescs.begin(), |
| 947 | E = ToolDescs.end(); B != E; ++B) { |
| 948 | const ToolDescription& TD = *(*B); |
| 949 | ExtractOptionNames Callback(nonSuperfluousOptions); |
| 950 | if (TD.Actions) |
| 951 | WalkCase(TD.Actions, Callback, Callback); |
| 952 | } |
| 953 | |
| 954 | // Add all options mentioned in the 'case' clauses of the |
| 955 | // OptionalEdges of the compilation graph to the set of |
| 956 | // non-superfluous options. |
| 957 | for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end(); |
| 958 | B != E; ++B) { |
| 959 | const Record* Edge = *B; |
| 960 | DagInit* Weight = Edge->getValueAsDag("weight"); |
| 961 | |
| 962 | if (!isDagEmpty(Weight)) |
| 963 | WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id()); |
Mikhail Glushenkov | a7d0ae3 | 2008-05-30 06:28:37 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | // Check that all options in OptDescs belong to the set of |
| 967 | // non-superfluous options. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 968 | for (OptionDescriptions::const_iterator B = OptDescs.begin(), |
Mikhail Glushenkov | a7d0ae3 | 2008-05-30 06:28:37 +0000 | [diff] [blame] | 969 | E = OptDescs.end(); B != E; ++B) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 970 | const OptionDescription& Val = B->second; |
Mikhail Glushenkov | a7d0ae3 | 2008-05-30 06:28:37 +0000 | [diff] [blame] | 971 | if (!nonSuperfluousOptions.count(Val.Name) |
| 972 | && Val.Type != OptionType::Alias) |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 973 | llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! " |
Mikhail Glushenkov | a7d0ae3 | 2008-05-30 06:28:37 +0000 | [diff] [blame] | 974 | "Probable cause: this option is specified only in the OptionList.\n"; |
| 975 | } |
| 976 | } |
| 977 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 978 | /// EmitCaseTest1Arg - Helper function used by |
| 979 | /// EmitCaseConstructHandler. |
| 980 | bool EmitCaseTest1Arg(const std::string& TestName, |
| 981 | const DagInit& d, |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 982 | const OptionDescriptions& OptDescs, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 983 | raw_ostream& O) { |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 984 | checkNumberOfArguments(&d, 1); |
| 985 | const std::string& OptName = InitPtrToString(d.getArg(0)); |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 986 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 987 | if (TestName == "switch_on") { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 988 | const OptionDescription& OptDesc = OptDescs.FindOption(OptName); |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 989 | if (!OptDesc.isSwitch()) |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 990 | throw OptName + ": incorrect option type - should be a switch!"; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 991 | O << OptDesc.GenVariableName(); |
| 992 | return true; |
| 993 | } else if (TestName == "input_languages_contain") { |
| 994 | O << "InLangs.count(\"" << OptName << "\") != 0"; |
| 995 | return true; |
Mikhail Glushenkov | 2e73e85 | 2008-05-30 06:19:52 +0000 | [diff] [blame] | 996 | } else if (TestName == "in_language") { |
Mikhail Glushenkov | 0737651 | 2008-09-22 20:48:22 +0000 | [diff] [blame] | 997 | // This works only for single-argument Tool::GenerateAction. Join |
| 998 | // tools can process several files in different languages simultaneously. |
| 999 | |
| 1000 | // TODO: make this work with Edge::Weight (if possible). |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 1001 | O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"'; |
Mikhail Glushenkov | 2e73e85 | 2008-05-30 06:19:52 +0000 | [diff] [blame] | 1002 | return true; |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 1003 | } else if (TestName == "not_empty" || TestName == "empty") { |
| 1004 | const char* Test = (TestName == "empty") ? "" : "!"; |
| 1005 | |
Mikhail Glushenkov | 92b8da7 | 2008-05-30 06:24:07 +0000 | [diff] [blame] | 1006 | if (OptName == "o") { |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 1007 | O << Test << "OutputFilename.empty()"; |
Mikhail Glushenkov | 92b8da7 | 2008-05-30 06:24:07 +0000 | [diff] [blame] | 1008 | return true; |
| 1009 | } |
| 1010 | else { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1011 | const OptionDescription& OptDesc = OptDescs.FindOption(OptName); |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 1012 | if (OptDesc.isSwitch()) |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1013 | throw OptName |
| 1014 | + ": incorrect option type - should be a list or parameter!"; |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 1015 | O << Test << OptDesc.GenVariableName() << ".empty()"; |
Mikhail Glushenkov | 92b8da7 | 2008-05-30 06:24:07 +0000 | [diff] [blame] | 1016 | return true; |
| 1017 | } |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | return false; |
| 1021 | } |
| 1022 | |
| 1023 | /// EmitCaseTest2Args - Helper function used by |
| 1024 | /// EmitCaseConstructHandler. |
| 1025 | bool EmitCaseTest2Args(const std::string& TestName, |
| 1026 | const DagInit& d, |
| 1027 | const char* IndentLevel, |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1028 | const OptionDescriptions& OptDescs, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1029 | raw_ostream& O) { |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1030 | checkNumberOfArguments(&d, 2); |
| 1031 | const std::string& OptName = InitPtrToString(d.getArg(0)); |
| 1032 | const std::string& OptArg = InitPtrToString(d.getArg(1)); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1033 | const OptionDescription& OptDesc = OptDescs.FindOption(OptName); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1034 | |
| 1035 | if (TestName == "parameter_equals") { |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 1036 | if (!OptDesc.isParameter()) |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1037 | throw OptName + ": incorrect option type - should be a parameter!"; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1038 | O << OptDesc.GenVariableName() << " == \"" << OptArg << "\""; |
| 1039 | return true; |
| 1040 | } |
| 1041 | else if (TestName == "element_in_list") { |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 1042 | if (!OptDesc.isList()) |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1043 | throw OptName + ": incorrect option type - should be a list!"; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1044 | const std::string& VarName = OptDesc.GenVariableName(); |
| 1045 | O << "std::find(" << VarName << ".begin(),\n" |
| 1046 | << IndentLevel << Indent1 << VarName << ".end(), \"" |
| 1047 | << OptArg << "\") != " << VarName << ".end()"; |
| 1048 | return true; |
| 1049 | } |
| 1050 | |
| 1051 | return false; |
| 1052 | } |
| 1053 | |
| 1054 | // Forward declaration. |
| 1055 | // EmitLogicalOperationTest and EmitCaseTest are mutually recursive. |
| 1056 | void EmitCaseTest(const DagInit& d, const char* IndentLevel, |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1057 | const OptionDescriptions& OptDescs, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1058 | raw_ostream& O); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1059 | |
| 1060 | /// EmitLogicalOperationTest - Helper function used by |
| 1061 | /// EmitCaseConstructHandler. |
| 1062 | void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp, |
| 1063 | const char* IndentLevel, |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1064 | const OptionDescriptions& OptDescs, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1065 | raw_ostream& O) { |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1066 | O << '('; |
| 1067 | for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) { |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 1068 | const DagInit& InnerTest = InitPtrToDag(d.getArg(j)); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1069 | EmitCaseTest(InnerTest, IndentLevel, OptDescs, O); |
| 1070 | if (j != NumArgs - 1) |
| 1071 | O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " ("; |
| 1072 | else |
| 1073 | O << ')'; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | /// EmitCaseTest - Helper function used by EmitCaseConstructHandler. |
| 1078 | void EmitCaseTest(const DagInit& d, const char* IndentLevel, |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1079 | const OptionDescriptions& OptDescs, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1080 | raw_ostream& O) { |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1081 | const std::string& TestName = d.getOperator()->getAsString(); |
| 1082 | |
| 1083 | if (TestName == "and") |
| 1084 | EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O); |
| 1085 | else if (TestName == "or") |
| 1086 | EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O); |
| 1087 | else if (EmitCaseTest1Arg(TestName, d, OptDescs, O)) |
| 1088 | return; |
| 1089 | else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O)) |
| 1090 | return; |
| 1091 | else |
| 1092 | throw TestName + ": unknown edge property!"; |
| 1093 | } |
| 1094 | |
| 1095 | // Emit code that handles the 'case' construct. |
| 1096 | // Takes a function object that should emit code for every case clause. |
| 1097 | // Callback's type is |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1098 | // void F(Init* Statement, const char* IndentLevel, raw_ostream& O). |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1099 | template <typename F> |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1100 | void EmitCaseConstructHandler(const Init* Dag, const char* IndentLevel, |
Mikhail Glushenkov | 310d2eb | 2008-05-31 13:43:21 +0000 | [diff] [blame] | 1101 | F Callback, bool EmitElseIf, |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1102 | const OptionDescriptions& OptDescs, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1103 | raw_ostream& O) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1104 | const DagInit* d = &InitPtrToDag(Dag); |
| 1105 | if (d->getOperator()->getAsString() != "case") |
| 1106 | throw std::string("EmitCaseConstructHandler should be invoked" |
| 1107 | " only on 'case' expressions!"); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1108 | |
Mikhail Glushenkov | cb64f4b | 2008-05-30 06:15:47 +0000 | [diff] [blame] | 1109 | unsigned numArgs = d->getNumArgs(); |
| 1110 | if (d->getNumArgs() < 2) |
| 1111 | throw "There should be at least one clause in the 'case' expression:\n" |
| 1112 | + d->getAsString(); |
| 1113 | |
| 1114 | for (unsigned i = 0; i != numArgs; ++i) { |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 1115 | const DagInit& Test = InitPtrToDag(d->getArg(i)); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1116 | |
Mikhail Glushenkov | cb64f4b | 2008-05-30 06:15:47 +0000 | [diff] [blame] | 1117 | // Emit the test. |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1118 | if (Test.getOperator()->getAsString() == "default") { |
| 1119 | if (i+2 != numArgs) |
| 1120 | throw std::string("The 'default' clause should be the last in the" |
| 1121 | "'case' construct!"); |
| 1122 | O << IndentLevel << "else {\n"; |
| 1123 | } |
| 1124 | else { |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1125 | O << IndentLevel << ((i != 0 && EmitElseIf) ? "else if (" : "if ("); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1126 | EmitCaseTest(Test, IndentLevel, OptDescs, O); |
| 1127 | O << ") {\n"; |
| 1128 | } |
| 1129 | |
Mikhail Glushenkov | cb64f4b | 2008-05-30 06:15:47 +0000 | [diff] [blame] | 1130 | // Emit the corresponding statement. |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1131 | ++i; |
| 1132 | if (i == numArgs) |
| 1133 | throw "Case construct handler: no corresponding action " |
| 1134 | "found for the test " + Test.getAsString() + '!'; |
| 1135 | |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1136 | Init* arg = d->getArg(i); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1137 | const DagInit* nd = dynamic_cast<DagInit*>(arg); |
| 1138 | if (nd && (nd->getOperator()->getAsString() == "case")) { |
| 1139 | // Handle the nested 'case'. |
| 1140 | EmitCaseConstructHandler(nd, (std::string(IndentLevel) + Indent1).c_str(), |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1141 | Callback, EmitElseIf, OptDescs, O); |
| 1142 | } |
| 1143 | else { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1144 | Callback(arg, (std::string(IndentLevel) + Indent1).c_str(), O); |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1145 | } |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1146 | O << IndentLevel << "}\n"; |
| 1147 | } |
| 1148 | } |
| 1149 | |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1150 | /// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to |
| 1151 | /// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] . |
| 1152 | /// Helper function used by EmitCmdLineVecFill and. |
| 1153 | void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) { |
| 1154 | const char* Delimiters = " \t\n\v\f\r"; |
| 1155 | enum TokenizerState |
| 1156 | { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks } |
| 1157 | cur_st = Normal; |
Mikhail Glushenkov | 7bba233 | 2009-06-25 18:21:34 +0000 | [diff] [blame] | 1158 | |
| 1159 | if (CmdLine.empty()) |
| 1160 | return; |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1161 | Out.push_back(""); |
| 1162 | |
| 1163 | std::string::size_type B = CmdLine.find_first_not_of(Delimiters), |
| 1164 | E = CmdLine.size(); |
Mikhail Glushenkov | 7bba233 | 2009-06-25 18:21:34 +0000 | [diff] [blame] | 1165 | |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1166 | for (; B != E; ++B) { |
| 1167 | char cur_ch = CmdLine[B]; |
| 1168 | |
| 1169 | switch (cur_st) { |
| 1170 | case Normal: |
| 1171 | if (cur_ch == '$') { |
| 1172 | cur_st = SpecialCommand; |
| 1173 | break; |
| 1174 | } |
| 1175 | if (oneOf(Delimiters, cur_ch)) { |
| 1176 | // Skip whitespace |
| 1177 | B = CmdLine.find_first_not_of(Delimiters, B); |
| 1178 | if (B == std::string::npos) { |
| 1179 | B = E-1; |
| 1180 | continue; |
| 1181 | } |
| 1182 | --B; |
| 1183 | Out.push_back(""); |
| 1184 | continue; |
| 1185 | } |
| 1186 | break; |
| 1187 | |
| 1188 | |
| 1189 | case SpecialCommand: |
| 1190 | if (oneOf(Delimiters, cur_ch)) { |
| 1191 | cur_st = Normal; |
| 1192 | Out.push_back(""); |
| 1193 | continue; |
| 1194 | } |
| 1195 | if (cur_ch == '(') { |
| 1196 | Out.push_back(""); |
| 1197 | cur_st = InsideSpecialCommand; |
| 1198 | continue; |
| 1199 | } |
| 1200 | break; |
| 1201 | |
| 1202 | case InsideSpecialCommand: |
| 1203 | if (oneOf(Delimiters, cur_ch)) { |
| 1204 | continue; |
| 1205 | } |
| 1206 | if (cur_ch == '\'') { |
| 1207 | cur_st = InsideQuotationMarks; |
| 1208 | Out.push_back(""); |
| 1209 | continue; |
| 1210 | } |
| 1211 | if (cur_ch == ')') { |
| 1212 | cur_st = Normal; |
| 1213 | Out.push_back(""); |
| 1214 | } |
| 1215 | if (cur_ch == ',') { |
| 1216 | continue; |
| 1217 | } |
| 1218 | |
| 1219 | break; |
| 1220 | |
| 1221 | case InsideQuotationMarks: |
| 1222 | if (cur_ch == '\'') { |
| 1223 | cur_st = InsideSpecialCommand; |
| 1224 | continue; |
| 1225 | } |
| 1226 | break; |
| 1227 | } |
| 1228 | |
| 1229 | Out.back().push_back(cur_ch); |
| 1230 | } |
| 1231 | } |
| 1232 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1233 | /// SubstituteSpecialCommands - Perform string substitution for $CALL |
| 1234 | /// and $ENV. Helper function used by EmitCmdLineVecFill(). |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1235 | StrVector::const_iterator SubstituteSpecialCommands |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1236 | (StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O) |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1237 | { |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame] | 1238 | |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1239 | const std::string& cmd = *Pos; |
| 1240 | |
| 1241 | if (cmd == "$CALL") { |
| 1242 | checkedIncrement(Pos, End, "Syntax error in $CALL invocation!"); |
| 1243 | const std::string& CmdName = *Pos; |
| 1244 | |
| 1245 | if (CmdName == ")") |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame] | 1246 | throw std::string("$CALL invocation: empty argument list!"); |
| 1247 | |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1248 | O << "hooks::"; |
| 1249 | O << CmdName << "("; |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame] | 1250 | |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1251 | |
| 1252 | bool firstIteration = true; |
| 1253 | while (true) { |
| 1254 | checkedIncrement(Pos, End, "Syntax error in $CALL invocation!"); |
| 1255 | const std::string& Arg = *Pos; |
| 1256 | assert(Arg.size() != 0); |
| 1257 | |
| 1258 | if (Arg[0] == ')') |
| 1259 | break; |
| 1260 | |
| 1261 | if (firstIteration) |
| 1262 | firstIteration = false; |
| 1263 | else |
| 1264 | O << ", "; |
| 1265 | |
| 1266 | O << '"' << Arg << '"'; |
| 1267 | } |
| 1268 | |
| 1269 | O << ')'; |
| 1270 | |
| 1271 | } |
| 1272 | else if (cmd == "$ENV") { |
| 1273 | checkedIncrement(Pos, End, "Syntax error in $ENV invocation!"); |
| 1274 | const std::string& EnvName = *Pos; |
| 1275 | |
| 1276 | if (EnvName == ")") |
| 1277 | throw "$ENV invocation: empty argument list!"; |
| 1278 | |
| 1279 | O << "checkCString(std::getenv(\""; |
| 1280 | O << EnvName; |
| 1281 | O << "\"))"; |
| 1282 | |
| 1283 | checkedIncrement(Pos, End, "Syntax error in $ENV invocation!"); |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame] | 1284 | } |
| 1285 | else { |
| 1286 | throw "Unknown special command: " + cmd; |
| 1287 | } |
| 1288 | |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1289 | const std::string& Leftover = *Pos; |
| 1290 | assert(Leftover.at(0) == ')'); |
| 1291 | if (Leftover.size() != 1) |
| 1292 | O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")"; |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame] | 1293 | |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1294 | return Pos; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | /// EmitCmdLineVecFill - Emit code that fills in the command line |
| 1298 | /// vector. Helper function used by EmitGenerateActionMethod(). |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1299 | void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName, |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1300 | bool IsJoin, const char* IndentLevel, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1301 | raw_ostream& O) { |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1302 | StrVector StrVec; |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1303 | TokenizeCmdline(InitPtrToString(CmdLine), StrVec); |
| 1304 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1305 | if (StrVec.empty()) |
Mikhail Glushenkov | 7bba233 | 2009-06-25 18:21:34 +0000 | [diff] [blame] | 1306 | throw "Tool '" + ToolName + "' has empty command line!"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1307 | |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1308 | StrVector::const_iterator I = StrVec.begin(), E = StrVec.end(); |
| 1309 | |
| 1310 | // If there is a hook invocation on the place of the first command, skip it. |
Mikhail Glushenkov | 0941b0f | 2009-04-19 00:22:35 +0000 | [diff] [blame] | 1311 | assert(!StrVec[0].empty()); |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1312 | if (StrVec[0][0] == '$') { |
| 1313 | while (I != E && (*I)[0] != ')' ) |
| 1314 | ++I; |
| 1315 | |
| 1316 | // Skip the ')' symbol. |
| 1317 | ++I; |
| 1318 | } |
| 1319 | else { |
| 1320 | ++I; |
| 1321 | } |
| 1322 | |
| 1323 | for (; I != E; ++I) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1324 | const std::string& cmd = *I; |
Mikhail Glushenkov | 0941b0f | 2009-04-19 00:22:35 +0000 | [diff] [blame] | 1325 | assert(!cmd.empty()); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1326 | O << IndentLevel; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1327 | if (cmd.at(0) == '$') { |
| 1328 | if (cmd == "$INFILE") { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1329 | if (IsJoin) |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1330 | O << "for (PathVector::const_iterator B = inFiles.begin()" |
| 1331 | << ", E = inFiles.end();\n" |
| 1332 | << IndentLevel << "B != E; ++B)\n" |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame^] | 1333 | << IndentLevel << Indent1 << "vec.push_back(B->str());\n"; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1334 | else |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame^] | 1335 | O << "vec.push_back(inFile.str());\n"; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1336 | } |
| 1337 | else if (cmd == "$OUTFILE") { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1338 | O << "vec.push_back(out_file);\n"; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1339 | } |
| 1340 | else { |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1341 | O << "vec.push_back("; |
| 1342 | I = SubstituteSpecialCommands(I, E, O); |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame] | 1343 | O << ");\n"; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1344 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1345 | } |
| 1346 | else { |
| 1347 | O << "vec.push_back(\"" << cmd << "\");\n"; |
| 1348 | } |
| 1349 | } |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1350 | O << IndentLevel << "cmd = "; |
| 1351 | |
| 1352 | if (StrVec[0][0] == '$') |
| 1353 | SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O); |
| 1354 | else |
| 1355 | O << '"' << StrVec[0] << '"'; |
| 1356 | O << ";\n"; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1359 | /// EmitCmdLineVecFillCallback - A function object wrapper around |
| 1360 | /// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an |
| 1361 | /// argument to EmitCaseConstructHandler(). |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1362 | class EmitCmdLineVecFillCallback { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1363 | bool IsJoin; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1364 | const std::string& ToolName; |
| 1365 | public: |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1366 | EmitCmdLineVecFillCallback(bool J, const std::string& TN) |
| 1367 | : IsJoin(J), ToolName(TN) {} |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1368 | |
| 1369 | void operator()(const Init* Statement, const char* IndentLevel, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1370 | raw_ostream& O) const |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1371 | { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1372 | EmitCmdLineVecFill(Statement, ToolName, IsJoin, |
| 1373 | IndentLevel, O); |
| 1374 | } |
| 1375 | }; |
| 1376 | |
| 1377 | /// EmitForwardOptionPropertyHandlingCode - Helper function used to |
| 1378 | /// implement EmitActionHandler. Emits code for |
| 1379 | /// handling the (forward) and (forward_as) option properties. |
| 1380 | void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D, |
| 1381 | const char* Indent, |
| 1382 | const std::string& NewName, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1383 | raw_ostream& O) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1384 | const std::string& Name = NewName.empty() |
| 1385 | ? ("-" + D.Name) |
| 1386 | : NewName; |
| 1387 | |
| 1388 | switch (D.Type) { |
| 1389 | case OptionType::Switch: |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1390 | O << Indent << "vec.push_back(\"" << Name << "\");\n"; |
| 1391 | break; |
| 1392 | case OptionType::Parameter: |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1393 | O << Indent << "vec.push_back(\"" << Name << "\");\n"; |
| 1394 | O << Indent << "vec.push_back(" << D.GenVariableName() << ");\n"; |
| 1395 | break; |
| 1396 | case OptionType::Prefix: |
| 1397 | O << Indent << "vec.push_back(\"" << Name << "\" + " |
| 1398 | << D.GenVariableName() << ");\n"; |
| 1399 | break; |
| 1400 | case OptionType::PrefixList: |
| 1401 | O << Indent << "for (" << D.GenTypeDeclaration() |
| 1402 | << "::iterator B = " << D.GenVariableName() << ".begin(),\n" |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 1403 | << Indent << "E = " << D.GenVariableName() << ".end(); B != E;) {\n" |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1404 | << Indent << Indent1 << "vec.push_back(\"" << Name << "\" + " |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 1405 | << "*B);\n" |
| 1406 | << Indent << Indent1 << "++B;\n"; |
| 1407 | |
| 1408 | for (int i = 1, j = D.MultiVal; i < j; ++i) { |
| 1409 | O << Indent << Indent1 << "vec.push_back(*B);\n" |
| 1410 | << Indent << Indent1 << "++B;\n"; |
| 1411 | } |
| 1412 | |
| 1413 | O << Indent << "}\n"; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1414 | break; |
| 1415 | case OptionType::ParameterList: |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1416 | O << Indent << "for (" << D.GenTypeDeclaration() |
| 1417 | << "::iterator B = " << D.GenVariableName() << ".begin(),\n" |
| 1418 | << Indent << "E = " << D.GenVariableName() |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 1419 | << ".end() ; B != E;) {\n" |
| 1420 | << Indent << Indent1 << "vec.push_back(\"" << Name << "\");\n"; |
| 1421 | |
| 1422 | for (int i = 0, j = D.MultiVal; i < j; ++i) { |
| 1423 | O << Indent << Indent1 << "vec.push_back(*B);\n" |
| 1424 | << Indent << Indent1 << "++B;\n"; |
| 1425 | } |
| 1426 | |
| 1427 | O << Indent << "}\n"; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1428 | break; |
| 1429 | case OptionType::Alias: |
| 1430 | default: |
| 1431 | throw std::string("Aliases are not allowed in tool option descriptions!"); |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | /// EmitActionHandler - Emit code that handles actions. Used by |
| 1436 | /// EmitGenerateActionMethod() as an argument to |
| 1437 | /// EmitCaseConstructHandler(). |
| 1438 | class EmitActionHandler { |
| 1439 | const OptionDescriptions& OptDescs; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1440 | |
Mikhail Glushenkov | 0850949 | 2008-12-07 16:42:22 +0000 | [diff] [blame] | 1441 | void processActionDag(const Init* Statement, const char* IndentLevel, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1442 | raw_ostream& O) const |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1443 | { |
| 1444 | const DagInit& Dag = InitPtrToDag(Statement); |
| 1445 | const std::string& ActionName = Dag.getOperator()->getAsString(); |
| 1446 | |
| 1447 | if (ActionName == "append_cmd") { |
| 1448 | checkNumberOfArguments(&Dag, 1); |
| 1449 | const std::string& Cmd = InitPtrToString(Dag.getArg(0)); |
Mikhail Glushenkov | c52551d | 2009-02-27 06:46:55 +0000 | [diff] [blame] | 1450 | StrVector Out; |
| 1451 | llvm::SplitString(Cmd, Out); |
| 1452 | |
| 1453 | for (StrVector::const_iterator B = Out.begin(), E = Out.end(); |
| 1454 | B != E; ++B) |
| 1455 | O << IndentLevel << "vec.push_back(\"" << *B << "\");\n"; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1456 | } |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 1457 | else if (ActionName == "error") { |
| 1458 | O << IndentLevel << "throw std::runtime_error(\"" << |
| 1459 | (Dag.getNumArgs() >= 1 ? InitPtrToString(Dag.getArg(0)) |
| 1460 | : "Unknown error!") |
| 1461 | << "\");\n"; |
| 1462 | } |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1463 | else if (ActionName == "forward") { |
| 1464 | checkNumberOfArguments(&Dag, 1); |
| 1465 | const std::string& Name = InitPtrToString(Dag.getArg(0)); |
| 1466 | EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name), |
| 1467 | IndentLevel, "", O); |
| 1468 | } |
| 1469 | else if (ActionName == "forward_as") { |
| 1470 | checkNumberOfArguments(&Dag, 2); |
| 1471 | const std::string& Name = InitPtrToString(Dag.getArg(0)); |
Mikhail Glushenkov | e89331b | 2009-05-06 01:41:19 +0000 | [diff] [blame] | 1472 | const std::string& NewName = InitPtrToString(Dag.getArg(1)); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1473 | EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name), |
| 1474 | IndentLevel, NewName, O); |
| 1475 | } |
| 1476 | else if (ActionName == "output_suffix") { |
| 1477 | checkNumberOfArguments(&Dag, 1); |
| 1478 | const std::string& OutSuf = InitPtrToString(Dag.getArg(0)); |
| 1479 | O << IndentLevel << "output_suffix = \"" << OutSuf << "\";\n"; |
| 1480 | } |
| 1481 | else if (ActionName == "stop_compilation") { |
| 1482 | O << IndentLevel << "stop_compilation = true;\n"; |
| 1483 | } |
| 1484 | else if (ActionName == "unpack_values") { |
| 1485 | checkNumberOfArguments(&Dag, 1); |
| 1486 | const std::string& Name = InitPtrToString(Dag.getArg(0)); |
| 1487 | const OptionDescription& D = OptDescs.FindOption(Name); |
| 1488 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 1489 | if (D.isMultiVal()) |
| 1490 | throw std::string("Can't use unpack_values with multi-valued options!"); |
| 1491 | |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 1492 | if (D.isList()) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1493 | O << IndentLevel << "for (" << D.GenTypeDeclaration() |
| 1494 | << "::iterator B = " << D.GenVariableName() << ".begin(),\n" |
| 1495 | << IndentLevel << "E = " << D.GenVariableName() |
| 1496 | << ".end(); B != E; ++B)\n" |
| 1497 | << IndentLevel << Indent1 << "llvm::SplitString(*B, vec, \",\");\n"; |
| 1498 | } |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 1499 | else if (D.isParameter()){ |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1500 | O << Indent3 << "llvm::SplitString(" |
| 1501 | << D.GenVariableName() << ", vec, \",\");\n"; |
| 1502 | } |
| 1503 | else { |
| 1504 | throw "Option '" + D.Name + |
| 1505 | "': switches can't have the 'unpack_values' property!"; |
| 1506 | } |
| 1507 | } |
| 1508 | else { |
| 1509 | throw "Unknown action name: " + ActionName + "!"; |
| 1510 | } |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1511 | } |
Mikhail Glushenkov | 0850949 | 2008-12-07 16:42:22 +0000 | [diff] [blame] | 1512 | public: |
| 1513 | EmitActionHandler(const OptionDescriptions& OD) |
| 1514 | : OptDescs(OD) {} |
| 1515 | |
| 1516 | void operator()(const Init* Statement, const char* IndentLevel, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1517 | raw_ostream& O) const |
Mikhail Glushenkov | 0850949 | 2008-12-07 16:42:22 +0000 | [diff] [blame] | 1518 | { |
| 1519 | if (typeid(*Statement) == typeid(ListInit)) { |
| 1520 | const ListInit& DagList = *static_cast<const ListInit*>(Statement); |
| 1521 | for (ListInit::const_iterator B = DagList.begin(), E = DagList.end(); |
| 1522 | B != E; ++B) |
| 1523 | this->processActionDag(*B, IndentLevel, O); |
| 1524 | } |
| 1525 | else { |
| 1526 | this->processActionDag(Statement, IndentLevel, O); |
| 1527 | } |
| 1528 | } |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1529 | }; |
| 1530 | |
| 1531 | // EmitGenerateActionMethod - Emit one of two versions of the |
| 1532 | // Tool::GenerateAction() method. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1533 | void EmitGenerateActionMethod (const ToolDescription& D, |
| 1534 | const OptionDescriptions& OptDescs, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1535 | bool IsJoin, raw_ostream& O) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1536 | if (IsJoin) |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1537 | O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"; |
| 1538 | else |
| 1539 | O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n"; |
| 1540 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1541 | O << Indent2 << "bool HasChildren,\n" |
| 1542 | << Indent2 << "const llvm::sys::Path& TempDir,\n" |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 1543 | << Indent2 << "const InputLanguagesSet& InLangs,\n" |
| 1544 | << Indent2 << "const LanguageMap& LangMap) const\n" |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1545 | << Indent1 << "{\n" |
Mikhail Glushenkov | c389e94 | 2008-11-08 19:43:32 +0000 | [diff] [blame] | 1546 | << Indent2 << "std::string cmd;\n" |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1547 | << Indent2 << "std::vector<std::string> vec;\n" |
| 1548 | << Indent2 << "bool stop_compilation = !HasChildren;\n" |
| 1549 | << Indent2 << "const char* output_suffix = \"" << D.OutputSuffix << "\";\n" |
| 1550 | << Indent2 << "std::string out_file;\n\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1551 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1552 | // For every understood option, emit handling code. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1553 | if (D.Actions) |
| 1554 | EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandler(OptDescs), |
| 1555 | false, OptDescs, O); |
| 1556 | |
| 1557 | O << '\n' << Indent2 |
| 1558 | << "out_file = OutFilename(" << (IsJoin ? "sys::Path(),\n" : "inFile,\n") |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame^] | 1559 | << Indent3 << "TempDir, stop_compilation, output_suffix).str();\n\n"; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1560 | |
| 1561 | // cmd_line is either a string or a 'case' construct. |
| 1562 | if (!D.CmdLine) |
| 1563 | throw "Tool " + D.Name + " has no cmd_line property!"; |
| 1564 | else if (typeid(*D.CmdLine) == typeid(StringInit)) |
| 1565 | EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O); |
| 1566 | else |
| 1567 | EmitCaseConstructHandler(D.CmdLine, Indent2, |
| 1568 | EmitCmdLineVecFillCallback(IsJoin, D.Name), |
| 1569 | true, OptDescs, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1570 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1571 | // Handle the Sink property. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1572 | if (D.isSink()) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1573 | O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n" |
| 1574 | << Indent3 << "vec.insert(vec.end(), " |
| 1575 | << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n" |
| 1576 | << Indent2 << "}\n"; |
| 1577 | } |
| 1578 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1579 | O << Indent2 << "return Action(cmd, vec, stop_compilation, out_file);\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1580 | << Indent1 << "}\n\n"; |
| 1581 | } |
| 1582 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1583 | /// EmitGenerateActionMethods - Emit two GenerateAction() methods for |
| 1584 | /// a given Tool class. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1585 | void EmitGenerateActionMethods (const ToolDescription& ToolDesc, |
| 1586 | const OptionDescriptions& OptDescs, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1587 | raw_ostream& O) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1588 | if (!ToolDesc.isJoin()) |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1589 | O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n" |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1590 | << Indent2 << "bool HasChildren,\n" |
| 1591 | << Indent2 << "const llvm::sys::Path& TempDir,\n" |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 1592 | << Indent2 << "const InputLanguagesSet& InLangs,\n" |
| 1593 | << Indent2 << "const LanguageMap& LangMap) const\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1594 | << Indent1 << "{\n" |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1595 | << Indent2 << "throw std::runtime_error(\"" << ToolDesc.Name |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1596 | << " is not a Join tool!\");\n" |
| 1597 | << Indent1 << "}\n\n"; |
| 1598 | else |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1599 | EmitGenerateActionMethod(ToolDesc, OptDescs, true, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1600 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1601 | EmitGenerateActionMethod(ToolDesc, OptDescs, false, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1604 | /// EmitInOutLanguageMethods - Emit the [Input,Output]Language() |
| 1605 | /// methods for a given Tool class. |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1606 | void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) { |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 1607 | O << Indent1 << "const char** InputLanguages() const {\n" |
| 1608 | << Indent2 << "return InputLanguages_;\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1609 | << Indent1 << "}\n\n"; |
| 1610 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1611 | if (D.OutLanguage.empty()) |
| 1612 | throw "Tool " + D.Name + " has no 'out_language' property!"; |
| 1613 | |
Mikhail Glushenkov | b96cb60 | 2008-05-06 17:24:26 +0000 | [diff] [blame] | 1614 | O << Indent1 << "const char* OutputLanguage() const {\n" |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1615 | << Indent2 << "return \"" << D.OutLanguage << "\";\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1616 | << Indent1 << "}\n\n"; |
| 1617 | } |
| 1618 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1619 | /// EmitNameMethod - Emit the Name() method for a given Tool class. |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1620 | void EmitNameMethod (const ToolDescription& D, raw_ostream& O) { |
Mikhail Glushenkov | b96cb60 | 2008-05-06 17:24:26 +0000 | [diff] [blame] | 1621 | O << Indent1 << "const char* Name() const {\n" |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1622 | << Indent2 << "return \"" << D.Name << "\";\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1623 | << Indent1 << "}\n\n"; |
| 1624 | } |
| 1625 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1626 | /// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool |
| 1627 | /// class. |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1628 | void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1629 | O << Indent1 << "bool IsJoin() const {\n"; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1630 | if (D.isJoin()) |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1631 | O << Indent2 << "return true;\n"; |
| 1632 | else |
| 1633 | O << Indent2 << "return false;\n"; |
| 1634 | O << Indent1 << "}\n\n"; |
| 1635 | } |
| 1636 | |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 1637 | /// EmitStaticMemberDefinitions - Emit static member definitions for a |
| 1638 | /// given Tool class. |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1639 | void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1640 | if (D.InLanguage.empty()) |
| 1641 | throw "Tool " + D.Name + " has no 'in_language' property!"; |
| 1642 | |
| 1643 | O << "const char* " << D.Name << "::InputLanguages_[] = {"; |
| 1644 | for (StrVector::const_iterator B = D.InLanguage.begin(), |
| 1645 | E = D.InLanguage.end(); B != E; ++B) |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 1646 | O << '\"' << *B << "\", "; |
| 1647 | O << "0};\n\n"; |
| 1648 | } |
| 1649 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1650 | /// EmitToolClassDefinition - Emit a Tool class definition. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1651 | void EmitToolClassDefinition (const ToolDescription& D, |
| 1652 | const OptionDescriptions& OptDescs, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1653 | raw_ostream& O) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1654 | if (D.Name == "root") |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1655 | return; |
| 1656 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1657 | // Header |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1658 | O << "class " << D.Name << " : public "; |
| 1659 | if (D.isJoin()) |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 1660 | O << "JoinTool"; |
| 1661 | else |
| 1662 | O << "Tool"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1663 | |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 1664 | O << "{\nprivate:\n" |
| 1665 | << Indent1 << "static const char* InputLanguages_[];\n\n"; |
| 1666 | |
| 1667 | O << "public:\n"; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1668 | EmitNameMethod(D, O); |
| 1669 | EmitInOutLanguageMethods(D, O); |
| 1670 | EmitIsJoinMethod(D, O); |
| 1671 | EmitGenerateActionMethods(D, OptDescs, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1672 | |
| 1673 | // Close class definition |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 1674 | O << "};\n"; |
| 1675 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1676 | EmitStaticMemberDefinitions(D, O); |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 1677 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
Mikhail Glushenkov | 7c8deb3 | 2009-06-23 20:45:07 +0000 | [diff] [blame] | 1680 | /// EmitOptionDefinitions - Iterate over a list of option descriptions |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1681 | /// and emit registration code. |
Mikhail Glushenkov | 7c8deb3 | 2009-06-23 20:45:07 +0000 | [diff] [blame] | 1682 | void EmitOptionDefinitions (const OptionDescriptions& descs, |
| 1683 | bool HasSink, bool HasExterns, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1684 | raw_ostream& O) |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1685 | { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1686 | std::vector<OptionDescription> Aliases; |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 1687 | |
Mikhail Glushenkov | 34f3762 | 2008-05-30 06:23:29 +0000 | [diff] [blame] | 1688 | // Emit static cl::Option variables. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1689 | for (OptionDescriptions::const_iterator B = descs.begin(), |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1690 | E = descs.end(); B!=E; ++B) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1691 | const OptionDescription& val = B->second; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1692 | |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 1693 | if (val.Type == OptionType::Alias) { |
| 1694 | Aliases.push_back(val); |
| 1695 | continue; |
| 1696 | } |
| 1697 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1698 | if (val.isExtern()) |
| 1699 | O << "extern "; |
| 1700 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1701 | O << val.GenTypeDeclaration() << ' ' |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1702 | << val.GenVariableName(); |
| 1703 | |
| 1704 | if (val.isExtern()) { |
| 1705 | O << ";\n"; |
| 1706 | continue; |
| 1707 | } |
| 1708 | |
Mikhail Glushenkov | 0cbb590 | 2009-06-23 20:45:31 +0000 | [diff] [blame] | 1709 | O << "(\"" << val.Name << "\"\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1710 | |
| 1711 | if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList) |
| 1712 | O << ", cl::Prefix"; |
| 1713 | |
| 1714 | if (val.isRequired()) { |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 1715 | if (val.isList() && !val.isMultiVal()) |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1716 | O << ", cl::OneOrMore"; |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 1717 | else |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1718 | O << ", cl::Required"; |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 1719 | } |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 1720 | else if (val.isOneOrMore() && val.isList()) { |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 1721 | O << ", cl::OneOrMore"; |
| 1722 | } |
Mikhail Glushenkov | cbc360d | 2009-07-07 16:08:11 +0000 | [diff] [blame] | 1723 | else if (val.isZeroOrOne() && val.isList()) { |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 1724 | O << ", cl::ZeroOrOne"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 1727 | if (val.isReallyHidden()) { |
| 1728 | O << ", cl::ReallyHidden"; |
Mikhail Glushenkov | 739c720 | 2008-11-28 00:13:25 +0000 | [diff] [blame] | 1729 | } |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 1730 | else if (val.isHidden()) { |
| 1731 | O << ", cl::Hidden"; |
| 1732 | } |
| 1733 | |
| 1734 | if (val.MultiVal > 1) |
Mikhail Glushenkov | 8fe4447 | 2009-07-07 16:08:41 +0000 | [diff] [blame] | 1735 | O << ", cl::multi_val(" << val.MultiVal << ')'; |
| 1736 | |
| 1737 | if (val.InitVal) { |
| 1738 | const std::string& str = val.InitVal->getAsString(); |
| 1739 | O << ", cl::init(" << str << ')'; |
| 1740 | } |
Mikhail Glushenkov | 739c720 | 2008-11-28 00:13:25 +0000 | [diff] [blame] | 1741 | |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 1742 | if (!val.Help.empty()) |
| 1743 | O << ", cl::desc(\"" << val.Help << "\")"; |
| 1744 | |
Mikhail Glushenkov | 0cbb590 | 2009-06-23 20:45:31 +0000 | [diff] [blame] | 1745 | O << ");\n\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 1748 | // Emit the aliases (they should go after all the 'proper' options). |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1749 | for (std::vector<OptionDescription>::const_iterator |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 1750 | B = Aliases.begin(), E = Aliases.end(); B != E; ++B) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1751 | const OptionDescription& val = *B; |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 1752 | |
| 1753 | O << val.GenTypeDeclaration() << ' ' |
| 1754 | << val.GenVariableName() |
| 1755 | << "(\"" << val.Name << '\"'; |
| 1756 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1757 | const OptionDescription& D = descs.FindOption(val.Help); |
| 1758 | O << ", cl::aliasopt(" << D.GenVariableName() << ")"; |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 1759 | |
| 1760 | O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n"; |
| 1761 | } |
| 1762 | |
| 1763 | // Emit the sink option. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1764 | if (HasSink) |
Mikhail Glushenkov | b59dbad | 2008-12-07 16:42:47 +0000 | [diff] [blame] | 1765 | O << (HasExterns ? "extern cl" : "cl") |
| 1766 | << "::list<std::string> " << SinkOptionName |
| 1767 | << (HasExterns ? ";\n" : "(cl::Sink);\n"); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1768 | |
| 1769 | O << '\n'; |
| 1770 | } |
| 1771 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1772 | /// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function. |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1773 | void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O) |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1774 | { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1775 | // Generate code |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 1776 | O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1777 | |
Mikhail Glushenkov | 0108877 | 2008-11-17 17:29:18 +0000 | [diff] [blame] | 1778 | // Get the relevant field out of RecordKeeper |
Mikhail Glushenkov | fa27077 | 2008-11-17 17:29:42 +0000 | [diff] [blame] | 1779 | const Record* LangMapRecord = Records.getDef("LanguageMap"); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1780 | |
Mikhail Glushenkov | 0108877 | 2008-11-17 17:29:18 +0000 | [diff] [blame] | 1781 | // It is allowed for a plugin to have no language map. |
| 1782 | if (LangMapRecord) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1783 | |
Mikhail Glushenkov | 0108877 | 2008-11-17 17:29:18 +0000 | [diff] [blame] | 1784 | ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map"); |
| 1785 | if (!LangsToSuffixesList) |
| 1786 | throw std::string("Error in the language map definition!"); |
| 1787 | |
| 1788 | for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) { |
Mikhail Glushenkov | fa27077 | 2008-11-17 17:29:42 +0000 | [diff] [blame] | 1789 | const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i); |
Mikhail Glushenkov | 0108877 | 2008-11-17 17:29:18 +0000 | [diff] [blame] | 1790 | |
| 1791 | const std::string& Lang = LangToSuffixes->getValueAsString("lang"); |
| 1792 | const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes"); |
| 1793 | |
| 1794 | for (unsigned i = 0; i < Suffixes->size(); ++i) |
| 1795 | O << Indent1 << "langMap[\"" |
| 1796 | << InitPtrToString(Suffixes->getElement(i)) |
| 1797 | << "\"] = \"" << Lang << "\";\n"; |
| 1798 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Mikhail Glushenkov | e1d44b5 | 2008-12-11 10:34:18 +0000 | [diff] [blame] | 1801 | O << "}\n\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1804 | /// IncDecWeight - Helper function passed to EmitCaseConstructHandler() |
| 1805 | /// by EmitEdgeClass(). |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1806 | void IncDecWeight (const Init* i, const char* IndentLevel, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1807 | raw_ostream& O) { |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 1808 | const DagInit& d = InitPtrToDag(i); |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1809 | const std::string& OpName = d.getOperator()->getAsString(); |
| 1810 | |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 1811 | if (OpName == "inc_weight") { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1812 | O << IndentLevel << "ret += "; |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 1813 | } |
| 1814 | else if (OpName == "dec_weight") { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1815 | O << IndentLevel << "ret -= "; |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 1816 | } |
| 1817 | else if (OpName == "error") { |
| 1818 | O << IndentLevel << "throw std::runtime_error(\"" << |
| 1819 | (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0)) |
| 1820 | : "Unknown error!") |
| 1821 | << "\");\n"; |
| 1822 | return; |
| 1823 | } |
| 1824 | |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1825 | else |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 1826 | throw "Unknown operator in edge properties list: " + OpName + '!' + |
Mikhail Glushenkov | 65ee1e6 | 2008-12-18 04:06:58 +0000 | [diff] [blame] | 1827 | "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed."; |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1828 | |
| 1829 | if (d.getNumArgs() > 0) |
| 1830 | O << InitPtrToInt(d.getArg(0)) << ";\n"; |
| 1831 | else |
| 1832 | O << "2;\n"; |
| 1833 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1836 | /// EmitEdgeClass - Emit a single Edge# class. |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1837 | void EmitEdgeClass (unsigned N, const std::string& Target, |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1838 | DagInit* Case, const OptionDescriptions& OptDescs, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1839 | raw_ostream& O) { |
Mikhail Glushenkov | 9ef501b | 2008-05-06 17:23:14 +0000 | [diff] [blame] | 1840 | |
| 1841 | // Class constructor. |
| 1842 | O << "class Edge" << N << ": public Edge {\n" |
| 1843 | << "public:\n" |
| 1844 | << Indent1 << "Edge" << N << "() : Edge(\"" << Target |
| 1845 | << "\") {}\n\n" |
| 1846 | |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 1847 | // Function Weight(). |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 1848 | << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n" |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 1849 | << Indent2 << "unsigned ret = 0;\n"; |
Mikhail Glushenkov | 9ef501b | 2008-05-06 17:23:14 +0000 | [diff] [blame] | 1850 | |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1851 | // Handle the 'case' construct. |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1852 | EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O); |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 1853 | |
| 1854 | O << Indent2 << "return ret;\n" |
| 1855 | << Indent1 << "};\n\n};\n\n"; |
Mikhail Glushenkov | 9ef501b | 2008-05-06 17:23:14 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1858 | /// EmitEdgeClasses - Emit Edge* classes that represent graph edges. |
Mikhail Glushenkov | ad746be | 2008-11-28 00:13:47 +0000 | [diff] [blame] | 1859 | void EmitEdgeClasses (const RecordVector& EdgeVector, |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1860 | const OptionDescriptions& OptDescs, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1861 | raw_ostream& O) { |
Mikhail Glushenkov | ad746be | 2008-11-28 00:13:47 +0000 | [diff] [blame] | 1862 | int i = 0; |
Mikhail Glushenkov | 15b71ba | 2008-12-07 16:44:47 +0000 | [diff] [blame] | 1863 | for (RecordVector::const_iterator B = EdgeVector.begin(), |
| 1864 | E = EdgeVector.end(); B != E; ++B) { |
| 1865 | const Record* Edge = *B; |
| 1866 | const std::string& NodeB = Edge->getValueAsString("b"); |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1867 | DagInit* Weight = Edge->getValueAsDag("weight"); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1868 | |
Mikhail Glushenkov | ad746be | 2008-11-28 00:13:47 +0000 | [diff] [blame] | 1869 | if (!isDagEmpty(Weight)) |
Mikhail Glushenkov | 15b71ba | 2008-12-07 16:44:47 +0000 | [diff] [blame] | 1870 | EmitEdgeClass(i, NodeB, Weight, OptDescs, O); |
Mikhail Glushenkov | ad746be | 2008-11-28 00:13:47 +0000 | [diff] [blame] | 1871 | ++i; |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1875 | /// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() |
| 1876 | /// function. |
Mikhail Glushenkov | ad746be | 2008-11-28 00:13:47 +0000 | [diff] [blame] | 1877 | void EmitPopulateCompilationGraph (const RecordVector& EdgeVector, |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1878 | const ToolDescriptions& ToolDescs, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1879 | raw_ostream& O) |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1880 | { |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 1881 | O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1882 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1883 | for (ToolDescriptions::const_iterator B = ToolDescs.begin(), |
| 1884 | E = ToolDescs.end(); B != E; ++B) |
Mikhail Glushenkov | fa27077 | 2008-11-17 17:29:42 +0000 | [diff] [blame] | 1885 | O << Indent1 << "G.insertNode(new " << (*B)->Name << "());\n"; |
Mikhail Glushenkov | 262d248 | 2008-11-12 00:05:17 +0000 | [diff] [blame] | 1886 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1887 | O << '\n'; |
| 1888 | |
Mikhail Glushenkov | 262d248 | 2008-11-12 00:05:17 +0000 | [diff] [blame] | 1889 | // Insert edges. |
| 1890 | |
Mikhail Glushenkov | ad746be | 2008-11-28 00:13:47 +0000 | [diff] [blame] | 1891 | int i = 0; |
Mikhail Glushenkov | 15b71ba | 2008-12-07 16:44:47 +0000 | [diff] [blame] | 1892 | for (RecordVector::const_iterator B = EdgeVector.begin(), |
| 1893 | E = EdgeVector.end(); B != E; ++B) { |
| 1894 | const Record* Edge = *B; |
| 1895 | const std::string& NodeA = Edge->getValueAsString("a"); |
| 1896 | const std::string& NodeB = Edge->getValueAsString("b"); |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1897 | DagInit* Weight = Edge->getValueAsDag("weight"); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1898 | |
Mikhail Glushenkov | 15b71ba | 2008-12-07 16:44:47 +0000 | [diff] [blame] | 1899 | O << Indent1 << "G.insertEdge(\"" << NodeA << "\", "; |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1900 | |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1901 | if (isDagEmpty(Weight)) |
Mikhail Glushenkov | 15b71ba | 2008-12-07 16:44:47 +0000 | [diff] [blame] | 1902 | O << "new SimpleEdge(\"" << NodeB << "\")"; |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1903 | else |
| 1904 | O << "new Edge" << i << "()"; |
| 1905 | |
| 1906 | O << ");\n"; |
Mikhail Glushenkov | ad746be | 2008-11-28 00:13:47 +0000 | [diff] [blame] | 1907 | ++i; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Mikhail Glushenkov | e1d44b5 | 2008-12-11 10:34:18 +0000 | [diff] [blame] | 1910 | O << "}\n\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1913 | /// ExtractHookNames - Extract the hook names from all instances of |
| 1914 | /// $CALL(HookName) in the provided command line string. Helper |
| 1915 | /// function used by FillInHookNames(). |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1916 | class ExtractHookNames { |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1917 | llvm::StringMap<unsigned>& HookNames_; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1918 | public: |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1919 | ExtractHookNames(llvm::StringMap<unsigned>& HookNames) |
| 1920 | : HookNames_(HookNames) {} |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1921 | |
| 1922 | void operator()(const Init* CmdLine) { |
| 1923 | StrVector cmds; |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1924 | TokenizeCmdline(InitPtrToString(CmdLine), cmds); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1925 | for (StrVector::const_iterator B = cmds.begin(), E = cmds.end(); |
| 1926 | B != E; ++B) { |
| 1927 | const std::string& cmd = *B; |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1928 | |
| 1929 | if (cmd == "$CALL") { |
| 1930 | unsigned NumArgs = 0; |
| 1931 | checkedIncrement(B, E, "Syntax error in $CALL invocation!"); |
| 1932 | const std::string& HookName = *B; |
| 1933 | |
| 1934 | |
| 1935 | if (HookName.at(0) == ')') |
| 1936 | throw "$CALL invoked with no arguments!"; |
| 1937 | |
| 1938 | while (++B != E && B->at(0) != ')') { |
| 1939 | ++NumArgs; |
| 1940 | } |
| 1941 | |
| 1942 | StringMap<unsigned>::const_iterator H = HookNames_.find(HookName); |
| 1943 | |
| 1944 | if (H != HookNames_.end() && H->second != NumArgs) |
| 1945 | throw "Overloading of hooks is not allowed. Overloaded hook: " |
| 1946 | + HookName; |
| 1947 | else |
| 1948 | HookNames_[HookName] = NumArgs; |
| 1949 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1950 | } |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1951 | } |
| 1952 | } |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1953 | }; |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1954 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1955 | /// FillInHookNames - Actually extract the hook names from all command |
| 1956 | /// line strings. Helper function used by EmitHookDeclarations(). |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1957 | void FillInHookNames(const ToolDescriptions& ToolDescs, |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1958 | llvm::StringMap<unsigned>& HookNames) |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1959 | { |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1960 | // For all command lines: |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1961 | for (ToolDescriptions::const_iterator B = ToolDescs.begin(), |
| 1962 | E = ToolDescs.end(); B != E; ++B) { |
| 1963 | const ToolDescription& D = *(*B); |
| 1964 | if (!D.CmdLine) |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1965 | continue; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1966 | if (dynamic_cast<StringInit*>(D.CmdLine)) |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1967 | // This is a string. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1968 | ExtractHookNames(HookNames).operator()(D.CmdLine); |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1969 | else |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1970 | // This is a 'case' construct. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1971 | WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames)); |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | /// EmitHookDeclarations - Parse CmdLine fields of all the tool |
| 1976 | /// property records and emit hook function declaration for each |
| 1977 | /// instance of $CALL(HookName). |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1978 | void EmitHookDeclarations(const ToolDescriptions& ToolDescs, raw_ostream& O) { |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1979 | llvm::StringMap<unsigned> HookNames; |
| 1980 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1981 | FillInHookNames(ToolDescs, HookNames); |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1982 | if (HookNames.empty()) |
| 1983 | return; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1984 | |
| 1985 | O << "namespace hooks {\n"; |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1986 | for (StringMap<unsigned>::const_iterator B = HookNames.begin(), |
| 1987 | E = HookNames.end(); B != E; ++B) { |
Mikhail Glushenkov | b6b5141 | 2009-01-21 13:04:33 +0000 | [diff] [blame] | 1988 | O << Indent1 << "std::string " << B->first() << "("; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1989 | |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 1990 | for (unsigned i = 0, j = B->second; i < j; ++i) { |
| 1991 | O << "const char* Arg" << i << (i+1 == j ? "" : ", "); |
| 1992 | } |
| 1993 | |
| 1994 | O <<");\n"; |
| 1995 | } |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1996 | O << "}\n\n"; |
| 1997 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1998 | |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 1999 | /// EmitRegisterPlugin - Emit code to register this plugin. |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 2000 | void EmitRegisterPlugin(int Priority, raw_ostream& O) { |
Mikhail Glushenkov | e1d44b5 | 2008-12-11 10:34:18 +0000 | [diff] [blame] | 2001 | O << "struct Plugin : public llvmc::BasePlugin {\n\n" |
Mikhail Glushenkov | 35fde15 | 2008-11-17 17:30:25 +0000 | [diff] [blame] | 2002 | << Indent1 << "int Priority() const { return " << Priority << "; }\n\n" |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 2003 | << Indent1 << "void PopulateLanguageMap(LanguageMap& langMap) const\n" |
| 2004 | << Indent1 << "{ PopulateLanguageMapLocal(langMap); }\n\n" |
| 2005 | << Indent1 |
| 2006 | << "void PopulateCompilationGraph(CompilationGraph& graph) const\n" |
| 2007 | << Indent1 << "{ PopulateCompilationGraphLocal(graph); }\n" |
| 2008 | << "};\n\n" |
| 2009 | |
Mikhail Glushenkov | e1d44b5 | 2008-12-11 10:34:18 +0000 | [diff] [blame] | 2010 | << "static llvmc::RegisterPlugin<Plugin> RP;\n\n"; |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
Mikhail Glushenkov | 6766572 | 2008-11-12 12:41:18 +0000 | [diff] [blame] | 2013 | /// EmitIncludes - Emit necessary #include directives and some |
| 2014 | /// additional declarations. |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 2015 | void EmitIncludes(raw_ostream& O) { |
Mikhail Glushenkov | 4a1a77c | 2008-09-22 20:50:40 +0000 | [diff] [blame] | 2016 | O << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n" |
Mikhail Glushenkov | d80d869 | 2009-06-23 20:46:48 +0000 | [diff] [blame] | 2017 | << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n" |
Mikhail Glushenkov | 4a1a77c | 2008-09-22 20:50:40 +0000 | [diff] [blame] | 2018 | << "#include \"llvm/CompilerDriver/Plugin.h\"\n" |
| 2019 | << "#include \"llvm/CompilerDriver/Tool.h\"\n\n" |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 2020 | |
| 2021 | << "#include \"llvm/ADT/StringExtras.h\"\n" |
| 2022 | << "#include \"llvm/Support/CommandLine.h\"\n\n" |
| 2023 | |
| 2024 | << "#include <cstdlib>\n" |
| 2025 | << "#include <stdexcept>\n\n" |
| 2026 | |
| 2027 | << "using namespace llvm;\n" |
| 2028 | << "using namespace llvmc;\n\n" |
| 2029 | |
Mikhail Glushenkov | 6766572 | 2008-11-12 12:41:18 +0000 | [diff] [blame] | 2030 | << "extern cl::opt<std::string> OutputFilename;\n\n" |
| 2031 | |
| 2032 | << "inline const char* checkCString(const char* s)\n" |
| 2033 | << "{ return s == NULL ? \"\" : s; }\n\n"; |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Mikhail Glushenkov | fa27077 | 2008-11-17 17:29:42 +0000 | [diff] [blame] | 2036 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2037 | /// PluginData - Holds all information about a plugin. |
| 2038 | struct PluginData { |
| 2039 | OptionDescriptions OptDescs; |
| 2040 | bool HasSink; |
Mikhail Glushenkov | b59dbad | 2008-12-07 16:42:47 +0000 | [diff] [blame] | 2041 | bool HasExterns; |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2042 | ToolDescriptions ToolDescs; |
| 2043 | RecordVector Edges; |
| 2044 | int Priority; |
Mikhail Glushenkov | fa27077 | 2008-11-17 17:29:42 +0000 | [diff] [blame] | 2045 | }; |
| 2046 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2047 | /// HasSink - Go through the list of tool descriptions and check if |
Mikhail Glushenkov | b59dbad | 2008-12-07 16:42:47 +0000 | [diff] [blame] | 2048 | /// there are any with the 'sink' property set. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2049 | bool HasSink(const ToolDescriptions& ToolDescs) { |
| 2050 | for (ToolDescriptions::const_iterator B = ToolDescs.begin(), |
| 2051 | E = ToolDescs.end(); B != E; ++B) |
| 2052 | if ((*B)->isSink()) |
| 2053 | return true; |
Mikhail Glushenkov | fa27077 | 2008-11-17 17:29:42 +0000 | [diff] [blame] | 2054 | |
Mikhail Glushenkov | b59dbad | 2008-12-07 16:42:47 +0000 | [diff] [blame] | 2055 | return false; |
| 2056 | } |
| 2057 | |
| 2058 | /// HasExterns - Go through the list of option descriptions and check |
| 2059 | /// if there are any external options. |
| 2060 | bool HasExterns(const OptionDescriptions& OptDescs) { |
| 2061 | for (OptionDescriptions::const_iterator B = OptDescs.begin(), |
| 2062 | E = OptDescs.end(); B != E; ++B) |
| 2063 | if (B->second.isExtern()) |
| 2064 | return true; |
| 2065 | |
| 2066 | return false; |
Mikhail Glushenkov | fa27077 | 2008-11-17 17:29:42 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2069 | /// CollectPluginData - Collect tool and option properties, |
| 2070 | /// compilation graph edges and plugin priority from the parse tree. |
| 2071 | void CollectPluginData (const RecordKeeper& Records, PluginData& Data) { |
| 2072 | // Collect option properties. |
| 2073 | const RecordVector& OptionLists = |
| 2074 | Records.getAllDerivedDefinitions("OptionList"); |
| 2075 | CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(), |
| 2076 | Data.OptDescs); |
| 2077 | |
| 2078 | // Collect tool properties. |
| 2079 | const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool"); |
| 2080 | CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs); |
| 2081 | Data.HasSink = HasSink(Data.ToolDescs); |
Mikhail Glushenkov | b59dbad | 2008-12-07 16:42:47 +0000 | [diff] [blame] | 2082 | Data.HasExterns = HasExterns(Data.OptDescs); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2083 | |
| 2084 | // Collect compilation graph edges. |
| 2085 | const RecordVector& CompilationGraphs = |
| 2086 | Records.getAllDerivedDefinitions("CompilationGraph"); |
| 2087 | FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(), |
| 2088 | Data.Edges); |
| 2089 | |
| 2090 | // Calculate the priority of this plugin. |
| 2091 | const RecordVector& Priorities = |
| 2092 | Records.getAllDerivedDefinitions("PluginPriority"); |
| 2093 | Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end()); |
Mikhail Glushenkov | 35fde15 | 2008-11-17 17:30:25 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2096 | /// CheckPluginData - Perform some sanity checks on the collected data. |
| 2097 | void CheckPluginData(PluginData& Data) { |
| 2098 | // Filter out all tools not mentioned in the compilation graph. |
| 2099 | FilterNotInGraph(Data.Edges, Data.ToolDescs); |
Mikhail Glushenkov | ad746be | 2008-11-28 00:13:47 +0000 | [diff] [blame] | 2100 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2101 | // Typecheck the compilation graph. |
| 2102 | TypecheckGraph(Data.Edges, Data.ToolDescs); |
| 2103 | |
| 2104 | // Check that there are no options without side effects (specified |
| 2105 | // only in the OptionList). |
| 2106 | CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs); |
| 2107 | |
Mikhail Glushenkov | ad746be | 2008-11-28 00:13:47 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 2110 | void EmitPluginCode(const PluginData& Data, raw_ostream& O) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2111 | // Emit file header. |
| 2112 | EmitIncludes(O); |
| 2113 | |
| 2114 | // Emit global option registration code. |
Mikhail Glushenkov | 7c8deb3 | 2009-06-23 20:45:07 +0000 | [diff] [blame] | 2115 | EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2116 | |
| 2117 | // Emit hook declarations. |
| 2118 | EmitHookDeclarations(Data.ToolDescs, O); |
| 2119 | |
Mikhail Glushenkov | e1d44b5 | 2008-12-11 10:34:18 +0000 | [diff] [blame] | 2120 | O << "namespace {\n\n"; |
| 2121 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2122 | // Emit PopulateLanguageMap() function |
| 2123 | // (a language map maps from file extensions to language names). |
| 2124 | EmitPopulateLanguageMap(Records, O); |
| 2125 | |
| 2126 | // Emit Tool classes. |
| 2127 | for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(), |
| 2128 | E = Data.ToolDescs.end(); B!=E; ++B) |
| 2129 | EmitToolClassDefinition(*(*B), Data.OptDescs, O); |
| 2130 | |
| 2131 | // Emit Edge# classes. |
| 2132 | EmitEdgeClasses(Data.Edges, Data.OptDescs, O); |
| 2133 | |
| 2134 | // Emit PopulateCompilationGraph() function. |
| 2135 | EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O); |
| 2136 | |
| 2137 | // Emit code for plugin registration. |
| 2138 | EmitRegisterPlugin(Data.Priority, O); |
| 2139 | |
Mikhail Glushenkov | d80d869 | 2009-06-23 20:46:48 +0000 | [diff] [blame] | 2140 | O << "} // End anonymous namespace.\n\n"; |
| 2141 | |
| 2142 | // Force linkage magic. |
| 2143 | O << "namespace llvmc {\n"; |
| 2144 | O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n"; |
| 2145 | O << "}\n"; |
| 2146 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2147 | // EOF |
| 2148 | } |
| 2149 | |
| 2150 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 2151 | // End of anonymous namespace |
Mikhail Glushenkov | 895820d | 2008-05-06 18:12:03 +0000 | [diff] [blame] | 2152 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 2153 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 2154 | /// run - The back-end entry point. |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 2155 | void LLVMCConfigurationEmitter::run (raw_ostream &O) { |
Mikhail Glushenkov | 4fb71ea | 2008-05-30 06:21:48 +0000 | [diff] [blame] | 2156 | try { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2157 | PluginData Data; |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 2158 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2159 | CollectPluginData(Records, Data); |
| 2160 | CheckPluginData(Data); |
| 2161 | |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 2162 | EmitSourceFileHeader("LLVMC Configuration Library", O); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 2163 | EmitPluginCode(Data, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 2164 | |
Mikhail Glushenkov | 4fb71ea | 2008-05-30 06:21:48 +0000 | [diff] [blame] | 2165 | } catch (std::exception& Error) { |
| 2166 | throw Error.what() + std::string(" - usually this means a syntax error."); |
| 2167 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 2168 | } |