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