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