2008-05-30 06:20:54 +00:00
|
|
|
//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
|
2008-03-23 08:57:20 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open
|
|
|
|
// Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-05-06 18:08:59 +00:00
|
|
|
// This tablegen backend is responsible for emitting LLVMC configuration code.
|
2008-03-23 08:57:20 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-06 18:09:29 +00:00
|
|
|
#include "LLVMCConfigurationEmitter.h"
|
2008-03-23 08:57:20 +00:00
|
|
|
#include "Record.h"
|
|
|
|
|
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2008-05-30 06:18:16 +00:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2009-12-23 12:49:30 +00:00
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <functional>
|
2008-05-30 06:21:48 +00:00
|
|
|
#include <stdexcept>
|
2008-03-23 08:57:20 +00:00
|
|
|
#include <string>
|
2008-06-04 04:46:14 +00:00
|
|
|
#include <typeinfo>
|
2008-11-12 00:04:46 +00:00
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-12-17 07:48:34 +00:00
|
|
|
namespace {
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Typedefs
|
|
|
|
|
|
|
|
typedef std::vector<Record*> RecordVector;
|
2010-08-23 23:21:23 +00:00
|
|
|
typedef std::vector<const DagInit*> DagVector;
|
2008-03-23 08:57:20 +00:00
|
|
|
typedef std::vector<std::string> StrVector;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Constants
|
|
|
|
|
2009-09-21 15:53:44 +00:00
|
|
|
// Indentation.
|
2009-12-17 07:48:34 +00:00
|
|
|
const unsigned TabWidth = 4;
|
|
|
|
const unsigned Indent1 = TabWidth*1;
|
|
|
|
const unsigned Indent2 = TabWidth*2;
|
|
|
|
const unsigned Indent3 = TabWidth*3;
|
2010-02-23 09:04:44 +00:00
|
|
|
const unsigned Indent4 = TabWidth*4;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Default help string.
|
2009-12-17 07:48:34 +00:00
|
|
|
const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Name for the "sink" option.
|
2010-08-20 11:24:51 +00:00
|
|
|
const char * const SinkOptionName = "SinkOption";
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Helper functions
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// Id - An 'identity' function object.
|
|
|
|
struct Id {
|
2009-10-18 22:51:30 +00:00
|
|
|
template<typename T0>
|
|
|
|
void operator()(const T0&) const {
|
|
|
|
}
|
|
|
|
template<typename T0, typename T1>
|
|
|
|
void operator()(const T0&, const T1&) const {
|
|
|
|
}
|
|
|
|
template<typename T0, typename T1, typename T2>
|
|
|
|
void operator()(const T0&, const T1&, const T2&) const {
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-05-30 06:18:16 +00:00
|
|
|
int InitPtrToInt(const Init* ptr) {
|
|
|
|
const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
|
|
|
|
return val.getValue();
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:10:19 +00:00
|
|
|
const std::string& InitPtrToString(const Init* ptr) {
|
|
|
|
const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
|
2008-03-23 08:57:20 +00:00
|
|
|
return val.getValue();
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:18:16 +00:00
|
|
|
const ListInit& InitPtrToList(const Init* ptr) {
|
|
|
|
const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
|
|
|
|
return val;
|
2008-05-06 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 06:18:16 +00:00
|
|
|
const DagInit& InitPtrToDag(const Init* ptr) {
|
2008-05-30 06:10:19 +00:00
|
|
|
const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
|
2008-05-06 18:18:20 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2009-10-18 22:51:30 +00:00
|
|
|
const std::string GetOperatorName(const DagInit& D) {
|
2009-12-17 07:48:49 +00:00
|
|
|
return D.getOperator()->getAsString();
|
2009-10-18 22:51:30 +00:00
|
|
|
}
|
|
|
|
|
2009-12-23 12:49:30 +00:00
|
|
|
/// CheckBooleanConstant - Check that the provided value is a boolean constant.
|
|
|
|
void CheckBooleanConstant(const Init* I) {
|
|
|
|
const DefInit& val = dynamic_cast<const DefInit&>(*I);
|
|
|
|
const std::string& str = val.getAsString();
|
|
|
|
|
|
|
|
if (str != "true" && str != "false") {
|
|
|
|
throw "Incorrect boolean value: '" + str +
|
|
|
|
"': must be either 'true' or 'false'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-23 12:49:41 +00:00
|
|
|
// CheckNumberOfArguments - Ensure that the number of args in d is
|
2009-07-07 16:07:36 +00:00
|
|
|
// greater than or equal to min_arguments, otherwise throw an exception.
|
2009-12-23 12:49:41 +00:00
|
|
|
void CheckNumberOfArguments (const DagInit& d, unsigned minArgs) {
|
2009-12-17 07:48:49 +00:00
|
|
|
if (d.getNumArgs() < minArgs)
|
|
|
|
throw GetOperatorName(d) + ": too few arguments!";
|
2009-12-07 17:03:05 +00:00
|
|
|
}
|
2008-05-06 17:22:03 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
// EscapeVariableName - Escape commas and other symbols not allowed
|
|
|
|
// in the C++ variable names. Makes it possible to use options named
|
|
|
|
// like "Wa," (useful for prefix options).
|
2010-01-26 14:55:04 +00:00
|
|
|
std::string EscapeVariableName (const std::string& Var) {
|
2008-12-07 16:41:11 +00:00
|
|
|
std::string ret;
|
|
|
|
for (unsigned i = 0; i != Var.size(); ++i) {
|
|
|
|
char cur_char = Var[i];
|
|
|
|
if (cur_char == ',') {
|
|
|
|
ret += "_comma_";
|
|
|
|
}
|
|
|
|
else if (cur_char == '+') {
|
|
|
|
ret += "_plus_";
|
|
|
|
}
|
|
|
|
else if (cur_char == '-') {
|
|
|
|
ret += "_dash_";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret.push_back(cur_char);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-01-26 14:55:04 +00:00
|
|
|
/// EscapeQuotes - Replace '"' with '\"'.
|
|
|
|
std::string EscapeQuotes (const std::string& Var) {
|
|
|
|
std::string ret;
|
|
|
|
for (unsigned i = 0; i != Var.size(); ++i) {
|
|
|
|
char cur_char = Var[i];
|
|
|
|
if (cur_char == '"') {
|
|
|
|
ret += "\\\"";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret.push_back(cur_char);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-12-23 12:49:41 +00:00
|
|
|
/// OneOf - Does the input string contain this character?
|
|
|
|
bool OneOf(const char* lst, char c) {
|
2009-01-21 13:04:00 +00:00
|
|
|
while (*lst) {
|
|
|
|
if (*lst++ == c)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-01-28 03:47:20 +00:00
|
|
|
template <class I, class S>
|
2009-12-23 12:49:41 +00:00
|
|
|
void CheckedIncrement(I& P, I E, S ErrorString) {
|
2009-01-28 03:47:20 +00:00
|
|
|
++P;
|
|
|
|
if (P == E)
|
|
|
|
throw ErrorString;
|
|
|
|
}
|
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Back-end specific code
|
|
|
|
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// OptionType - One of six different option types. See the
|
|
|
|
/// documentation for detailed description of differences.
|
2008-03-23 08:57:20 +00:00
|
|
|
namespace OptionType {
|
2009-07-07 16:08:11 +00:00
|
|
|
|
2010-07-19 03:16:25 +00:00
|
|
|
enum OptionType { Alias, Switch, SwitchList,
|
|
|
|
Parameter, ParameterList, Prefix, PrefixList };
|
2008-12-07 16:41:11 +00:00
|
|
|
|
2009-10-21 02:13:13 +00:00
|
|
|
bool IsAlias(OptionType t) {
|
|
|
|
return (t == Alias);
|
|
|
|
}
|
|
|
|
|
2009-07-07 16:08:11 +00:00
|
|
|
bool IsList (OptionType t) {
|
2010-07-19 03:16:25 +00:00
|
|
|
return (t == SwitchList || t == ParameterList || t == PrefixList);
|
2009-07-07 16:08:11 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2009-07-07 16:08:11 +00:00
|
|
|
bool IsSwitch (OptionType t) {
|
|
|
|
return (t == Switch);
|
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
|
2010-07-19 03:16:25 +00:00
|
|
|
bool IsSwitchList (OptionType t) {
|
|
|
|
return (t == SwitchList);
|
|
|
|
}
|
|
|
|
|
2009-07-07 16:08:11 +00:00
|
|
|
bool IsParameter (OptionType t) {
|
|
|
|
return (t == Parameter || t == Prefix);
|
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionType::OptionType stringToOptionType(const std::string& T) {
|
|
|
|
if (T == "alias_option")
|
|
|
|
return OptionType::Alias;
|
|
|
|
else if (T == "switch_option")
|
|
|
|
return OptionType::Switch;
|
2010-07-19 03:16:25 +00:00
|
|
|
else if (T == "switch_list_option")
|
|
|
|
return OptionType::SwitchList;
|
2008-12-07 16:41:11 +00:00
|
|
|
else if (T == "parameter_option")
|
|
|
|
return OptionType::Parameter;
|
|
|
|
else if (T == "parameter_list_option")
|
|
|
|
return OptionType::ParameterList;
|
|
|
|
else if (T == "prefix_option")
|
|
|
|
return OptionType::Prefix;
|
|
|
|
else if (T == "prefix_list_option")
|
|
|
|
return OptionType::PrefixList;
|
|
|
|
else
|
|
|
|
throw "Unknown option type: " + T + '!';
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
namespace OptionDescriptionFlags {
|
|
|
|
enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
|
2010-08-15 07:07:12 +00:00
|
|
|
ReallyHidden = 0x4, OneOrMore = 0x8,
|
|
|
|
Optional = 0x10, CommaSeparated = 0x20,
|
|
|
|
ForwardNotSplit = 0x40, ZeroOrMore = 0x80 };
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// OptionDescription - Represents data contained in a single
|
|
|
|
/// OptionList entry.
|
2008-03-23 08:57:20 +00:00
|
|
|
struct OptionDescription {
|
|
|
|
OptionType::OptionType Type;
|
|
|
|
std::string Name;
|
2008-12-07 16:41:11 +00:00
|
|
|
unsigned Flags;
|
|
|
|
std::string Help;
|
2009-01-28 03:47:20 +00:00
|
|
|
unsigned MultiVal;
|
2009-07-07 16:08:41 +00:00
|
|
|
Init* InitVal;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
OptionDescription(OptionType::OptionType t = OptionType::Switch,
|
2008-12-07 16:41:11 +00:00
|
|
|
const std::string& n = "",
|
|
|
|
const std::string& h = DefaultHelpString)
|
2009-07-07 16:08:41 +00:00
|
|
|
: Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
|
2008-03-23 08:57:20 +00:00
|
|
|
{}
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// GenTypeDeclaration - Returns the C++ variable type of this
|
|
|
|
/// option.
|
|
|
|
const char* GenTypeDeclaration() const;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// GenVariableName - Returns the variable name used in the
|
|
|
|
/// generated C++ code.
|
2010-08-20 11:24:51 +00:00
|
|
|
std::string GenVariableName() const
|
|
|
|
{ return "autogenerated::" + GenOptionType() + EscapeVariableName(Name); }
|
|
|
|
|
|
|
|
/// GenPlainVariableName - Returns the variable name without the namespace
|
|
|
|
/// prefix.
|
|
|
|
std::string GenPlainVariableName() const
|
|
|
|
{ return GenOptionType() + EscapeVariableName(Name); }
|
2008-05-12 16:33:06 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// Merge - Merge two option descriptions.
|
|
|
|
void Merge (const OptionDescription& other);
|
|
|
|
|
2010-03-05 04:46:39 +00:00
|
|
|
/// CheckConsistency - Check that the flags are consistent.
|
|
|
|
void CheckConsistency() const;
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
// Misc convenient getters/setters.
|
|
|
|
|
|
|
|
bool isAlias() const;
|
2008-12-07 16:47:12 +00:00
|
|
|
|
2009-01-28 03:47:20 +00:00
|
|
|
bool isMultiVal() const;
|
|
|
|
|
2009-12-07 18:25:54 +00:00
|
|
|
bool isCommaSeparated() const;
|
|
|
|
void setCommaSeparated();
|
|
|
|
|
2010-02-23 09:04:13 +00:00
|
|
|
bool isForwardNotSplit() const;
|
|
|
|
void setForwardNotSplit();
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
bool isRequired() const;
|
|
|
|
void setRequired();
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2009-01-28 03:47:20 +00:00
|
|
|
bool isOneOrMore() const;
|
|
|
|
void setOneOrMore();
|
|
|
|
|
2010-03-05 04:46:39 +00:00
|
|
|
bool isZeroOrMore() const;
|
|
|
|
void setZeroOrMore();
|
|
|
|
|
2009-12-15 03:04:52 +00:00
|
|
|
bool isOptional() const;
|
|
|
|
void setOptional();
|
2009-01-28 03:47:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
bool isHidden() const;
|
|
|
|
void setHidden();
|
|
|
|
|
|
|
|
bool isReallyHidden() const;
|
|
|
|
void setReallyHidden();
|
2009-01-28 03:47:20 +00:00
|
|
|
|
2009-07-07 16:08:11 +00:00
|
|
|
bool isSwitch() const
|
|
|
|
{ return OptionType::IsSwitch(this->Type); }
|
|
|
|
|
2010-07-19 03:16:25 +00:00
|
|
|
bool isSwitchList() const
|
|
|
|
{ return OptionType::IsSwitchList(this->Type); }
|
|
|
|
|
2009-10-21 02:13:13 +00:00
|
|
|
bool isParameter() const
|
|
|
|
{ return OptionType::IsParameter(this->Type); }
|
|
|
|
|
2009-07-07 16:08:11 +00:00
|
|
|
bool isList() const
|
|
|
|
{ return OptionType::IsList(this->Type); }
|
|
|
|
|
2010-07-19 17:17:10 +00:00
|
|
|
bool isParameterList() const
|
|
|
|
{ return (OptionType::IsList(this->Type)
|
|
|
|
&& !OptionType::IsSwitchList(this->Type)); }
|
|
|
|
|
2010-08-20 11:24:51 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
// GenOptionType - Helper function used by GenVariableName().
|
|
|
|
std::string GenOptionType() const;
|
2008-03-23 08:57:20 +00:00
|
|
|
};
|
|
|
|
|
2010-03-05 04:46:39 +00:00
|
|
|
void OptionDescription::CheckConsistency() const {
|
|
|
|
unsigned i = 0;
|
|
|
|
|
|
|
|
i += this->isRequired();
|
|
|
|
i += this->isOptional();
|
|
|
|
i += this->isOneOrMore();
|
|
|
|
i += this->isZeroOrMore();
|
|
|
|
|
|
|
|
if (i > 1) {
|
|
|
|
throw "Only one of (required), (optional), (one_or_more) or "
|
|
|
|
"(zero_or_more) properties is allowed!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
void OptionDescription::Merge (const OptionDescription& other)
|
|
|
|
{
|
|
|
|
if (other.Type != Type)
|
|
|
|
throw "Conflicting definitions for the option " + Name + "!";
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
if (Help == other.Help || Help == DefaultHelpString)
|
|
|
|
Help = other.Help;
|
|
|
|
else if (other.Help != DefaultHelpString) {
|
2009-07-03 00:10:29 +00:00
|
|
|
llvm::errs() << "Warning: several different help strings"
|
2008-12-07 16:41:11 +00:00
|
|
|
" defined for option " + Name + "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
Flags |= other.Flags;
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
bool OptionDescription::isAlias() const {
|
2009-10-21 02:13:13 +00:00
|
|
|
return OptionType::IsAlias(this->Type);
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2009-01-28 03:47:20 +00:00
|
|
|
bool OptionDescription::isMultiVal() const {
|
2009-01-28 03:47:58 +00:00
|
|
|
return MultiVal > 1;
|
2009-01-28 03:47:20 +00:00
|
|
|
}
|
|
|
|
|
2009-12-07 18:25:54 +00:00
|
|
|
bool OptionDescription::isCommaSeparated() const {
|
|
|
|
return Flags & OptionDescriptionFlags::CommaSeparated;
|
|
|
|
}
|
|
|
|
void OptionDescription::setCommaSeparated() {
|
|
|
|
Flags |= OptionDescriptionFlags::CommaSeparated;
|
|
|
|
}
|
|
|
|
|
2010-02-23 09:04:13 +00:00
|
|
|
bool OptionDescription::isForwardNotSplit() const {
|
|
|
|
return Flags & OptionDescriptionFlags::ForwardNotSplit;
|
|
|
|
}
|
|
|
|
void OptionDescription::setForwardNotSplit() {
|
|
|
|
Flags |= OptionDescriptionFlags::ForwardNotSplit;
|
|
|
|
}
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
bool OptionDescription::isRequired() const {
|
|
|
|
return Flags & OptionDescriptionFlags::Required;
|
|
|
|
}
|
|
|
|
void OptionDescription::setRequired() {
|
|
|
|
Flags |= OptionDescriptionFlags::Required;
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2009-01-28 03:47:20 +00:00
|
|
|
bool OptionDescription::isOneOrMore() const {
|
|
|
|
return Flags & OptionDescriptionFlags::OneOrMore;
|
|
|
|
}
|
|
|
|
void OptionDescription::setOneOrMore() {
|
|
|
|
Flags |= OptionDescriptionFlags::OneOrMore;
|
|
|
|
}
|
|
|
|
|
2010-03-05 04:46:39 +00:00
|
|
|
bool OptionDescription::isZeroOrMore() const {
|
|
|
|
return Flags & OptionDescriptionFlags::ZeroOrMore;
|
|
|
|
}
|
|
|
|
void OptionDescription::setZeroOrMore() {
|
|
|
|
Flags |= OptionDescriptionFlags::ZeroOrMore;
|
|
|
|
}
|
|
|
|
|
2009-12-15 03:04:52 +00:00
|
|
|
bool OptionDescription::isOptional() const {
|
|
|
|
return Flags & OptionDescriptionFlags::Optional;
|
2009-01-28 03:47:20 +00:00
|
|
|
}
|
2009-12-15 03:04:52 +00:00
|
|
|
void OptionDescription::setOptional() {
|
|
|
|
Flags |= OptionDescriptionFlags::Optional;
|
2009-01-28 03:47:20 +00:00
|
|
|
}
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
bool OptionDescription::isHidden() const {
|
|
|
|
return Flags & OptionDescriptionFlags::Hidden;
|
|
|
|
}
|
|
|
|
void OptionDescription::setHidden() {
|
|
|
|
Flags |= OptionDescriptionFlags::Hidden;
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
bool OptionDescription::isReallyHidden() const {
|
|
|
|
return Flags & OptionDescriptionFlags::ReallyHidden;
|
|
|
|
}
|
|
|
|
void OptionDescription::setReallyHidden() {
|
|
|
|
Flags |= OptionDescriptionFlags::ReallyHidden;
|
|
|
|
}
|
2008-11-28 00:13:25 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
const char* OptionDescription::GenTypeDeclaration() const {
|
|
|
|
switch (Type) {
|
|
|
|
case OptionType::Alias:
|
|
|
|
return "cl::alias";
|
|
|
|
case OptionType::PrefixList:
|
|
|
|
case OptionType::ParameterList:
|
|
|
|
return "cl::list<std::string>";
|
|
|
|
case OptionType::Switch:
|
|
|
|
return "cl::opt<bool>";
|
2010-07-19 03:16:25 +00:00
|
|
|
case OptionType::SwitchList:
|
|
|
|
return "cl::list<bool>";
|
2008-12-07 16:41:11 +00:00
|
|
|
case OptionType::Parameter:
|
|
|
|
case OptionType::Prefix:
|
|
|
|
default:
|
|
|
|
return "cl::opt<std::string>";
|
2008-11-28 00:13:25 +00:00
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
2008-11-28 00:13:25 +00:00
|
|
|
|
2010-08-20 11:24:51 +00:00
|
|
|
std::string OptionDescription::GenOptionType() const {
|
2008-12-07 16:41:11 +00:00
|
|
|
switch (Type) {
|
|
|
|
case OptionType::Alias:
|
2010-08-20 11:24:51 +00:00
|
|
|
return "Alias_";
|
2008-12-07 16:41:11 +00:00
|
|
|
case OptionType::PrefixList:
|
|
|
|
case OptionType::ParameterList:
|
2010-08-20 11:24:51 +00:00
|
|
|
return "List_";
|
2008-12-07 16:41:11 +00:00
|
|
|
case OptionType::Switch:
|
2010-08-20 11:24:51 +00:00
|
|
|
return "Switch_";
|
2010-07-19 03:16:25 +00:00
|
|
|
case OptionType::SwitchList:
|
2010-08-20 11:24:51 +00:00
|
|
|
return "SwitchList_";
|
2008-12-07 16:41:11 +00:00
|
|
|
case OptionType::Prefix:
|
|
|
|
case OptionType::Parameter:
|
|
|
|
default:
|
2010-08-20 11:24:51 +00:00
|
|
|
return "Parameter_";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// OptionDescriptions - An OptionDescription array plus some helper
|
|
|
|
/// functions.
|
|
|
|
class OptionDescriptions {
|
|
|
|
typedef StringMap<OptionDescription> container_type;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// Descriptions - A list of OptionDescriptions.
|
2008-03-23 08:57:20 +00:00
|
|
|
container_type Descriptions;
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
public:
|
2008-05-30 06:28:37 +00:00
|
|
|
/// FindOption - exception-throwing wrapper for find().
|
2008-12-07 16:41:11 +00:00
|
|
|
const OptionDescription& FindOption(const std::string& OptName) const;
|
2009-10-21 02:13:13 +00:00
|
|
|
|
|
|
|
// Wrappers for FindOption that throw an exception in case the option has a
|
|
|
|
// wrong type.
|
2009-10-17 20:09:29 +00:00
|
|
|
const OptionDescription& FindSwitch(const std::string& OptName) const;
|
2009-10-21 02:13:13 +00:00
|
|
|
const OptionDescription& FindParameter(const std::string& OptName) const;
|
2010-07-19 17:17:10 +00:00
|
|
|
const OptionDescription& FindParameterList(const std::string& OptName) const;
|
2009-10-21 02:13:13 +00:00
|
|
|
const OptionDescription&
|
|
|
|
FindListOrParameter(const std::string& OptName) const;
|
2010-07-19 17:17:10 +00:00
|
|
|
const OptionDescription&
|
|
|
|
FindParameterListOrParameter(const std::string& OptName) const;
|
2008-05-06 17:22:03 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// insertDescription - Insert new OptionDescription into
|
|
|
|
/// OptionDescriptions list
|
|
|
|
void InsertDescription (const OptionDescription& o);
|
2008-05-30 06:27:02 +00:00
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
// Support for STL-style iteration
|
2008-12-07 16:41:11 +00:00
|
|
|
typedef container_type::const_iterator const_iterator;
|
2008-03-23 08:57:20 +00:00
|
|
|
const_iterator begin() const { return Descriptions.begin(); }
|
|
|
|
const_iterator end() const { return Descriptions.end(); }
|
|
|
|
};
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
const OptionDescription&
|
2009-10-21 02:13:13 +00:00
|
|
|
OptionDescriptions::FindOption(const std::string& OptName) const {
|
2008-12-07 16:41:11 +00:00
|
|
|
const_iterator I = Descriptions.find(OptName);
|
|
|
|
if (I != Descriptions.end())
|
|
|
|
return I->second;
|
|
|
|
else
|
|
|
|
throw OptName + ": no such option!";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2009-10-17 20:09:29 +00:00
|
|
|
const OptionDescription&
|
2009-10-21 02:13:13 +00:00
|
|
|
OptionDescriptions::FindSwitch(const std::string& OptName) const {
|
2009-10-17 20:09:29 +00:00
|
|
|
const OptionDescription& OptDesc = this->FindOption(OptName);
|
|
|
|
if (!OptDesc.isSwitch())
|
|
|
|
throw OptName + ": incorrect option type - should be a switch!";
|
|
|
|
return OptDesc;
|
|
|
|
}
|
|
|
|
|
2010-07-19 17:17:10 +00:00
|
|
|
const OptionDescription&
|
|
|
|
OptionDescriptions::FindParameterList(const std::string& OptName) const {
|
|
|
|
const OptionDescription& OptDesc = this->FindOption(OptName);
|
|
|
|
if (!OptDesc.isList() || OptDesc.isSwitchList())
|
|
|
|
throw OptName + ": incorrect option type - should be a parameter list!";
|
|
|
|
return OptDesc;
|
|
|
|
}
|
|
|
|
|
2009-10-21 02:13:13 +00:00
|
|
|
const OptionDescription&
|
|
|
|
OptionDescriptions::FindParameter(const std::string& OptName) const {
|
|
|
|
const OptionDescription& OptDesc = this->FindOption(OptName);
|
|
|
|
if (!OptDesc.isParameter())
|
|
|
|
throw OptName + ": incorrect option type - should be a parameter!";
|
|
|
|
return OptDesc;
|
|
|
|
}
|
|
|
|
|
|
|
|
const OptionDescription&
|
|
|
|
OptionDescriptions::FindListOrParameter(const std::string& OptName) const {
|
|
|
|
const OptionDescription& OptDesc = this->FindOption(OptName);
|
|
|
|
if (!OptDesc.isList() && !OptDesc.isParameter())
|
|
|
|
throw OptName
|
|
|
|
+ ": incorrect option type - should be a list or parameter!";
|
|
|
|
return OptDesc;
|
|
|
|
}
|
|
|
|
|
2010-07-19 17:17:10 +00:00
|
|
|
const OptionDescription&
|
|
|
|
OptionDescriptions::FindParameterListOrParameter
|
|
|
|
(const std::string& OptName) const {
|
|
|
|
const OptionDescription& OptDesc = this->FindOption(OptName);
|
|
|
|
if ((!OptDesc.isList() && !OptDesc.isParameter()) || OptDesc.isSwitchList())
|
|
|
|
throw OptName
|
|
|
|
+ ": incorrect option type - should be a parameter list or parameter!";
|
|
|
|
return OptDesc;
|
|
|
|
}
|
|
|
|
|
2009-10-21 02:13:13 +00:00
|
|
|
void OptionDescriptions::InsertDescription (const OptionDescription& o) {
|
2008-12-07 16:41:11 +00:00
|
|
|
container_type::iterator I = Descriptions.find(o.Name);
|
|
|
|
if (I != Descriptions.end()) {
|
|
|
|
OptionDescription& D = I->second;
|
|
|
|
D.Merge(o);
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
else {
|
|
|
|
Descriptions[o.Name] = o;
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// HandlerTable - A base class for function objects implemented as
|
|
|
|
/// 'tables of handlers'.
|
2009-12-07 10:51:55 +00:00
|
|
|
template <typename Handler>
|
2008-12-07 16:41:11 +00:00
|
|
|
class HandlerTable {
|
|
|
|
protected:
|
|
|
|
// Implementation details.
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// HandlerMap - A map from property names to property handlers
|
|
|
|
typedef StringMap<Handler> HandlerMap;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
static HandlerMap Handlers_;
|
|
|
|
static bool staticMembersInitialized_;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
public:
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2009-12-07 10:51:55 +00:00
|
|
|
Handler GetHandler (const std::string& HandlerName) const {
|
|
|
|
typename HandlerMap::iterator method = Handlers_.find(HandlerName);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
if (method != Handlers_.end()) {
|
|
|
|
Handler h = method->second;
|
2009-12-07 10:51:55 +00:00
|
|
|
return h;
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-12-07 10:51:55 +00:00
|
|
|
throw "No handler found for property " + HandlerName + "!";
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2009-12-07 10:51:55 +00:00
|
|
|
void AddHandler(const char* Property, Handler H) {
|
|
|
|
Handlers_[Property] = H;
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
2009-12-07 10:51:55 +00:00
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
};
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
template <class Handler, class FunctionObject>
|
|
|
|
Handler GetHandler(FunctionObject* Obj, const DagInit& Dag) {
|
|
|
|
const std::string& HandlerName = GetOperatorName(Dag);
|
|
|
|
return Obj->GetHandler(HandlerName);
|
|
|
|
}
|
|
|
|
|
2009-12-07 10:51:55 +00:00
|
|
|
template <class FunctionObject>
|
2009-12-17 07:48:49 +00:00
|
|
|
void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
|
|
|
|
typedef void (FunctionObject::*Handler) (const DagInit&);
|
2009-12-07 10:51:55 +00:00
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
const DagInit& Dag = InitPtrToDag(I);
|
|
|
|
Handler h = GetHandler<Handler>(Obj, Dag);
|
2009-12-07 10:51:55 +00:00
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
((Obj)->*(h))(Dag);
|
2009-12-07 10:51:55 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
template <class FunctionObject>
|
|
|
|
void InvokeDagInitHandler(const FunctionObject* const Obj,
|
|
|
|
const Init* I, unsigned IndentLevel, raw_ostream& O)
|
|
|
|
{
|
|
|
|
typedef void (FunctionObject::*Handler)
|
|
|
|
(const DagInit&, unsigned IndentLevel, raw_ostream& O) const;
|
|
|
|
|
|
|
|
const DagInit& Dag = InitPtrToDag(I);
|
|
|
|
Handler h = GetHandler<Handler>(Obj, Dag);
|
|
|
|
|
|
|
|
((Obj)->*(h))(Dag, IndentLevel, O);
|
|
|
|
}
|
|
|
|
|
2009-12-07 10:51:55 +00:00
|
|
|
template <typename H>
|
|
|
|
typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
|
|
|
|
|
|
|
|
template <typename H>
|
|
|
|
bool HandlerTable<H>::staticMembersInitialized_ = false;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// CollectOptionProperties - Function object for iterating over an
|
|
|
|
/// option property list.
|
2009-12-07 10:51:55 +00:00
|
|
|
class CollectOptionProperties;
|
|
|
|
typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
|
2009-12-17 07:48:49 +00:00
|
|
|
(const DagInit&);
|
2009-12-07 10:51:55 +00:00
|
|
|
|
|
|
|
class CollectOptionProperties
|
|
|
|
: public HandlerTable<CollectOptionPropertiesHandler>
|
|
|
|
{
|
2008-05-30 06:27:02 +00:00
|
|
|
private:
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// optDescs_ - OptionDescriptions table. This is where the
|
|
|
|
/// information is stored.
|
|
|
|
OptionDescription& optDesc_;
|
2008-05-30 06:27:02 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
explicit CollectOptionProperties(OptionDescription& OD)
|
2009-12-07 10:51:55 +00:00
|
|
|
: optDesc_(OD)
|
2008-05-30 06:27:02 +00:00
|
|
|
{
|
|
|
|
if (!staticMembersInitialized_) {
|
2008-12-07 16:41:11 +00:00
|
|
|
AddHandler("help", &CollectOptionProperties::onHelp);
|
|
|
|
AddHandler("hidden", &CollectOptionProperties::onHidden);
|
2009-07-07 16:08:41 +00:00
|
|
|
AddHandler("init", &CollectOptionProperties::onInit);
|
2009-01-28 03:47:20 +00:00
|
|
|
AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
|
|
|
|
AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
|
2010-03-05 04:46:39 +00:00
|
|
|
AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
|
2008-12-07 16:41:11 +00:00
|
|
|
AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
|
|
|
|
AddHandler("required", &CollectOptionProperties::onRequired);
|
2009-12-15 03:04:52 +00:00
|
|
|
AddHandler("optional", &CollectOptionProperties::onOptional);
|
2009-12-07 18:25:54 +00:00
|
|
|
AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
|
2010-02-23 09:04:13 +00:00
|
|
|
AddHandler("forward_not_split",
|
|
|
|
&CollectOptionProperties::onForwardNotSplit);
|
2008-05-30 06:27:02 +00:00
|
|
|
|
|
|
|
staticMembersInitialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-07 10:51:55 +00:00
|
|
|
/// operator() - Just forwards to the corresponding property
|
|
|
|
/// handler.
|
2009-12-17 07:48:49 +00:00
|
|
|
void operator() (Init* I) {
|
|
|
|
InvokeDagInitHandler(this, I);
|
2009-12-07 10:51:55 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 06:27:02 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
/// Option property handlers --
|
2008-12-07 16:41:11 +00:00
|
|
|
/// Methods that handle option properties such as (help) or (hidden).
|
2008-09-22 20:46:19 +00:00
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onHelp (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2010-01-26 14:55:04 +00:00
|
|
|
optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
|
2008-05-30 06:27:02 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onHidden (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2008-11-28 00:13:25 +00:00
|
|
|
optDesc_.setHidden();
|
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onReallyHidden (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2008-11-28 00:13:25 +00:00
|
|
|
optDesc_.setReallyHidden();
|
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onCommaSeparated (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2010-07-19 17:17:10 +00:00
|
|
|
if (!optDesc_.isParameterList())
|
|
|
|
throw "'comma_separated' is valid only on parameter list options!";
|
2009-12-07 18:25:54 +00:00
|
|
|
optDesc_.setCommaSeparated();
|
|
|
|
}
|
|
|
|
|
2010-02-23 09:04:13 +00:00
|
|
|
void onForwardNotSplit (const DagInit& d) {
|
|
|
|
CheckNumberOfArguments(d, 0);
|
|
|
|
if (!optDesc_.isParameter())
|
|
|
|
throw "'forward_not_split' is valid only for parameter options!";
|
|
|
|
optDesc_.setForwardNotSplit();
|
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onRequired (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2010-03-05 04:46:39 +00:00
|
|
|
|
2008-05-30 06:27:02 +00:00
|
|
|
optDesc_.setRequired();
|
2010-03-05 04:46:39 +00:00
|
|
|
optDesc_.CheckConsistency();
|
2008-05-30 06:27:02 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onInit (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 07:48:49 +00:00
|
|
|
Init* i = d.getArg(0);
|
2009-07-07 16:08:41 +00:00
|
|
|
const std::string& str = i->getAsString();
|
|
|
|
|
|
|
|
bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
|
|
|
|
correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
|
|
|
|
|
|
|
|
if (!correct)
|
2009-12-07 18:25:54 +00:00
|
|
|
throw "Incorrect usage of the 'init' option property!";
|
2009-07-07 16:08:41 +00:00
|
|
|
|
|
|
|
optDesc_.InitVal = i;
|
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onOneOrMore (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2010-03-05 04:46:39 +00:00
|
|
|
|
2009-01-28 03:47:20 +00:00
|
|
|
optDesc_.setOneOrMore();
|
2010-03-05 04:46:39 +00:00
|
|
|
optDesc_.CheckConsistency();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onZeroOrMore (const DagInit& d) {
|
|
|
|
CheckNumberOfArguments(d, 0);
|
|
|
|
|
2010-07-19 03:16:25 +00:00
|
|
|
if (optDesc_.isList())
|
2010-03-05 04:46:39 +00:00
|
|
|
llvm::errs() << "Warning: specifying the 'zero_or_more' property "
|
|
|
|
"on a list option has no effect.\n";
|
|
|
|
|
|
|
|
optDesc_.setZeroOrMore();
|
|
|
|
optDesc_.CheckConsistency();
|
2009-01-28 03:47:20 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onOptional (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2010-03-05 04:46:39 +00:00
|
|
|
|
2010-07-19 03:16:25 +00:00
|
|
|
if (!optDesc_.isList())
|
2009-12-15 03:04:52 +00:00
|
|
|
llvm::errs() << "Warning: specifying the 'optional' property"
|
2010-03-05 04:46:39 +00:00
|
|
|
"on a non-list option has no effect.\n";
|
|
|
|
|
2009-12-15 03:04:52 +00:00
|
|
|
optDesc_.setOptional();
|
2010-03-05 04:46:39 +00:00
|
|
|
optDesc_.CheckConsistency();
|
2009-01-28 03:47:20 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onMultiVal (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 07:48:49 +00:00
|
|
|
int val = InitPtrToInt(d.getArg(0));
|
2009-01-28 03:47:20 +00:00
|
|
|
if (val < 2)
|
2009-12-07 18:25:54 +00:00
|
|
|
throw "Error in the 'multi_val' property: "
|
|
|
|
"the value must be greater than 1!";
|
2010-07-19 17:17:10 +00:00
|
|
|
if (!optDesc_.isParameterList())
|
2009-12-07 18:25:54 +00:00
|
|
|
throw "The multi_val property is valid only on list options!";
|
2009-01-28 03:47:20 +00:00
|
|
|
optDesc_.MultiVal = val;
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:27:02 +00:00
|
|
|
};
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// AddOption - A function object that is applied to every option
|
|
|
|
/// description. Used by CollectOptionDescriptions.
|
2008-05-30 06:27:29 +00:00
|
|
|
class AddOption {
|
|
|
|
private:
|
2008-12-07 16:41:11 +00:00
|
|
|
OptionDescriptions& OptDescs_;
|
2008-05-30 06:27:29 +00:00
|
|
|
|
|
|
|
public:
|
2008-12-07 16:41:11 +00:00
|
|
|
explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
|
2008-05-30 06:27:29 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
void operator()(const Init* i) {
|
|
|
|
const DagInit& d = InitPtrToDag(i);
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2008-05-30 06:27:29 +00:00
|
|
|
|
|
|
|
const OptionType::OptionType Type =
|
2009-10-18 22:51:30 +00:00
|
|
|
stringToOptionType(GetOperatorName(d));
|
2008-05-30 06:27:29 +00:00
|
|
|
const std::string& Name = InitPtrToString(d.getArg(0));
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
OptionDescription OD(Type, Name);
|
|
|
|
|
2010-08-15 07:07:12 +00:00
|
|
|
CheckNumberOfArguments(d, 2);
|
2008-12-07 16:41:11 +00:00
|
|
|
|
2010-12-15 01:21:59 +00:00
|
|
|
// Alias option store the aliased option name in the 'Help' field and do not
|
|
|
|
// have any properties.
|
2008-12-07 16:41:11 +00:00
|
|
|
if (OD.isAlias()) {
|
2008-05-30 06:27:29 +00:00
|
|
|
OD.Help = InitPtrToString(d.getArg(1));
|
|
|
|
}
|
2010-08-15 07:07:12 +00:00
|
|
|
else {
|
2009-12-17 07:48:49 +00:00
|
|
|
processOptionProperties(d, OD);
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
2010-08-15 07:07:12 +00:00
|
|
|
|
2010-12-15 01:21:59 +00:00
|
|
|
// Switch options are ZeroOrMore by default.
|
|
|
|
if (OD.isSwitch()) {
|
|
|
|
if (!(OD.isOptional() || OD.isOneOrMore() || OD.isRequired()))
|
|
|
|
OD.setZeroOrMore();
|
|
|
|
}
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
OptDescs_.InsertDescription(OD);
|
2008-05-30 06:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2008-12-07 16:41:11 +00:00
|
|
|
/// processOptionProperties - Go through the list of option
|
|
|
|
/// properties and call a corresponding handler for each.
|
2009-12-17 07:48:49 +00:00
|
|
|
static void processOptionProperties (const DagInit& d, OptionDescription& o) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 2);
|
2009-12-17 07:48:49 +00:00
|
|
|
DagInit::const_arg_iterator B = d.arg_begin();
|
2008-12-07 16:41:11 +00:00
|
|
|
// Skip the first argument: it's always the option name.
|
|
|
|
++B;
|
2009-12-17 07:48:49 +00:00
|
|
|
std::for_each(B, d.arg_end(), CollectOptionProperties(o));
|
2008-05-30 06:27:29 +00:00
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
|
2008-05-30 06:27:29 +00:00
|
|
|
};
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// CollectOptionDescriptions - Collects option properties from all
|
|
|
|
/// OptionLists.
|
2010-08-23 23:21:23 +00:00
|
|
|
void CollectOptionDescriptions (const RecordVector& V,
|
2008-12-07 16:41:11 +00:00
|
|
|
OptionDescriptions& OptDescs)
|
|
|
|
{
|
|
|
|
// For every OptionList:
|
2010-10-23 07:32:46 +00:00
|
|
|
for (RecordVector::const_iterator B = V.begin(), E = V.end(); B!=E; ++B)
|
|
|
|
{
|
2008-12-07 16:41:11 +00:00
|
|
|
// Throws an exception if the value does not exist.
|
2010-08-23 23:21:23 +00:00
|
|
|
ListInit* PropList = (*B)->getValueAsListInit("options");
|
2008-05-30 06:27:29 +00:00
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
// For every option description in this list: invoke AddOption.
|
2008-12-07 16:41:11 +00:00
|
|
|
std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
|
|
|
|
}
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
// Tool information record
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
namespace ToolFlags {
|
|
|
|
enum ToolFlags { Join = 0x1, Sink = 0x2 };
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
struct ToolDescription : public RefCountedBase<ToolDescription> {
|
|
|
|
std::string Name;
|
|
|
|
Init* CmdLine;
|
|
|
|
Init* Actions;
|
|
|
|
StrVector InLanguage;
|
2010-02-23 09:04:44 +00:00
|
|
|
std::string InFileOption;
|
|
|
|
std::string OutFileOption;
|
2010-09-21 14:59:42 +00:00
|
|
|
StrVector OutLanguage;
|
2008-12-07 16:41:11 +00:00
|
|
|
std::string OutputSuffix;
|
|
|
|
unsigned Flags;
|
2010-02-23 09:04:28 +00:00
|
|
|
const Init* OnEmpty;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
// Various boolean properties
|
|
|
|
void setSink() { Flags |= ToolFlags::Sink; }
|
|
|
|
bool isSink() const { return Flags & ToolFlags::Sink; }
|
|
|
|
void setJoin() { Flags |= ToolFlags::Join; }
|
|
|
|
bool isJoin() const { return Flags & ToolFlags::Join; }
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
// Default ctor here is needed because StringMap can only store
|
|
|
|
// DefaultConstructible objects
|
2010-08-28 03:43:50 +00:00
|
|
|
ToolDescription (const std::string &n = "")
|
2010-02-23 09:04:44 +00:00
|
|
|
: Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
|
|
|
|
Flags(0), OnEmpty(0)
|
2008-12-07 16:41:11 +00:00
|
|
|
{}
|
|
|
|
};
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// ToolDescriptions - A list of Tool information records.
|
|
|
|
typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
|
|
|
|
/// CollectToolProperties - Function object for iterating over a list of
|
|
|
|
/// tool property records.
|
2009-12-07 10:51:55 +00:00
|
|
|
|
|
|
|
class CollectToolProperties;
|
|
|
|
typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
|
2009-12-17 07:48:49 +00:00
|
|
|
(const DagInit&);
|
2009-12-07 10:51:55 +00:00
|
|
|
|
|
|
|
class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
|
|
|
|
{
|
2008-12-07 16:41:11 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
/// toolDesc_ - Properties of the current Tool. This is where the
|
|
|
|
/// information is stored.
|
|
|
|
ToolDescription& toolDesc_;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
explicit CollectToolProperties (ToolDescription& d)
|
2009-12-07 10:51:55 +00:00
|
|
|
: toolDesc_(d)
|
2008-03-23 08:57:20 +00:00
|
|
|
{
|
|
|
|
if (!staticMembersInitialized_) {
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
AddHandler("actions", &CollectToolProperties::onActions);
|
2010-02-23 09:04:44 +00:00
|
|
|
AddHandler("command", &CollectToolProperties::onCommand);
|
2008-12-07 16:41:11 +00:00
|
|
|
AddHandler("in_language", &CollectToolProperties::onInLanguage);
|
|
|
|
AddHandler("join", &CollectToolProperties::onJoin);
|
|
|
|
AddHandler("out_language", &CollectToolProperties::onOutLanguage);
|
2010-02-23 09:04:44 +00:00
|
|
|
|
|
|
|
AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
|
|
|
|
AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
|
|
|
|
AddHandler("sink", &CollectToolProperties::onSink);
|
2010-02-23 09:04:28 +00:00
|
|
|
AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
staticMembersInitialized_ = true;
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void operator() (Init* I) {
|
|
|
|
InvokeDagInitHandler(this, I);
|
2009-12-07 10:51:55 +00:00
|
|
|
}
|
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
/// Property handlers --
|
|
|
|
/// Functions that extract information about tool properties from
|
|
|
|
/// DAG representation.
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onActions (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 07:48:49 +00:00
|
|
|
Init* Case = d.getArg(0);
|
2008-12-07 16:45:12 +00:00
|
|
|
if (typeid(*Case) != typeid(DagInit) ||
|
2009-12-17 07:48:49 +00:00
|
|
|
GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
|
2009-12-07 19:15:57 +00:00
|
|
|
throw "The argument to (actions) should be a 'case' construct!";
|
2008-12-07 16:45:12 +00:00
|
|
|
toolDesc_.Actions = Case;
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 09:04:44 +00:00
|
|
|
void onCommand (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 07:48:49 +00:00
|
|
|
toolDesc_.CmdLine = d.getArg(0);
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2010-09-21 14:59:42 +00:00
|
|
|
/// onInOutLanguage - Common implementation of on{In,Out}Language().
|
|
|
|
void onInOutLanguage (const DagInit& d, StrVector& OutVec) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2008-05-30 06:18:16 +00:00
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
// Copy strings to the output vector.
|
|
|
|
for (unsigned i = 0, NumArgs = d.getNumArgs(); i < NumArgs; ++i) {
|
|
|
|
OutVec.push_back(InitPtrToString(d.getArg(i)));
|
2008-05-30 06:18:16 +00:00
|
|
|
}
|
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
// Remove duplicates.
|
|
|
|
std::sort(OutVec.begin(), OutVec.end());
|
|
|
|
StrVector::iterator newE = std::unique(OutVec.begin(), OutVec.end());
|
|
|
|
OutVec.erase(newE, OutVec.end());
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2010-09-21 14:59:42 +00:00
|
|
|
|
|
|
|
void onInLanguage (const DagInit& d) {
|
|
|
|
this->onInOutLanguage(d, toolDesc_.InLanguage);
|
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onJoin (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2008-12-07 16:41:11 +00:00
|
|
|
toolDesc_.setJoin();
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onOutLanguage (const DagInit& d) {
|
2010-09-21 14:59:42 +00:00
|
|
|
this->onInOutLanguage(d, toolDesc_.OutLanguage);
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 09:04:44 +00:00
|
|
|
void onOutFileOption (const DagInit& d) {
|
|
|
|
CheckNumberOfArguments(d, 1);
|
|
|
|
toolDesc_.OutFileOption = InitPtrToString(d.getArg(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void onInFileOption (const DagInit& d) {
|
|
|
|
CheckNumberOfArguments(d, 1);
|
|
|
|
toolDesc_.InFileOption = InitPtrToString(d.getArg(0));
|
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onOutputSuffix (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 07:48:49 +00:00
|
|
|
toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void onSink (const DagInit& d) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2008-12-07 16:41:11 +00:00
|
|
|
toolDesc_.setSink();
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 09:04:28 +00:00
|
|
|
void onWorksOnEmpty (const DagInit& d) {
|
|
|
|
toolDesc_.OnEmpty = d.getArg(0);
|
|
|
|
}
|
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
};
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// CollectToolDescriptions - Gather information about tool properties
|
2008-05-30 06:26:08 +00:00
|
|
|
/// from the parsed TableGen data (basically a wrapper for the
|
2008-12-07 16:41:11 +00:00
|
|
|
/// CollectToolProperties function object).
|
2010-08-23 23:21:23 +00:00
|
|
|
void CollectToolDescriptions (const RecordVector& Tools,
|
2008-12-07 16:41:11 +00:00
|
|
|
ToolDescriptions& ToolDescs)
|
2008-03-23 08:57:20 +00:00
|
|
|
{
|
|
|
|
// Iterate over a properties list of every Tool definition
|
2010-08-23 23:21:23 +00:00
|
|
|
for (RecordVector::const_iterator B = Tools.begin(),
|
|
|
|
E = Tools.end(); B!=E; ++B) {
|
2008-11-17 17:29:42 +00:00
|
|
|
const Record* T = *B;
|
2008-05-30 06:26:08 +00:00
|
|
|
// Throws an exception if the value does not exist.
|
2008-03-23 08:57:20 +00:00
|
|
|
ListInit* PropList = T->getValueAsListInit("properties");
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
IntrusiveRefCntPtr<ToolDescription>
|
|
|
|
ToolDesc(new ToolDescription(T->getName()));
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
std::for_each(PropList->begin(), PropList->end(),
|
2008-12-07 16:41:11 +00:00
|
|
|
CollectToolProperties(*ToolDesc));
|
|
|
|
ToolDescs.push_back(ToolDesc);
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// FillInEdgeVector - Merge all compilation graph definitions into
|
|
|
|
/// one single edge list.
|
2010-08-23 23:21:23 +00:00
|
|
|
void FillInEdgeVector(const RecordVector& CompilationGraphs,
|
|
|
|
DagVector& Out) {
|
|
|
|
for (RecordVector::const_iterator B = CompilationGraphs.begin(),
|
|
|
|
E = CompilationGraphs.end(); B != E; ++B) {
|
|
|
|
const ListInit* Edges = (*B)->getValueAsListInit("edges");
|
2008-05-30 06:27:02 +00:00
|
|
|
|
2010-08-23 23:21:23 +00:00
|
|
|
for (ListInit::const_iterator B = Edges->begin(),
|
|
|
|
E = Edges->end(); B != E; ++B) {
|
|
|
|
Out.push_back(&InitPtrToDag(*B));
|
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// NotInGraph - Helper function object for FilterNotInGraph.
|
|
|
|
struct NotInGraph {
|
|
|
|
private:
|
|
|
|
const llvm::StringSet<>& ToolsInGraph_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NotInGraph(const llvm::StringSet<>& ToolsInGraph)
|
|
|
|
: ToolsInGraph_(ToolsInGraph)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
|
|
|
|
return (ToolsInGraph_.count(x->Name) == 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// FilterNotInGraph - Filter out from ToolDescs all Tools not
|
|
|
|
/// mentioned in the compilation graph definition.
|
2010-08-23 23:21:23 +00:00
|
|
|
void FilterNotInGraph (const DagVector& EdgeVector,
|
2008-12-07 16:41:11 +00:00
|
|
|
ToolDescriptions& ToolDescs) {
|
|
|
|
|
|
|
|
// List all tools mentioned in the graph.
|
|
|
|
llvm::StringSet<> ToolsInGraph;
|
|
|
|
|
2010-08-23 23:21:23 +00:00
|
|
|
for (DagVector::const_iterator B = EdgeVector.begin(),
|
2008-12-07 16:41:11 +00:00
|
|
|
E = EdgeVector.end(); B != E; ++B) {
|
|
|
|
|
2010-08-23 23:21:23 +00:00
|
|
|
const DagInit* Edge = *B;
|
|
|
|
const std::string& NodeA = InitPtrToString(Edge->getArg(0));
|
|
|
|
const std::string& NodeB = InitPtrToString(Edge->getArg(1));
|
2008-12-07 16:41:11 +00:00
|
|
|
|
2008-12-07 16:44:47 +00:00
|
|
|
if (NodeA != "root")
|
|
|
|
ToolsInGraph.insert(NodeA);
|
|
|
|
ToolsInGraph.insert(NodeB);
|
2008-05-30 06:26:08 +00:00
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
|
|
|
|
// Filter ToolPropertiesList.
|
|
|
|
ToolDescriptions::iterator new_end =
|
|
|
|
std::remove_if(ToolDescs.begin(), ToolDescs.end(),
|
|
|
|
NotInGraph(ToolsInGraph));
|
|
|
|
ToolDescs.erase(new_end, ToolDescs.end());
|
2008-05-30 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// FillInToolToLang - Fills in two tables that map tool names to
|
2010-09-21 14:59:42 +00:00
|
|
|
/// input & output language names. Helper function used by TypecheckGraph().
|
2008-12-07 16:41:11 +00:00
|
|
|
void FillInToolToLang (const ToolDescriptions& ToolDescs,
|
|
|
|
StringMap<StringSet<> >& ToolToInLang,
|
2010-09-21 14:59:42 +00:00
|
|
|
StringMap<StringSet<> >& ToolToOutLang) {
|
2008-12-07 16:41:11 +00:00
|
|
|
for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
|
|
|
|
E = ToolDescs.end(); B != E; ++B) {
|
|
|
|
const ToolDescription& D = *(*B);
|
|
|
|
for (StrVector::const_iterator B = D.InLanguage.begin(),
|
|
|
|
E = D.InLanguage.end(); B != E; ++B)
|
|
|
|
ToolToInLang[D.Name].insert(*B);
|
2010-09-21 14:59:42 +00:00
|
|
|
for (StrVector::const_iterator B = D.OutLanguage.begin(),
|
|
|
|
E = D.OutLanguage.end(); B != E; ++B)
|
|
|
|
ToolToOutLang[D.Name].insert(*B);
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-21 14:59:42 +00:00
|
|
|
/// Intersect - Is set intersection non-empty?
|
|
|
|
bool Intersect (const StringSet<>& S1, const StringSet<>& S2) {
|
|
|
|
for (StringSet<>::const_iterator B = S1.begin(), E = S1.end(); B != E; ++B) {
|
|
|
|
if (S2.count(B->first()) != 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// TypecheckGraph - Check that names for output and input languages
|
2010-08-23 19:24:00 +00:00
|
|
|
/// on all edges do match.
|
2010-08-23 23:21:23 +00:00
|
|
|
void TypecheckGraph (const DagVector& EdgeVector,
|
2008-12-07 16:41:11 +00:00
|
|
|
const ToolDescriptions& ToolDescs) {
|
|
|
|
StringMap<StringSet<> > ToolToInLang;
|
2010-09-21 14:59:42 +00:00
|
|
|
StringMap<StringSet<> > ToolToOutLang;
|
2008-12-07 16:41:11 +00:00
|
|
|
|
|
|
|
FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
|
|
|
|
|
2010-08-23 23:21:23 +00:00
|
|
|
for (DagVector::const_iterator B = EdgeVector.begin(),
|
2008-12-07 16:41:11 +00:00
|
|
|
E = EdgeVector.end(); B != E; ++B) {
|
2010-08-23 23:21:23 +00:00
|
|
|
const DagInit* Edge = *B;
|
|
|
|
const std::string& NodeA = InitPtrToString(Edge->getArg(0));
|
|
|
|
const std::string& NodeB = InitPtrToString(Edge->getArg(1));
|
2010-09-21 14:59:42 +00:00
|
|
|
StringMap<StringSet<> >::iterator IA = ToolToOutLang.find(NodeA);
|
2008-12-07 16:44:47 +00:00
|
|
|
StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
|
2008-12-07 16:41:11 +00:00
|
|
|
|
2010-09-21 14:59:42 +00:00
|
|
|
if (NodeB == "root")
|
|
|
|
throw "Edges back to the root are not allowed!";
|
|
|
|
|
2008-12-07 16:44:47 +00:00
|
|
|
if (NodeA != "root") {
|
2010-09-21 14:59:42 +00:00
|
|
|
if (IA == ToolToOutLang.end())
|
|
|
|
throw NodeA + ": no output language defined!";
|
|
|
|
if (IB == ToolToInLang.end())
|
|
|
|
throw NodeB + ": no input language defined!";
|
|
|
|
|
|
|
|
if (!Intersect(IA->second, IB->second)) {
|
2008-12-07 16:44:47 +00:00
|
|
|
throw "Edge " + NodeA + "->" + NodeB
|
2008-12-07 16:41:11 +00:00
|
|
|
+ ": output->input language mismatch";
|
2010-09-21 14:59:42 +00:00
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// WalkCase - Walks the 'case' expression DAG and invokes
|
|
|
|
/// TestCallback on every test, and StatementCallback on every
|
|
|
|
/// statement. Handles 'case' nesting, but not the 'and' and 'or'
|
2009-10-18 22:51:30 +00:00
|
|
|
/// combinators (that is, they are passed directly to TestCallback).
|
|
|
|
/// TestCallback must have type 'void TestCallback(const DagInit*, unsigned
|
|
|
|
/// IndentLevel, bool FirstTest)'.
|
|
|
|
/// StatementCallback must have type 'void StatementCallback(const Init*,
|
|
|
|
/// unsigned IndentLevel)'.
|
2008-12-07 16:41:11 +00:00
|
|
|
template <typename F1, typename F2>
|
2009-10-18 22:51:30 +00:00
|
|
|
void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
|
|
|
|
unsigned IndentLevel = 0)
|
|
|
|
{
|
2008-12-07 16:41:11 +00:00
|
|
|
const DagInit& d = InitPtrToDag(Case);
|
2009-10-18 22:51:30 +00:00
|
|
|
|
|
|
|
// Error checks.
|
|
|
|
if (GetOperatorName(d) != "case")
|
2009-12-07 19:15:57 +00:00
|
|
|
throw "WalkCase should be invoked only on 'case' expressions!";
|
2009-10-18 22:51:30 +00:00
|
|
|
|
|
|
|
if (d.getNumArgs() < 2)
|
|
|
|
throw "There should be at least one clause in the 'case' expression:\n"
|
|
|
|
+ d.getAsString();
|
|
|
|
|
|
|
|
// Main loop.
|
2008-12-07 16:41:11 +00:00
|
|
|
bool even = false;
|
2009-10-18 22:51:30 +00:00
|
|
|
const unsigned numArgs = d.getNumArgs();
|
|
|
|
unsigned i = 1;
|
2008-12-07 16:41:11 +00:00
|
|
|
for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
|
|
|
|
B != E; ++B) {
|
|
|
|
Init* arg = *B;
|
2009-10-18 22:51:30 +00:00
|
|
|
|
|
|
|
if (!even)
|
|
|
|
{
|
|
|
|
// Handle test.
|
|
|
|
const DagInit& Test = InitPtrToDag(arg);
|
|
|
|
|
|
|
|
if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
|
2009-12-07 19:15:57 +00:00
|
|
|
throw "The 'default' clause should be the last in the "
|
|
|
|
"'case' construct!";
|
2009-10-18 22:51:30 +00:00
|
|
|
if (i == numArgs)
|
|
|
|
throw "Case construct handler: no corresponding action "
|
|
|
|
"found for the test " + Test.getAsString() + '!';
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
TestCallback(Test, IndentLevel, (i == 1));
|
2009-10-18 22:51:30 +00:00
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
else
|
2009-10-18 22:51:30 +00:00
|
|
|
{
|
|
|
|
if (dynamic_cast<DagInit*>(arg)
|
2009-12-17 07:48:49 +00:00
|
|
|
&& GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
|
2009-10-18 22:51:30 +00:00
|
|
|
// Nested 'case'.
|
|
|
|
WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle statement.
|
|
|
|
StatementCallback(arg, IndentLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
2008-12-07 16:41:11 +00:00
|
|
|
even = !even;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ExtractOptionNames - A helper function object used by
|
|
|
|
/// CheckForSuperfluousOptions() to walk the 'case' DAG.
|
|
|
|
class ExtractOptionNames {
|
|
|
|
llvm::StringSet<>& OptionNames_;
|
|
|
|
|
2008-12-07 16:42:22 +00:00
|
|
|
void processDag(const Init* Statement) {
|
2008-12-07 16:41:11 +00:00
|
|
|
const DagInit& Stmt = InitPtrToDag(Statement);
|
2009-10-18 22:51:30 +00:00
|
|
|
const std::string& ActionName = GetOperatorName(Stmt);
|
2008-12-07 16:41:11 +00:00
|
|
|
if (ActionName == "forward" || ActionName == "forward_as" ||
|
2009-12-07 17:03:05 +00:00
|
|
|
ActionName == "forward_value" ||
|
|
|
|
ActionName == "forward_transformed_value" ||
|
2010-10-23 07:32:46 +00:00
|
|
|
ActionName == "parameter_equals" || ActionName == "element_in_list") {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(Stmt, 1);
|
2010-01-01 03:51:02 +00:00
|
|
|
|
|
|
|
Init* Arg = Stmt.getArg(0);
|
2010-10-23 07:32:46 +00:00
|
|
|
if (typeid(*Arg) == typeid(StringInit))
|
|
|
|
OptionNames_.insert(InitPtrToString(Arg));
|
|
|
|
}
|
|
|
|
else if (ActionName == "any_switch_on" || ActionName == "switch_on" ||
|
|
|
|
ActionName == "any_not_empty" || ActionName == "any_empty" ||
|
|
|
|
ActionName == "not_empty" || ActionName == "empty") {
|
|
|
|
for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
|
|
|
|
Init* Arg = Stmt.getArg(i);
|
|
|
|
if (typeid(*Arg) == typeid(StringInit))
|
|
|
|
OptionNames_.insert(InitPtrToString(Arg));
|
2010-01-01 03:51:02 +00:00
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
2010-01-01 03:51:02 +00:00
|
|
|
else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
|
2008-12-07 16:41:11 +00:00
|
|
|
for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
|
2008-12-07 16:42:22 +00:00
|
|
|
this->processDag(Stmt.getArg(i));
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-07 16:42:22 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void operator()(const Init* Statement) {
|
2010-10-23 07:32:46 +00:00
|
|
|
// Statement is either a dag, or a list of dags.
|
2008-12-07 16:42:22 +00:00
|
|
|
if (typeid(*Statement) == typeid(ListInit)) {
|
|
|
|
const ListInit& DagList = *static_cast<const ListInit*>(Statement);
|
|
|
|
for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
|
|
|
|
B != E; ++B)
|
|
|
|
this->processDag(*B);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->processDag(Statement);
|
|
|
|
}
|
|
|
|
}
|
2009-10-18 22:51:30 +00:00
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void operator()(const DagInit& Test, unsigned, bool) {
|
|
|
|
this->operator()(&Test);
|
2009-10-18 22:51:30 +00:00
|
|
|
}
|
|
|
|
void operator()(const Init* Statement, unsigned) {
|
|
|
|
this->operator()(Statement);
|
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
};
|
|
|
|
|
2010-08-24 01:10:22 +00:00
|
|
|
/// IsOptionalEdge - Validate that the 'optional_edge' has proper structure.
|
|
|
|
bool IsOptionalEdge (const DagInit& Edg) {
|
|
|
|
return (GetOperatorName(Edg) == "optional_edge") && (Edg.getNumArgs() > 2);
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:28:37 +00:00
|
|
|
/// CheckForSuperfluousOptions - Check that there are no side
|
|
|
|
/// effect-free options (specified only in the OptionList). Otherwise,
|
|
|
|
/// output a warning.
|
2010-08-23 23:21:23 +00:00
|
|
|
void CheckForSuperfluousOptions (const DagVector& EdgeVector,
|
2008-12-07 16:41:11 +00:00
|
|
|
const ToolDescriptions& ToolDescs,
|
|
|
|
const OptionDescriptions& OptDescs) {
|
2008-05-30 06:28:37 +00:00
|
|
|
llvm::StringSet<> nonSuperfluousOptions;
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
// Add all options mentioned in the ToolDesc.Actions to the set of
|
2008-05-30 06:28:37 +00:00
|
|
|
// non-superfluous options.
|
2008-12-07 16:41:11 +00:00
|
|
|
for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
|
|
|
|
E = ToolDescs.end(); B != E; ++B) {
|
|
|
|
const ToolDescription& TD = *(*B);
|
|
|
|
ExtractOptionNames Callback(nonSuperfluousOptions);
|
|
|
|
if (TD.Actions)
|
|
|
|
WalkCase(TD.Actions, Callback, Callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all options mentioned in the 'case' clauses of the
|
|
|
|
// OptionalEdges of the compilation graph to the set of
|
|
|
|
// non-superfluous options.
|
2010-08-23 23:21:23 +00:00
|
|
|
for (DagVector::const_iterator B = EdgeVector.begin(),
|
|
|
|
E = EdgeVector.end(); B != E; ++B) {
|
2010-08-24 01:10:22 +00:00
|
|
|
const DagInit& Edge = **B;
|
|
|
|
if (IsOptionalEdge(Edge)) {
|
|
|
|
const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
|
2009-12-17 07:48:49 +00:00
|
|
|
WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
|
2010-08-23 23:21:23 +00:00
|
|
|
}
|
2008-05-30 06:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all options in OptDescs belong to the set of
|
|
|
|
// non-superfluous options.
|
2008-12-07 16:41:11 +00:00
|
|
|
for (OptionDescriptions::const_iterator B = OptDescs.begin(),
|
2008-05-30 06:28:37 +00:00
|
|
|
E = OptDescs.end(); B != E; ++B) {
|
2008-12-07 16:41:11 +00:00
|
|
|
const OptionDescription& Val = B->second;
|
2008-05-30 06:28:37 +00:00
|
|
|
if (!nonSuperfluousOptions.count(Val.Name)
|
|
|
|
&& Val.Type != OptionType::Alias)
|
2009-07-03 00:10:29 +00:00
|
|
|
llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
|
2008-05-30 06:28:37 +00:00
|
|
|
"Probable cause: this option is specified only in the OptionList.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-28 01:16:42 +00:00
|
|
|
/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
|
|
|
|
bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
|
|
|
|
if (TestName == "single_input_file") {
|
|
|
|
O << "InputFilenames.size() == 1";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "multiple_input_files") {
|
|
|
|
O << "InputFilenames.size() > 1";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
/// EmitMultipleArgumentTest - Helper function used by
|
|
|
|
/// EmitCaseTestMultipleArgs()
|
2009-10-21 02:13:13 +00:00
|
|
|
template <typename F>
|
2010-10-23 07:32:46 +00:00
|
|
|
void EmitMultipleArgumentTest(const DagInit& D, const char* LogicOp,
|
|
|
|
F Callback, raw_ostream& O)
|
2009-10-21 02:13:13 +00:00
|
|
|
{
|
2010-10-23 07:32:46 +00:00
|
|
|
for (unsigned i = 0, NumArgs = D.getNumArgs(); i < NumArgs; ++i) {
|
|
|
|
if (i != 0)
|
|
|
|
O << ' ' << LogicOp << ' ';
|
|
|
|
Callback(InitPtrToString(D.getArg(i)), O);
|
2009-10-21 02:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-17 02:47:01 +00:00
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
// Callbacks for use with EmitMultipleArgumentTest
|
2009-10-21 02:13:13 +00:00
|
|
|
|
|
|
|
class EmitSwitchOn {
|
|
|
|
const OptionDescriptions& OptDescs_;
|
|
|
|
public:
|
|
|
|
EmitSwitchOn(const OptionDescriptions& OptDescs) : OptDescs_(OptDescs)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void operator()(const std::string& OptName, raw_ostream& O) const {
|
|
|
|
const OptionDescription& OptDesc = OptDescs_.FindSwitch(OptName);
|
2008-05-30 06:10:19 +00:00
|
|
|
O << OptDesc.GenVariableName();
|
2009-10-17 20:09:29 +00:00
|
|
|
}
|
2009-10-21 02:13:13 +00:00
|
|
|
};
|
2008-09-22 20:48:22 +00:00
|
|
|
|
2009-10-21 02:13:13 +00:00
|
|
|
class EmitEmptyTest {
|
|
|
|
bool EmitNegate_;
|
|
|
|
const OptionDescriptions& OptDescs_;
|
|
|
|
public:
|
|
|
|
EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
|
|
|
|
: EmitNegate_(EmitNegate), OptDescs_(OptDescs)
|
|
|
|
{}
|
2008-12-17 02:47:01 +00:00
|
|
|
|
2009-10-21 02:13:13 +00:00
|
|
|
void operator()(const std::string& OptName, raw_ostream& O) const {
|
|
|
|
const char* Neg = (EmitNegate_ ? "!" : "");
|
2008-05-30 06:24:07 +00:00
|
|
|
if (OptName == "o") {
|
2009-10-21 02:13:13 +00:00
|
|
|
O << Neg << "OutputFilename.empty()";
|
2008-05-30 06:24:07 +00:00
|
|
|
}
|
2009-12-01 06:51:30 +00:00
|
|
|
else if (OptName == "save-temps") {
|
|
|
|
O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
|
|
|
|
}
|
2008-05-30 06:24:07 +00:00
|
|
|
else {
|
2009-10-21 02:13:13 +00:00
|
|
|
const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
|
|
|
|
O << Neg << OptDesc.GenVariableName() << ".empty()";
|
2008-05-30 06:24:07 +00:00
|
|
|
}
|
2008-05-30 06:10:19 +00:00
|
|
|
}
|
2009-10-21 02:13:13 +00:00
|
|
|
};
|
2008-05-30 06:10:19 +00:00
|
|
|
|
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
/// EmitCaseTestMultipleArgs - Helper function used by EmitCaseTest1Arg()
|
|
|
|
bool EmitCaseTestMultipleArgs (const std::string& TestName,
|
|
|
|
const DagInit& d,
|
|
|
|
const OptionDescriptions& OptDescs,
|
|
|
|
raw_ostream& O) {
|
2009-10-17 20:09:29 +00:00
|
|
|
if (TestName == "any_switch_on") {
|
2010-10-23 07:32:46 +00:00
|
|
|
EmitMultipleArgumentTest(d, "||", EmitSwitchOn(OptDescs), O);
|
2009-10-21 02:13:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "switch_on") {
|
2010-10-23 07:32:46 +00:00
|
|
|
EmitMultipleArgumentTest(d, "&&", EmitSwitchOn(OptDescs), O);
|
2009-10-21 02:13:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "any_not_empty") {
|
2010-10-23 07:32:46 +00:00
|
|
|
EmitMultipleArgumentTest(d, "||", EmitEmptyTest(true, OptDescs), O);
|
2009-10-21 02:13:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "any_empty") {
|
2010-10-23 07:32:46 +00:00
|
|
|
EmitMultipleArgumentTest(d, "||", EmitEmptyTest(false, OptDescs), O);
|
2009-10-21 02:13:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "not_empty") {
|
2010-10-23 07:32:46 +00:00
|
|
|
EmitMultipleArgumentTest(d, "&&", EmitEmptyTest(true, OptDescs), O);
|
2009-10-21 02:13:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "empty") {
|
2010-10-23 07:32:46 +00:00
|
|
|
EmitMultipleArgumentTest(d, "&&", EmitEmptyTest(false, OptDescs), O);
|
2009-10-21 02:13:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-10-17 20:09:29 +00:00
|
|
|
|
2009-10-21 02:13:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-10-17 20:09:29 +00:00
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
/// EmitCaseTest1Arg - Helper function used by EmitCaseTest1OrMoreArgs()
|
|
|
|
bool EmitCaseTest1Arg (const std::string& TestName,
|
|
|
|
const DagInit& d,
|
|
|
|
const OptionDescriptions& OptDescs,
|
|
|
|
raw_ostream& O) {
|
|
|
|
const std::string& Arg = InitPtrToString(d.getArg(0));
|
2009-10-17 20:09:29 +00:00
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
if (TestName == "input_languages_contain") {
|
|
|
|
O << "InLangs.count(\"" << Arg << "\") != 0";
|
2009-10-21 02:13:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "in_language") {
|
|
|
|
// This works only for single-argument Tool::GenerateAction. Join
|
|
|
|
// tools can process several files in different languages simultaneously.
|
2009-10-17 20:09:29 +00:00
|
|
|
|
2009-10-21 02:13:13 +00:00
|
|
|
// TODO: make this work with Edge::Weight (if possible).
|
2010-10-23 07:32:46 +00:00
|
|
|
O << "LangMap.GetLanguage(inFile) == \"" << Arg << '\"';
|
2009-10-21 02:13:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-10-17 20:09:29 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
/// EmitCaseTest1OrMoreArgs - Helper function used by
|
|
|
|
/// EmitCaseConstructHandler()
|
|
|
|
bool EmitCaseTest1OrMoreArgs(const std::string& TestName,
|
|
|
|
const DagInit& d,
|
|
|
|
const OptionDescriptions& OptDescs,
|
|
|
|
raw_ostream& O) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2010-10-23 07:32:46 +00:00
|
|
|
return EmitCaseTest1Arg(TestName, d, OptDescs, O) ||
|
|
|
|
EmitCaseTestMultipleArgs(TestName, d, OptDescs, O);
|
2009-10-17 20:09:29 +00:00
|
|
|
}
|
|
|
|
|
2009-09-28 01:16:42 +00:00
|
|
|
/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
|
2008-05-30 06:10:19 +00:00
|
|
|
bool EmitCaseTest2Args(const std::string& TestName,
|
|
|
|
const DagInit& d,
|
2009-09-21 15:53:44 +00:00
|
|
|
unsigned IndentLevel,
|
2008-12-07 16:41:11 +00:00
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 00:10:29 +00:00
|
|
|
raw_ostream& O) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 2);
|
2008-05-30 06:10:19 +00:00
|
|
|
const std::string& OptName = InitPtrToString(d.getArg(0));
|
|
|
|
const std::string& OptArg = InitPtrToString(d.getArg(1));
|
|
|
|
|
|
|
|
if (TestName == "parameter_equals") {
|
2009-10-21 02:13:13 +00:00
|
|
|
const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
|
2008-05-30 06:10:19 +00:00
|
|
|
O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "element_in_list") {
|
2010-07-19 17:17:10 +00:00
|
|
|
const OptionDescription& OptDesc = OptDescs.FindParameterList(OptName);
|
2008-05-30 06:10:19 +00:00
|
|
|
const std::string& VarName = OptDesc.GenVariableName();
|
2009-09-21 15:53:44 +00:00
|
|
|
O << "std::find(" << VarName << ".begin(),\n";
|
|
|
|
O.indent(IndentLevel + Indent1)
|
|
|
|
<< VarName << ".end(), \""
|
2008-05-30 06:10:19 +00:00
|
|
|
<< OptArg << "\") != " << VarName << ".end()";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward declaration.
|
|
|
|
// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
|
2009-09-21 15:53:44 +00:00
|
|
|
void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
|
2008-12-07 16:41:11 +00:00
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 00:10:29 +00:00
|
|
|
raw_ostream& O);
|
2008-05-30 06:10:19 +00:00
|
|
|
|
|
|
|
/// EmitLogicalOperationTest - Helper function used by
|
|
|
|
/// EmitCaseConstructHandler.
|
|
|
|
void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
|
2009-09-21 15:53:44 +00:00
|
|
|
unsigned IndentLevel,
|
2008-12-07 16:41:11 +00:00
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 00:10:29 +00:00
|
|
|
raw_ostream& O) {
|
2008-05-30 06:10:19 +00:00
|
|
|
O << '(';
|
2010-10-23 07:32:46 +00:00
|
|
|
for (unsigned i = 0, NumArgs = d.getNumArgs(); i < NumArgs; ++i) {
|
|
|
|
const DagInit& InnerTest = InitPtrToDag(d.getArg(i));
|
2008-05-30 06:10:19 +00:00
|
|
|
EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
|
2010-10-23 07:32:46 +00:00
|
|
|
if (i != NumArgs - 1) {
|
2009-09-21 15:53:44 +00:00
|
|
|
O << ")\n";
|
|
|
|
O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
|
|
|
|
}
|
|
|
|
else {
|
2008-05-30 06:10:19 +00:00
|
|
|
O << ')';
|
2009-09-21 15:53:44 +00:00
|
|
|
}
|
2008-05-30 06:10:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-21 15:53:44 +00:00
|
|
|
void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
|
2009-09-10 16:21:38 +00:00
|
|
|
const OptionDescriptions& OptDescs, raw_ostream& O)
|
|
|
|
{
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-09-10 16:21:38 +00:00
|
|
|
const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
|
|
|
|
O << "! (";
|
|
|
|
EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
|
|
|
|
O << ")";
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:10:19 +00:00
|
|
|
/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
|
2009-09-21 15:53:44 +00:00
|
|
|
void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
|
2008-12-07 16:41:11 +00:00
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 00:10:29 +00:00
|
|
|
raw_ostream& O) {
|
2009-10-18 22:51:30 +00:00
|
|
|
const std::string& TestName = GetOperatorName(d);
|
2008-05-30 06:10:19 +00:00
|
|
|
|
|
|
|
if (TestName == "and")
|
|
|
|
EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
|
|
|
|
else if (TestName == "or")
|
|
|
|
EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
|
2009-09-10 16:21:38 +00:00
|
|
|
else if (TestName == "not")
|
|
|
|
EmitLogicalNot(d, IndentLevel, OptDescs, O);
|
2009-09-28 01:16:42 +00:00
|
|
|
else if (EmitCaseTest0Args(TestName, O))
|
|
|
|
return;
|
2010-10-23 07:32:46 +00:00
|
|
|
else if (EmitCaseTest1OrMoreArgs(TestName, d, OptDescs, O))
|
2008-05-30 06:10:19 +00:00
|
|
|
return;
|
|
|
|
else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
|
|
|
|
return;
|
|
|
|
else
|
2010-01-01 03:50:34 +00:00
|
|
|
throw "Unknown test '" + TestName + "' used in the 'case' construct!";
|
2008-05-30 06:10:19 +00:00
|
|
|
}
|
|
|
|
|
2009-10-18 22:51:30 +00:00
|
|
|
|
|
|
|
/// EmitCaseTestCallback - Callback used by EmitCaseConstructHandler.
|
|
|
|
class EmitCaseTestCallback {
|
|
|
|
bool EmitElseIf_;
|
|
|
|
const OptionDescriptions& OptDescs_;
|
|
|
|
raw_ostream& O_;
|
|
|
|
public:
|
|
|
|
|
|
|
|
EmitCaseTestCallback(bool EmitElseIf,
|
|
|
|
const OptionDescriptions& OptDescs, raw_ostream& O)
|
|
|
|
: EmitElseIf_(EmitElseIf), OptDescs_(OptDescs), O_(O)
|
|
|
|
{}
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
|
2009-10-18 22:51:30 +00:00
|
|
|
{
|
|
|
|
if (GetOperatorName(Test) == "default") {
|
|
|
|
O_.indent(IndentLevel) << "else {\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
O_.indent(IndentLevel)
|
|
|
|
<< ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
|
2009-12-17 07:48:49 +00:00
|
|
|
EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
|
2009-10-18 22:51:30 +00:00
|
|
|
O_ << ") {\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// EmitCaseStatementCallback - Callback used by EmitCaseConstructHandler.
|
|
|
|
template <typename F>
|
|
|
|
class EmitCaseStatementCallback {
|
|
|
|
F Callback_;
|
|
|
|
raw_ostream& O_;
|
|
|
|
public:
|
|
|
|
|
|
|
|
EmitCaseStatementCallback(F Callback, raw_ostream& O)
|
|
|
|
: Callback_(Callback), O_(O)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void operator() (const Init* Statement, unsigned IndentLevel) {
|
2010-10-23 07:32:46 +00:00
|
|
|
// Is this a nested 'case'?
|
|
|
|
bool IsCase = dynamic_cast<const DagInit*>(Statement) &&
|
|
|
|
GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case";
|
2009-10-19 21:24:28 +00:00
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
// If so, ignore it, it is handled by our caller, WalkCase.
|
|
|
|
if (!IsCase) {
|
2009-10-19 21:24:28 +00:00
|
|
|
if (typeid(*Statement) == typeid(ListInit)) {
|
|
|
|
const ListInit& DagList = *static_cast<const ListInit*>(Statement);
|
|
|
|
for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
|
|
|
|
B != E; ++B)
|
|
|
|
Callback_(*B, (IndentLevel + Indent1), O_);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Callback_(Statement, (IndentLevel + Indent1), O_);
|
|
|
|
}
|
|
|
|
}
|
2009-10-18 22:51:30 +00:00
|
|
|
O_.indent(IndentLevel) << "}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-10-17 20:09:29 +00:00
|
|
|
/// EmitCaseConstructHandler - Emit code that handles the 'case'
|
|
|
|
/// construct. Takes a function object that should emit code for every case
|
2009-10-18 22:51:30 +00:00
|
|
|
/// clause. Implemented on top of WalkCase.
|
2010-02-23 09:04:28 +00:00
|
|
|
/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
|
2009-10-17 20:09:29 +00:00
|
|
|
/// raw_ostream& O).
|
|
|
|
/// EmitElseIf parameter controls the type of condition that is emitted ('if
|
2009-10-18 22:51:30 +00:00
|
|
|
/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
|
|
|
|
/// .. else {..}').
|
2008-05-30 06:10:19 +00:00
|
|
|
template <typename F>
|
2009-10-18 22:51:30 +00:00
|
|
|
void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
|
2008-05-31 13:43:21 +00:00
|
|
|
F Callback, bool EmitElseIf,
|
2008-12-07 16:41:11 +00:00
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 00:10:29 +00:00
|
|
|
raw_ostream& O) {
|
2009-10-18 22:51:30 +00:00
|
|
|
WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
|
|
|
|
EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 07:48:34 +00:00
|
|
|
/// TokenizeCmdLine - converts from
|
2009-12-15 07:20:50 +00:00
|
|
|
/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
|
|
|
|
/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
|
2009-12-17 07:48:34 +00:00
|
|
|
void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
|
2009-01-21 13:04:00 +00:00
|
|
|
const char* Delimiters = " \t\n\v\f\r";
|
|
|
|
enum TokenizerState
|
|
|
|
{ Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
|
|
|
|
cur_st = Normal;
|
2009-06-25 18:21:34 +00:00
|
|
|
|
|
|
|
if (CmdLine.empty())
|
|
|
|
return;
|
2009-01-21 13:04:00 +00:00
|
|
|
Out.push_back("");
|
|
|
|
|
|
|
|
std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
|
|
|
|
E = CmdLine.size();
|
2009-06-25 18:21:34 +00:00
|
|
|
|
2009-01-21 13:04:00 +00:00
|
|
|
for (; B != E; ++B) {
|
|
|
|
char cur_ch = CmdLine[B];
|
|
|
|
|
|
|
|
switch (cur_st) {
|
|
|
|
case Normal:
|
|
|
|
if (cur_ch == '$') {
|
|
|
|
cur_st = SpecialCommand;
|
|
|
|
break;
|
|
|
|
}
|
2009-12-23 12:49:41 +00:00
|
|
|
if (OneOf(Delimiters, cur_ch)) {
|
2009-01-21 13:04:00 +00:00
|
|
|
// Skip whitespace
|
|
|
|
B = CmdLine.find_first_not_of(Delimiters, B);
|
|
|
|
if (B == std::string::npos) {
|
|
|
|
B = E-1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
--B;
|
|
|
|
Out.push_back("");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case SpecialCommand:
|
2009-12-23 12:49:41 +00:00
|
|
|
if (OneOf(Delimiters, cur_ch)) {
|
2009-01-21 13:04:00 +00:00
|
|
|
cur_st = Normal;
|
|
|
|
Out.push_back("");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (cur_ch == '(') {
|
|
|
|
Out.push_back("");
|
|
|
|
cur_st = InsideSpecialCommand;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case InsideSpecialCommand:
|
2009-12-23 12:49:41 +00:00
|
|
|
if (OneOf(Delimiters, cur_ch)) {
|
2009-01-21 13:04:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (cur_ch == '\'') {
|
|
|
|
cur_st = InsideQuotationMarks;
|
|
|
|
Out.push_back("");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (cur_ch == ')') {
|
|
|
|
cur_st = Normal;
|
|
|
|
Out.push_back("");
|
|
|
|
}
|
|
|
|
if (cur_ch == ',') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case InsideQuotationMarks:
|
|
|
|
if (cur_ch == '\'') {
|
|
|
|
cur_st = InsideSpecialCommand;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Out.back().push_back(cur_ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-15 03:04:02 +00:00
|
|
|
/// SubstituteCall - Given "$CALL(HookName, [Arg1 [, Arg2 [...]]])", output
|
|
|
|
/// "hooks::HookName([Arg1 [, Arg2 [, ...]]])". Helper function used by
|
|
|
|
/// SubstituteSpecialCommands().
|
|
|
|
StrVector::const_iterator
|
|
|
|
SubstituteCall (StrVector::const_iterator Pos,
|
|
|
|
StrVector::const_iterator End,
|
|
|
|
bool IsJoin, raw_ostream& O)
|
2009-01-21 13:04:00 +00:00
|
|
|
{
|
2009-12-15 03:04:02 +00:00
|
|
|
const char* errorMessage = "Syntax error in $CALL invocation!";
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckedIncrement(Pos, End, errorMessage);
|
2009-12-15 03:04:02 +00:00
|
|
|
const std::string& CmdName = *Pos;
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-15 03:04:02 +00:00
|
|
|
if (CmdName == ")")
|
|
|
|
throw "$CALL invocation: empty argument list!";
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-15 03:04:02 +00:00
|
|
|
O << "hooks::";
|
|
|
|
O << CmdName << "(";
|
2008-05-30 06:13:29 +00:00
|
|
|
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-15 03:04:02 +00:00
|
|
|
bool firstIteration = true;
|
|
|
|
while (true) {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckedIncrement(Pos, End, errorMessage);
|
2009-12-15 03:04:02 +00:00
|
|
|
const std::string& Arg = *Pos;
|
|
|
|
assert(Arg.size() != 0);
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-15 03:04:02 +00:00
|
|
|
if (Arg[0] == ')')
|
|
|
|
break;
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-15 03:04:02 +00:00
|
|
|
if (firstIteration)
|
|
|
|
firstIteration = false;
|
|
|
|
else
|
|
|
|
O << ", ";
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-15 03:04:02 +00:00
|
|
|
if (Arg == "$INFILE") {
|
|
|
|
if (IsJoin)
|
|
|
|
throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
|
2009-01-21 13:04:00 +00:00
|
|
|
else
|
2009-12-15 03:04:02 +00:00
|
|
|
O << "inFile.c_str()";
|
|
|
|
}
|
|
|
|
else {
|
2009-01-21 13:04:00 +00:00
|
|
|
O << '"' << Arg << '"';
|
|
|
|
}
|
2009-12-15 03:04:02 +00:00
|
|
|
}
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-15 03:04:02 +00:00
|
|
|
O << ')';
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-15 03:04:02 +00:00
|
|
|
return Pos;
|
|
|
|
}
|
2008-05-30 06:13:29 +00:00
|
|
|
|
2009-12-15 03:04:02 +00:00
|
|
|
/// SubstituteEnv - Given '$ENV(VAR_NAME)', output 'getenv("VAR_NAME")'. Helper
|
|
|
|
/// function used by SubstituteSpecialCommands().
|
|
|
|
StrVector::const_iterator
|
|
|
|
SubstituteEnv (StrVector::const_iterator Pos,
|
|
|
|
StrVector::const_iterator End, raw_ostream& O)
|
|
|
|
{
|
|
|
|
const char* errorMessage = "Syntax error in $ENV invocation!";
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckedIncrement(Pos, End, errorMessage);
|
2009-12-15 03:04:02 +00:00
|
|
|
const std::string& EnvName = *Pos;
|
|
|
|
|
|
|
|
if (EnvName == ")")
|
|
|
|
throw "$ENV invocation: empty argument list!";
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-15 03:04:02 +00:00
|
|
|
O << "checkCString(std::getenv(\"";
|
|
|
|
O << EnvName;
|
|
|
|
O << "\"))";
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckedIncrement(Pos, End, errorMessage);
|
2009-12-15 03:04:02 +00:00
|
|
|
|
|
|
|
return Pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SubstituteSpecialCommands - Given an invocation of $CALL or $ENV, output
|
|
|
|
/// handler code. Helper function used by EmitCmdLineVecFill().
|
|
|
|
StrVector::const_iterator
|
|
|
|
SubstituteSpecialCommands (StrVector::const_iterator Pos,
|
|
|
|
StrVector::const_iterator End,
|
|
|
|
bool IsJoin, raw_ostream& O)
|
|
|
|
{
|
|
|
|
|
|
|
|
const std::string& cmd = *Pos;
|
|
|
|
|
|
|
|
// Perform substitution.
|
|
|
|
if (cmd == "$CALL") {
|
|
|
|
Pos = SubstituteCall(Pos, End, IsJoin, O);
|
|
|
|
}
|
|
|
|
else if (cmd == "$ENV") {
|
|
|
|
Pos = SubstituteEnv(Pos, End, O);
|
2008-05-30 06:13:29 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw "Unknown special command: " + cmd;
|
|
|
|
}
|
|
|
|
|
2009-12-15 03:04:02 +00:00
|
|
|
// Handle '$CMD(ARG)/additional/text'.
|
2009-01-21 13:04:00 +00:00
|
|
|
const std::string& Leftover = *Pos;
|
|
|
|
assert(Leftover.at(0) == ')');
|
|
|
|
if (Leftover.size() != 1)
|
|
|
|
O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
|
2008-05-30 06:13:29 +00:00
|
|
|
|
2009-01-21 13:04:00 +00:00
|
|
|
return Pos;
|
2008-05-30 06:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitCmdLineVecFill - Emit code that fills in the command line
|
|
|
|
/// vector. Helper function used by EmitGenerateActionMethod().
|
2008-05-30 06:10:19 +00:00
|
|
|
void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
|
2009-09-21 15:53:44 +00:00
|
|
|
bool IsJoin, unsigned IndentLevel,
|
2009-07-03 00:10:29 +00:00
|
|
|
raw_ostream& O) {
|
2008-05-30 06:10:19 +00:00
|
|
|
StrVector StrVec;
|
2009-12-17 07:48:34 +00:00
|
|
|
TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2008-05-30 06:12:24 +00:00
|
|
|
if (StrVec.empty())
|
2009-06-25 18:21:34 +00:00
|
|
|
throw "Tool '" + ToolName + "' has empty command line!";
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2010-02-23 09:04:44 +00:00
|
|
|
StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2010-02-23 09:04:44 +00:00
|
|
|
// Emit the command itself.
|
2009-04-19 00:22:35 +00:00
|
|
|
assert(!StrVec[0].empty());
|
2010-02-23 09:04:44 +00:00
|
|
|
O.indent(IndentLevel) << "cmd = ";
|
2009-01-21 13:04:00 +00:00
|
|
|
if (StrVec[0][0] == '$') {
|
2010-02-23 09:04:44 +00:00
|
|
|
B = SubstituteSpecialCommands(B, E, IsJoin, O);
|
|
|
|
++B;
|
2009-01-21 13:04:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-02-23 09:04:44 +00:00
|
|
|
O << '"' << StrVec[0] << '"';
|
|
|
|
++B;
|
2009-01-21 13:04:00 +00:00
|
|
|
}
|
2010-02-23 09:04:44 +00:00
|
|
|
O << ";\n";
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2010-02-23 09:04:44 +00:00
|
|
|
// Go through the command arguments.
|
|
|
|
assert(B <= E);
|
|
|
|
for (; B != E; ++B) {
|
|
|
|
const std::string& cmd = *B;
|
2009-10-08 04:40:08 +00:00
|
|
|
|
2009-04-19 00:22:35 +00:00
|
|
|
assert(!cmd.empty());
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(IndentLevel);
|
2010-02-23 09:04:44 +00:00
|
|
|
|
2008-05-30 06:12:24 +00:00
|
|
|
if (cmd.at(0) == '$') {
|
2010-02-23 09:04:44 +00:00
|
|
|
O << "vec.push_back(std::make_pair(0, ";
|
|
|
|
B = SubstituteSpecialCommands(B, E, IsJoin, O);
|
|
|
|
O << "));\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-02-23 09:04:44 +00:00
|
|
|
O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2008-05-30 06:10:19 +00:00
|
|
|
}
|
|
|
|
|
2010-07-19 17:17:22 +00:00
|
|
|
/// EmitForEachListElementCycleHeader - Emit common code for iterating through
|
|
|
|
/// all elements of a list. Helper function used by
|
|
|
|
/// EmitForwardOptionPropertyHandlingCode.
|
|
|
|
void EmitForEachListElementCycleHeader (const OptionDescription& D,
|
|
|
|
unsigned IndentLevel,
|
|
|
|
raw_ostream& O) {
|
|
|
|
unsigned IndentLevel1 = IndentLevel + Indent1;
|
|
|
|
|
|
|
|
O.indent(IndentLevel)
|
|
|
|
<< "for (" << D.GenTypeDeclaration()
|
|
|
|
<< "::iterator B = " << D.GenVariableName() << ".begin(),\n";
|
|
|
|
O.indent(IndentLevel)
|
|
|
|
<< "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
|
|
|
|
O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
|
|
|
|
<< ".getPosition(B - " << D.GenVariableName()
|
|
|
|
<< ".begin());\n";
|
|
|
|
}
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// EmitForwardOptionPropertyHandlingCode - Helper function used to
|
|
|
|
/// implement EmitActionHandler. Emits code for
|
|
|
|
/// handling the (forward) and (forward_as) option properties.
|
|
|
|
void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
|
2009-09-21 15:53:44 +00:00
|
|
|
unsigned IndentLevel,
|
2008-12-07 16:41:11 +00:00
|
|
|
const std::string& NewName,
|
2009-07-03 00:10:29 +00:00
|
|
|
raw_ostream& O) {
|
2008-12-07 16:41:11 +00:00
|
|
|
const std::string& Name = NewName.empty()
|
|
|
|
? ("-" + D.Name)
|
|
|
|
: NewName;
|
2009-09-21 15:53:44 +00:00
|
|
|
unsigned IndentLevel1 = IndentLevel + Indent1;
|
2008-12-07 16:41:11 +00:00
|
|
|
|
|
|
|
switch (D.Type) {
|
|
|
|
case OptionType::Switch:
|
2010-02-23 09:04:44 +00:00
|
|
|
O.indent(IndentLevel)
|
2010-02-23 09:04:57 +00:00
|
|
|
<< "vec.push_back(std::make_pair(" << D.GenVariableName()
|
|
|
|
<< ".getPosition(), \"" << Name << "\"));\n";
|
2008-12-07 16:41:11 +00:00
|
|
|
break;
|
|
|
|
case OptionType::Parameter:
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(IndentLevel) << "vec.push_back(std::make_pair("
|
|
|
|
<< D.GenVariableName()
|
|
|
|
<<".getPosition(), \"" << Name;
|
2010-02-23 09:04:13 +00:00
|
|
|
|
|
|
|
if (!D.isForwardNotSplit()) {
|
2010-02-23 09:04:44 +00:00
|
|
|
O << "\"));\n";
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(IndentLevel) << "vec.push_back(std::make_pair("
|
|
|
|
<< D.GenVariableName() << ".getPosition(), "
|
2010-02-23 09:04:44 +00:00
|
|
|
<< D.GenVariableName() << "));\n";
|
2010-02-23 09:04:13 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-02-23 09:04:44 +00:00
|
|
|
O << "=\" + " << D.GenVariableName() << "));\n";
|
2010-02-23 09:04:13 +00:00
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
break;
|
|
|
|
case OptionType::Prefix:
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(IndentLevel) << "vec.push_back(std::make_pair("
|
|
|
|
<< D.GenVariableName() << ".getPosition(), \""
|
2010-02-23 09:04:44 +00:00
|
|
|
<< Name << "\" + "
|
|
|
|
<< D.GenVariableName() << "));\n";
|
2008-12-07 16:41:11 +00:00
|
|
|
break;
|
|
|
|
case OptionType::PrefixList:
|
2010-07-19 17:17:22 +00:00
|
|
|
EmitForEachListElementCycleHeader(D, IndentLevel, O);
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
|
2010-02-23 09:04:44 +00:00
|
|
|
<< Name << "\" + " << "*B));\n";
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(IndentLevel1) << "++B;\n";
|
2009-01-28 03:47:20 +00:00
|
|
|
|
|
|
|
for (int i = 1, j = D.MultiVal; i < j; ++i) {
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(IndentLevel1) << "++B;\n";
|
2009-01-28 03:47:20 +00:00
|
|
|
}
|
|
|
|
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(IndentLevel) << "}\n";
|
2008-12-07 16:41:11 +00:00
|
|
|
break;
|
|
|
|
case OptionType::ParameterList:
|
2010-07-19 17:17:22 +00:00
|
|
|
EmitForEachListElementCycleHeader(D, IndentLevel, O);
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
|
2010-02-23 09:04:44 +00:00
|
|
|
<< Name << "\"));\n";
|
2009-01-28 03:47:20 +00:00
|
|
|
|
|
|
|
for (int i = 0, j = D.MultiVal; i < j; ++i) {
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(IndentLevel1) << "++B;\n";
|
2009-01-28 03:47:20 +00:00
|
|
|
}
|
|
|
|
|
2010-07-19 03:16:25 +00:00
|
|
|
O.indent(IndentLevel) << "}\n";
|
|
|
|
break;
|
|
|
|
case OptionType::SwitchList:
|
2010-07-19 17:17:22 +00:00
|
|
|
EmitForEachListElementCycleHeader(D, IndentLevel, O);
|
2010-07-19 03:16:25 +00:00
|
|
|
O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
|
|
|
|
<< Name << "\"));\n";
|
|
|
|
O.indent(IndentLevel1) << "++B;\n";
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(IndentLevel) << "}\n";
|
2008-12-07 16:41:11 +00:00
|
|
|
break;
|
|
|
|
case OptionType::Alias:
|
|
|
|
default:
|
2009-12-07 19:15:57 +00:00
|
|
|
throw "Aliases are not allowed in tool option descriptions!";
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-19 21:24:28 +00:00
|
|
|
/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
|
|
|
|
/// EmitPreprocessOptionsCallback.
|
2009-12-17 07:48:49 +00:00
|
|
|
struct ActionHandlingCallbackBase
|
|
|
|
{
|
2009-10-19 21:24:28 +00:00
|
|
|
|
|
|
|
void onErrorDag(const DagInit& d,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
|
|
|
O.indent(IndentLevel)
|
2010-07-23 03:42:55 +00:00
|
|
|
<< "PrintError(\""
|
|
|
|
<< (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0)) : "Unknown error!")
|
2009-10-19 21:24:28 +00:00
|
|
|
<< "\");\n";
|
2010-07-27 11:19:36 +00:00
|
|
|
O.indent(IndentLevel) << "return 1;\n";
|
2009-10-19 21:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onWarningDag(const DagInit& d,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-10-19 21:24:28 +00:00
|
|
|
O.indent(IndentLevel) << "llvm::errs() << \""
|
|
|
|
<< InitPtrToString(d.getArg(0)) << "\";\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/// EmitActionHandlersCallback - Emit code that handles actions. Used by
|
|
|
|
/// EmitGenerateActionMethod() as an argument to EmitCaseConstructHandler().
|
2009-12-07 10:51:55 +00:00
|
|
|
class EmitActionHandlersCallback;
|
2009-12-17 07:48:49 +00:00
|
|
|
|
2009-12-07 10:51:55 +00:00
|
|
|
typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
|
|
|
|
(const DagInit&, unsigned, raw_ostream&) const;
|
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
class EmitActionHandlersCallback :
|
|
|
|
public ActionHandlingCallbackBase,
|
2009-12-07 10:51:55 +00:00
|
|
|
public HandlerTable<EmitActionHandlersCallbackHandler>
|
|
|
|
{
|
|
|
|
typedef EmitActionHandlersCallbackHandler Handler;
|
2008-05-30 06:10:19 +00:00
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
const OptionDescriptions& OptDescs;
|
|
|
|
|
2009-12-15 07:20:50 +00:00
|
|
|
/// EmitHookInvocation - Common code for hook invocation from actions. Used by
|
|
|
|
/// onAppendCmd and onOutputSuffix.
|
|
|
|
void EmitHookInvocation(const std::string& Str,
|
|
|
|
const char* BlockOpen, const char* BlockClose,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
2008-05-30 06:10:19 +00:00
|
|
|
{
|
2009-12-07 10:51:55 +00:00
|
|
|
StrVector Out;
|
2009-12-17 07:48:34 +00:00
|
|
|
TokenizeCmdLine(Str, Out);
|
2008-12-07 16:41:11 +00:00
|
|
|
|
2009-12-07 10:51:55 +00:00
|
|
|
for (StrVector::const_iterator B = Out.begin(), E = Out.end();
|
2009-12-15 07:20:50 +00:00
|
|
|
B != E; ++B) {
|
|
|
|
const std::string& cmd = *B;
|
|
|
|
|
|
|
|
O.indent(IndentLevel) << BlockOpen;
|
|
|
|
|
|
|
|
if (cmd.at(0) == '$')
|
|
|
|
B = SubstituteSpecialCommands(B, E, /* IsJoin = */ true, O);
|
|
|
|
else
|
|
|
|
O << '"' << cmd << '"';
|
|
|
|
|
|
|
|
O << BlockClose;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onAppendCmd (const DagInit& Dag,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(Dag, 1);
|
2009-12-15 07:20:50 +00:00
|
|
|
this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
|
2010-02-23 09:04:57 +00:00
|
|
|
"vec.push_back(std::make_pair(65536, ", "));\n",
|
2010-02-23 09:04:44 +00:00
|
|
|
IndentLevel, O);
|
2009-12-07 10:51:55 +00:00
|
|
|
}
|
2009-02-27 06:46:55 +00:00
|
|
|
|
2009-12-07 10:51:55 +00:00
|
|
|
void onForward (const DagInit& Dag,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(Dag, 1);
|
2009-12-07 10:51:55 +00:00
|
|
|
const std::string& Name = InitPtrToString(Dag.getArg(0));
|
|
|
|
EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
|
|
|
|
IndentLevel, "", O);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onForwardAs (const DagInit& Dag,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(Dag, 2);
|
2009-12-07 10:51:55 +00:00
|
|
|
const std::string& Name = InitPtrToString(Dag.getArg(0));
|
|
|
|
const std::string& NewName = InitPtrToString(Dag.getArg(1));
|
|
|
|
EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
|
|
|
|
IndentLevel, NewName, O);
|
|
|
|
}
|
|
|
|
|
2009-12-07 17:03:05 +00:00
|
|
|
void onForwardValue (const DagInit& Dag,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(Dag, 1);
|
2009-12-07 17:03:05 +00:00
|
|
|
const std::string& Name = InitPtrToString(Dag.getArg(0));
|
2010-07-19 17:17:10 +00:00
|
|
|
const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
|
2009-12-07 17:03:05 +00:00
|
|
|
|
2010-07-19 03:16:25 +00:00
|
|
|
if (D.isSwitchList()) {
|
|
|
|
throw std::runtime_error
|
|
|
|
("forward_value is not allowed with switch_list");
|
|
|
|
}
|
|
|
|
|
2009-12-07 17:03:05 +00:00
|
|
|
if (D.isParameter()) {
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(IndentLevel) << "vec.push_back(std::make_pair("
|
|
|
|
<< D.GenVariableName() << ".getPosition(), "
|
2010-02-23 09:04:44 +00:00
|
|
|
<< D.GenVariableName() << "));\n";
|
2009-12-07 17:03:05 +00:00
|
|
|
}
|
2009-12-07 19:16:13 +00:00
|
|
|
else {
|
2010-07-19 03:16:25 +00:00
|
|
|
O.indent(IndentLevel) << "for (" << D.GenTypeDeclaration()
|
|
|
|
<< "::iterator B = " << D.GenVariableName()
|
|
|
|
<< ".begin(), \n";
|
2010-02-23 09:04:44 +00:00
|
|
|
O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
|
|
|
|
<< ".end(); B != E; ++B)\n";
|
|
|
|
O.indent(IndentLevel) << "{\n";
|
|
|
|
O.indent(IndentLevel + Indent1)
|
2010-02-23 09:04:57 +00:00
|
|
|
<< "unsigned pos = " << D.GenVariableName()
|
|
|
|
<< ".getPosition(B - " << D.GenVariableName()
|
|
|
|
<< ".begin());\n";
|
|
|
|
O.indent(IndentLevel + Indent1)
|
|
|
|
<< "vec.push_back(std::make_pair(pos, *B));\n";
|
2010-02-23 09:04:44 +00:00
|
|
|
O.indent(IndentLevel) << "}\n";
|
2009-12-07 17:03:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onForwardTransformedValue (const DagInit& Dag,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(Dag, 2);
|
2009-12-07 17:03:05 +00:00
|
|
|
const std::string& Name = InitPtrToString(Dag.getArg(0));
|
|
|
|
const std::string& Hook = InitPtrToString(Dag.getArg(1));
|
2010-07-19 17:17:10 +00:00
|
|
|
const OptionDescription& D = OptDescs.FindParameterListOrParameter(Name);
|
2009-12-07 17:03:05 +00:00
|
|
|
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(IndentLevel) << "vec.push_back(std::make_pair("
|
|
|
|
<< D.GenVariableName() << ".getPosition("
|
|
|
|
<< (D.isList() ? "0" : "") << "), "
|
|
|
|
<< "hooks::" << Hook << "(" << D.GenVariableName()
|
2010-02-23 09:04:44 +00:00
|
|
|
<< (D.isParameter() ? ".c_str()" : "") << ")));\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void onNoOutFile (const DagInit& Dag,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
|
|
|
CheckNumberOfArguments(Dag, 0);
|
|
|
|
O.indent(IndentLevel) << "no_out_file = true;\n";
|
2009-12-07 17:03:05 +00:00
|
|
|
}
|
|
|
|
|
2009-12-07 10:51:55 +00:00
|
|
|
void onOutputSuffix (const DagInit& Dag,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(Dag, 1);
|
2009-12-15 07:20:50 +00:00
|
|
|
this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
|
|
|
|
"output_suffix = ", ";\n", IndentLevel, O);
|
2009-12-07 10:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onStopCompilation (const DagInit& Dag,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
|
|
|
O.indent(IndentLevel) << "stop_compilation = true;\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void onUnpackValues (const DagInit& Dag,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
2009-12-07 18:25:54 +00:00
|
|
|
throw "'unpack_values' is deprecated. "
|
|
|
|
"Use 'comma_separated' + 'forward_value' instead!";
|
2008-05-30 06:10:19 +00:00
|
|
|
}
|
2009-12-07 10:51:55 +00:00
|
|
|
|
2008-12-07 16:42:22 +00:00
|
|
|
public:
|
2009-12-07 10:51:55 +00:00
|
|
|
|
|
|
|
explicit EmitActionHandlersCallback(const OptionDescriptions& OD)
|
|
|
|
: OptDescs(OD)
|
|
|
|
{
|
|
|
|
if (!staticMembersInitialized_) {
|
|
|
|
AddHandler("error", &EmitActionHandlersCallback::onErrorDag);
|
|
|
|
AddHandler("warning", &EmitActionHandlersCallback::onWarningDag);
|
|
|
|
AddHandler("append_cmd", &EmitActionHandlersCallback::onAppendCmd);
|
|
|
|
AddHandler("forward", &EmitActionHandlersCallback::onForward);
|
|
|
|
AddHandler("forward_as", &EmitActionHandlersCallback::onForwardAs);
|
2009-12-07 17:03:05 +00:00
|
|
|
AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
|
|
|
|
AddHandler("forward_transformed_value",
|
|
|
|
&EmitActionHandlersCallback::onForwardTransformedValue);
|
2010-02-23 09:04:44 +00:00
|
|
|
AddHandler("no_out_file",
|
|
|
|
&EmitActionHandlersCallback::onNoOutFile);
|
2009-12-07 10:51:55 +00:00
|
|
|
AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
|
|
|
|
AddHandler("stop_compilation",
|
|
|
|
&EmitActionHandlersCallback::onStopCompilation);
|
|
|
|
AddHandler("unpack_values",
|
|
|
|
&EmitActionHandlersCallback::onUnpackValues);
|
|
|
|
|
2010-02-23 09:04:44 +00:00
|
|
|
|
2009-12-07 10:51:55 +00:00
|
|
|
staticMembersInitialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
2008-12-07 16:42:22 +00:00
|
|
|
|
2009-12-17 07:48:49 +00:00
|
|
|
void operator()(const Init* I,
|
2009-10-19 21:24:28 +00:00
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
2008-12-07 16:42:22 +00:00
|
|
|
{
|
2009-12-17 07:48:49 +00:00
|
|
|
InvokeDagInitHandler(this, I, IndentLevel, O);
|
2008-12-07 16:42:22 +00:00
|
|
|
}
|
2008-05-30 06:10:19 +00:00
|
|
|
};
|
|
|
|
|
2009-12-07 10:51:55 +00:00
|
|
|
void EmitGenerateActionMethodHeader(const ToolDescription& D,
|
2010-07-23 03:42:55 +00:00
|
|
|
bool IsJoin, bool Naked,
|
|
|
|
raw_ostream& O)
|
2009-12-07 10:51:55 +00:00
|
|
|
{
|
2010-07-23 03:42:55 +00:00
|
|
|
O.indent(Indent1) << "int GenerateAction(Action& Out,\n";
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
if (IsJoin)
|
2010-07-23 03:42:55 +00:00
|
|
|
O.indent(Indent2) << "const PathVector& inFiles,\n";
|
2008-05-30 06:10:19 +00:00
|
|
|
else
|
2010-07-23 03:42:55 +00:00
|
|
|
O.indent(Indent2) << "const sys::Path& inFile,\n";
|
2009-09-21 15:53:44 +00:00
|
|
|
|
2010-07-23 03:42:55 +00:00
|
|
|
O.indent(Indent2) << "const bool HasChildren,\n";
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
|
|
|
|
O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
|
|
|
|
O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
|
|
|
|
O.indent(Indent1) << "{\n";
|
2010-07-23 03:42:55 +00:00
|
|
|
|
|
|
|
if (!Naked) {
|
|
|
|
O.indent(Indent2) << "std::string cmd;\n";
|
|
|
|
O.indent(Indent2) << "std::string out_file;\n";
|
|
|
|
O.indent(Indent2)
|
|
|
|
<< "std::vector<std::pair<unsigned, std::string> > vec;\n";
|
|
|
|
O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
|
|
|
|
O.indent(Indent2) << "bool no_out_file = false;\n";
|
2010-08-13 02:29:24 +00:00
|
|
|
O.indent(Indent2) << "std::string output_suffix(\""
|
|
|
|
<< D.OutputSuffix << "\");\n";
|
2010-07-23 03:42:55 +00:00
|
|
|
}
|
2009-12-07 10:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
|
|
|
|
// Tool::GenerateAction() method.
|
|
|
|
void EmitGenerateActionMethod (const ToolDescription& D,
|
|
|
|
const OptionDescriptions& OptDescs,
|
|
|
|
bool IsJoin, raw_ostream& O) {
|
|
|
|
|
2010-07-23 03:42:55 +00:00
|
|
|
EmitGenerateActionMethodHeader(D, IsJoin, /* Naked = */ false, O);
|
2008-05-30 06:10:19 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
if (!D.CmdLine)
|
|
|
|
throw "Tool " + D.Name + " has no cmd_line property!";
|
2009-10-09 05:45:21 +00:00
|
|
|
|
2010-02-23 09:04:44 +00:00
|
|
|
// Process the 'command' property.
|
|
|
|
O << '\n';
|
|
|
|
EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
|
|
|
|
O << '\n';
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2010-02-23 09:04:44 +00:00
|
|
|
// Process the 'actions' list of this tool.
|
2009-10-08 04:40:08 +00:00
|
|
|
if (D.Actions)
|
2009-10-27 09:02:49 +00:00
|
|
|
EmitCaseConstructHandler(D.Actions, Indent2,
|
|
|
|
EmitActionHandlersCallback(OptDescs),
|
2009-10-08 04:40:08 +00:00
|
|
|
false, OptDescs, O);
|
|
|
|
O << '\n';
|
2009-10-09 05:45:21 +00:00
|
|
|
|
2010-02-23 09:04:44 +00:00
|
|
|
// Input file (s)
|
|
|
|
if (!D.InFileOption.empty()) {
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(Indent2)
|
|
|
|
<< "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
|
|
|
|
<< D.InFileOption << "\");\n";
|
2010-02-23 09:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsJoin) {
|
|
|
|
O.indent(Indent2)
|
|
|
|
<< "for (PathVector::const_iterator B = inFiles.begin(),\n";
|
|
|
|
O.indent(Indent3) << "E = inFiles.end(); B != E; ++B)\n";
|
|
|
|
O.indent(Indent2) << "{\n";
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(Indent3) << "vec.push_back(std::make_pair("
|
|
|
|
<< "InputFilenames.getPosition(B - inFiles.begin()), "
|
|
|
|
<< "B->str()));\n";
|
2010-02-23 09:04:44 +00:00
|
|
|
O.indent(Indent2) << "}\n";
|
|
|
|
}
|
|
|
|
else {
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(Indent2) << "vec.push_back(std::make_pair("
|
|
|
|
<< "InputFilenames.getPosition(0), inFile.str()));\n";
|
2010-02-23 09:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output file
|
|
|
|
O.indent(Indent2) << "if (!no_out_file) {\n";
|
|
|
|
if (!D.OutFileOption.empty())
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
|
2010-02-23 09:04:44 +00:00
|
|
|
<< D.OutFileOption << "\"));\n";
|
|
|
|
|
|
|
|
O.indent(Indent3) << "out_file = this->OutFilename("
|
|
|
|
<< (IsJoin ? "sys::Path(),\n" : "inFile,\n");
|
2010-08-13 02:29:24 +00:00
|
|
|
O.indent(Indent4) <<
|
|
|
|
"TempDir, stop_compilation, output_suffix.c_str()).str();\n\n";
|
2010-02-23 09:04:57 +00:00
|
|
|
O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
|
2010-02-23 09:04:44 +00:00
|
|
|
|
|
|
|
O.indent(Indent2) << "}\n\n";
|
2009-10-08 04:40:08 +00:00
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
// Handle the Sink property.
|
2010-08-20 11:24:51 +00:00
|
|
|
std::string SinkOption("autogenerated::");
|
|
|
|
SinkOption += SinkOptionName;
|
2008-12-07 16:41:11 +00:00
|
|
|
if (D.isSink()) {
|
2010-08-20 11:24:51 +00:00
|
|
|
O.indent(Indent2) << "if (!" << SinkOption << ".empty()) {\n";
|
2010-02-23 09:04:44 +00:00
|
|
|
O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
|
2010-08-20 11:24:51 +00:00
|
|
|
<< SinkOption << ".begin(), E = " << SinkOption
|
2010-02-23 09:04:44 +00:00
|
|
|
<< ".end(); B != E; ++B)\n";
|
2010-08-20 11:24:51 +00:00
|
|
|
O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOption
|
|
|
|
<< ".getPosition(B - " << SinkOption
|
2010-02-23 09:04:57 +00:00
|
|
|
<< ".begin()), *B));\n";
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent2) << "}\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2010-07-23 03:42:55 +00:00
|
|
|
O.indent(Indent2) << "Out.Construct(cmd, this->SortArgs(vec), "
|
2010-02-23 09:04:44 +00:00
|
|
|
<< "stop_compilation, out_file);\n";
|
2010-07-23 03:42:55 +00:00
|
|
|
O.indent(Indent2) << "return 0;\n";
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent1) << "}\n\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
|
|
|
|
/// a given Tool class.
|
2008-12-07 16:41:11 +00:00
|
|
|
void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
|
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 00:10:29 +00:00
|
|
|
raw_ostream& O) {
|
2009-09-21 15:53:44 +00:00
|
|
|
if (!ToolDesc.isJoin()) {
|
2010-07-23 03:42:55 +00:00
|
|
|
EmitGenerateActionMethodHeader(ToolDesc, /* IsJoin = */ true,
|
|
|
|
/* Naked = */ true, O);
|
|
|
|
O.indent(Indent2) << "PrintError(\"" << ToolDesc.Name
|
2009-09-21 15:53:44 +00:00
|
|
|
<< " is not a Join tool!\");\n";
|
2010-07-23 03:42:55 +00:00
|
|
|
O.indent(Indent2) << "return -1;\n";
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent1) << "}\n\n";
|
|
|
|
}
|
|
|
|
else {
|
2008-12-07 16:41:11 +00:00
|
|
|
EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
|
2009-09-21 15:53:44 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
|
|
|
|
/// methods for a given Tool class.
|
2009-07-03 00:10:29 +00:00
|
|
|
void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent1) << "const char** InputLanguages() const {\n";
|
|
|
|
O.indent(Indent2) << "return InputLanguages_;\n";
|
|
|
|
O.indent(Indent1) << "}\n\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2010-09-21 14:59:42 +00:00
|
|
|
O.indent(Indent1) << "const char** OutputLanguages() const {\n";
|
|
|
|
O.indent(Indent2) << "return OutputLanguages_;\n";
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent1) << "}\n\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitNameMethod - Emit the Name() method for a given Tool class.
|
2009-07-03 00:10:29 +00:00
|
|
|
void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent1) << "const char* Name() const {\n";
|
|
|
|
O.indent(Indent2) << "return \"" << D.Name << "\";\n";
|
|
|
|
O.indent(Indent1) << "}\n\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
|
|
|
|
/// class.
|
2009-07-03 00:10:29 +00:00
|
|
|
void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent1) << "bool IsJoin() const {\n";
|
2008-12-07 16:41:11 +00:00
|
|
|
if (D.isJoin())
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent2) << "return true;\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
else
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent2) << "return false;\n";
|
|
|
|
O.indent(Indent1) << "}\n\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 09:04:28 +00:00
|
|
|
/// EmitWorksOnEmptyCallback - Callback used by EmitWorksOnEmptyMethod in
|
|
|
|
/// conjunction with EmitCaseConstructHandler.
|
|
|
|
void EmitWorksOnEmptyCallback (const Init* Value,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) {
|
|
|
|
CheckBooleanConstant(Value);
|
|
|
|
O.indent(IndentLevel) << "return " << Value->getAsString() << ";\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitWorksOnEmptyMethod - Emit the WorksOnEmpty() method for a given Tool
|
|
|
|
/// class.
|
|
|
|
void EmitWorksOnEmptyMethod (const ToolDescription& D,
|
|
|
|
const OptionDescriptions& OptDescs,
|
|
|
|
raw_ostream& O)
|
|
|
|
{
|
|
|
|
O.indent(Indent1) << "bool WorksOnEmpty() const {\n";
|
|
|
|
if (D.OnEmpty == 0)
|
|
|
|
O.indent(Indent2) << "return false;\n";
|
|
|
|
else
|
|
|
|
EmitCaseConstructHandler(D.OnEmpty, Indent2, EmitWorksOnEmptyCallback,
|
|
|
|
/*EmitElseIf = */ true, OptDescs, O);
|
|
|
|
O.indent(Indent1) << "}\n\n";
|
|
|
|
}
|
|
|
|
|
2010-09-21 14:59:42 +00:00
|
|
|
/// EmitStrArray - Emit definition of a 'const char**' static member
|
|
|
|
/// variable. Helper used by EmitStaticMemberDefinitions();
|
|
|
|
void EmitStrArray(const std::string& Name, const std::string& VarName,
|
|
|
|
const StrVector& StrVec, raw_ostream& O) {
|
|
|
|
O << "const char* " << Name << "::" << VarName << "[] = {";
|
|
|
|
for (StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
|
|
|
|
B != E; ++B)
|
|
|
|
O << '\"' << *B << "\", ";
|
|
|
|
O << "0};\n";
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:24:49 +00:00
|
|
|
/// EmitStaticMemberDefinitions - Emit static member definitions for a
|
|
|
|
/// given Tool class.
|
2009-07-03 00:10:29 +00:00
|
|
|
void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
|
2008-12-07 16:41:11 +00:00
|
|
|
if (D.InLanguage.empty())
|
|
|
|
throw "Tool " + D.Name + " has no 'in_language' property!";
|
2010-09-21 14:59:42 +00:00
|
|
|
if (D.OutLanguage.empty())
|
|
|
|
throw "Tool " + D.Name + " has no 'out_language' property!";
|
2008-12-07 16:41:11 +00:00
|
|
|
|
2010-09-21 14:59:42 +00:00
|
|
|
EmitStrArray(D.Name, "InputLanguages_", D.InLanguage, O);
|
|
|
|
EmitStrArray(D.Name, "OutputLanguages_", D.OutLanguage, O);
|
|
|
|
O << '\n';
|
2008-05-30 06:24:49 +00:00
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitToolClassDefinition - Emit a Tool class definition.
|
2008-12-07 16:41:11 +00:00
|
|
|
void EmitToolClassDefinition (const ToolDescription& D,
|
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 00:10:29 +00:00
|
|
|
raw_ostream& O) {
|
2008-12-07 16:41:11 +00:00
|
|
|
if (D.Name == "root")
|
2008-05-06 16:35:25 +00:00
|
|
|
return;
|
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
// Header
|
2008-12-07 16:41:11 +00:00
|
|
|
O << "class " << D.Name << " : public ";
|
|
|
|
if (D.isJoin())
|
2008-05-06 17:26:53 +00:00
|
|
|
O << "JoinTool";
|
|
|
|
else
|
|
|
|
O << "Tool";
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2009-12-15 07:21:14 +00:00
|
|
|
O << " {\nprivate:\n";
|
2010-09-21 14:59:42 +00:00
|
|
|
O.indent(Indent1) << "static const char* InputLanguages_[];\n";
|
|
|
|
O.indent(Indent1) << "static const char* OutputLanguages_[];\n\n";
|
2008-05-30 06:24:49 +00:00
|
|
|
|
|
|
|
O << "public:\n";
|
2008-12-07 16:41:11 +00:00
|
|
|
EmitNameMethod(D, O);
|
|
|
|
EmitInOutLanguageMethods(D, O);
|
|
|
|
EmitIsJoinMethod(D, O);
|
2010-02-23 09:04:28 +00:00
|
|
|
EmitWorksOnEmptyMethod(D, OptDescs, O);
|
2008-12-07 16:41:11 +00:00
|
|
|
EmitGenerateActionMethods(D, OptDescs, O);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
// Close class definition
|
2008-05-30 06:24:49 +00:00
|
|
|
O << "};\n";
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
EmitStaticMemberDefinitions(D, O);
|
2008-05-30 06:24:49 +00:00
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2009-06-23 20:45:07 +00:00
|
|
|
/// EmitOptionDefinitions - Iterate over a list of option descriptions
|
2008-12-07 16:41:11 +00:00
|
|
|
/// and emit registration code.
|
2009-06-23 20:45:07 +00:00
|
|
|
void EmitOptionDefinitions (const OptionDescriptions& descs,
|
2010-08-15 07:07:12 +00:00
|
|
|
bool HasSink, raw_ostream& O)
|
2008-03-23 08:57:20 +00:00
|
|
|
{
|
2008-12-07 16:41:11 +00:00
|
|
|
std::vector<OptionDescription> Aliases;
|
2008-05-30 06:22:52 +00:00
|
|
|
|
2008-05-30 06:23:29 +00:00
|
|
|
// Emit static cl::Option variables.
|
2008-12-07 16:41:11 +00:00
|
|
|
for (OptionDescriptions::const_iterator B = descs.begin(),
|
2008-03-23 08:57:20 +00:00
|
|
|
E = descs.end(); B!=E; ++B) {
|
2008-12-07 16:41:11 +00:00
|
|
|
const OptionDescription& val = B->second;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-30 06:22:52 +00:00
|
|
|
if (val.Type == OptionType::Alias) {
|
|
|
|
Aliases.push_back(val);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
O << val.GenTypeDeclaration() << ' '
|
2010-08-20 11:24:51 +00:00
|
|
|
<< val.GenPlainVariableName();
|
2008-12-07 16:41:11 +00:00
|
|
|
|
2009-06-23 20:45:31 +00:00
|
|
|
O << "(\"" << val.Name << "\"\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
|
|
|
|
O << ", cl::Prefix";
|
|
|
|
|
|
|
|
if (val.isRequired()) {
|
2009-07-07 16:08:11 +00:00
|
|
|
if (val.isList() && !val.isMultiVal())
|
2008-03-23 08:57:20 +00:00
|
|
|
O << ", cl::OneOrMore";
|
2009-01-28 03:47:20 +00:00
|
|
|
else
|
2008-03-23 08:57:20 +00:00
|
|
|
O << ", cl::Required";
|
2009-01-28 03:47:20 +00:00
|
|
|
}
|
2010-03-05 04:46:39 +00:00
|
|
|
|
|
|
|
if (val.isOptional())
|
2009-12-15 03:04:52 +00:00
|
|
|
O << ", cl::Optional";
|
2010-03-05 04:46:39 +00:00
|
|
|
|
|
|
|
if (val.isOneOrMore())
|
|
|
|
O << ", cl::OneOrMore";
|
|
|
|
|
|
|
|
if (val.isZeroOrMore())
|
|
|
|
O << ", cl::ZeroOrMore";
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2009-12-07 18:25:54 +00:00
|
|
|
if (val.isReallyHidden())
|
2009-01-28 03:47:20 +00:00
|
|
|
O << ", cl::ReallyHidden";
|
2009-12-07 18:25:54 +00:00
|
|
|
else if (val.isHidden())
|
2009-01-28 03:47:20 +00:00
|
|
|
O << ", cl::Hidden";
|
2009-12-07 18:25:54 +00:00
|
|
|
|
|
|
|
if (val.isCommaSeparated())
|
|
|
|
O << ", cl::CommaSeparated";
|
2008-11-28 00:13:25 +00:00
|
|
|
|
2009-01-28 03:47:20 +00:00
|
|
|
if (val.MultiVal > 1)
|
2009-07-07 16:08:41 +00:00
|
|
|
O << ", cl::multi_val(" << val.MultiVal << ')';
|
|
|
|
|
|
|
|
if (val.InitVal) {
|
|
|
|
const std::string& str = val.InitVal->getAsString();
|
|
|
|
O << ", cl::init(" << str << ')';
|
|
|
|
}
|
2009-01-28 03:47:20 +00:00
|
|
|
|
2008-05-30 06:22:52 +00:00
|
|
|
if (!val.Help.empty())
|
|
|
|
O << ", cl::desc(\"" << val.Help << "\")";
|
|
|
|
|
2009-06-23 20:45:31 +00:00
|
|
|
O << ");\n\n";
|
2008-05-30 06:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the aliases (they should go after all the 'proper' options).
|
2008-12-07 16:41:11 +00:00
|
|
|
for (std::vector<OptionDescription>::const_iterator
|
2008-05-30 06:22:52 +00:00
|
|
|
B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
|
2008-12-07 16:41:11 +00:00
|
|
|
const OptionDescription& val = *B;
|
2008-05-30 06:22:52 +00:00
|
|
|
|
|
|
|
O << val.GenTypeDeclaration() << ' '
|
2010-08-20 18:16:26 +00:00
|
|
|
<< val.GenPlainVariableName()
|
2008-05-30 06:22:52 +00:00
|
|
|
<< "(\"" << val.Name << '\"';
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
const OptionDescription& D = descs.FindOption(val.Help);
|
|
|
|
O << ", cl::aliasopt(" << D.GenVariableName() << ")";
|
2008-05-30 06:22:52 +00:00
|
|
|
|
|
|
|
O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 06:22:52 +00:00
|
|
|
// Emit the sink option.
|
2008-12-07 16:41:11 +00:00
|
|
|
if (HasSink)
|
2010-08-20 11:24:51 +00:00
|
|
|
O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
O << '\n';
|
|
|
|
}
|
|
|
|
|
2009-10-19 21:24:28 +00:00
|
|
|
/// EmitPreprocessOptionsCallback - Helper function passed to
|
2009-10-17 20:09:29 +00:00
|
|
|
/// EmitCaseConstructHandler() by EmitPreprocessOptions().
|
2009-12-17 07:48:49 +00:00
|
|
|
|
|
|
|
class EmitPreprocessOptionsCallback;
|
|
|
|
|
|
|
|
typedef void
|
|
|
|
(EmitPreprocessOptionsCallback::* EmitPreprocessOptionsCallbackHandler)
|
|
|
|
(const DagInit&, unsigned, raw_ostream&) const;
|
|
|
|
|
|
|
|
class EmitPreprocessOptionsCallback :
|
|
|
|
public ActionHandlingCallbackBase,
|
|
|
|
public HandlerTable<EmitPreprocessOptionsCallbackHandler>
|
|
|
|
{
|
|
|
|
typedef EmitPreprocessOptionsCallbackHandler Handler;
|
2009-12-17 07:49:16 +00:00
|
|
|
typedef void
|
|
|
|
(EmitPreprocessOptionsCallback::* HandlerImpl)
|
|
|
|
(const Init*, unsigned, raw_ostream&) const;
|
2009-12-17 07:48:49 +00:00
|
|
|
|
2009-10-17 20:09:29 +00:00
|
|
|
const OptionDescriptions& OptDescs_;
|
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
void onEachArgument(const DagInit& d, HandlerImpl h,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
2009-12-17 07:49:16 +00:00
|
|
|
{
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 07:49:16 +00:00
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
for (unsigned i = 0, NumArgs = d.getNumArgs(); i < NumArgs; ++i) {
|
|
|
|
((this)->*(h))(d.getArg(i), IndentLevel, O);
|
2009-12-17 07:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onUnsetOptionImpl(const Init* I,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
2009-12-17 07:48:49 +00:00
|
|
|
{
|
|
|
|
const std::string& OptName = InitPtrToString(I);
|
2009-10-17 20:09:29 +00:00
|
|
|
const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
|
|
|
|
|
2009-10-22 04:15:07 +00:00
|
|
|
if (OptDesc.isSwitch()) {
|
2009-10-17 20:09:29 +00:00
|
|
|
O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
|
|
|
|
}
|
2009-10-22 04:15:07 +00:00
|
|
|
else if (OptDesc.isParameter()) {
|
2009-10-17 20:09:29 +00:00
|
|
|
O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
|
|
|
|
}
|
2009-10-22 04:15:07 +00:00
|
|
|
else if (OptDesc.isList()) {
|
|
|
|
O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
|
|
|
|
}
|
2009-10-17 20:09:29 +00:00
|
|
|
else {
|
2009-12-07 17:03:05 +00:00
|
|
|
throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
|
2009-10-17 20:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-17 07:49:16 +00:00
|
|
|
void onUnsetOption(const DagInit& d,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
2009-10-17 20:09:29 +00:00
|
|
|
{
|
2010-10-23 07:32:46 +00:00
|
|
|
this->onEachArgument(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
|
|
|
|
IndentLevel, O);
|
2009-12-17 07:48:49 +00:00
|
|
|
}
|
2009-10-17 20:09:29 +00:00
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
void onSetOptionImpl(const DagInit& D,
|
2009-12-23 12:49:30 +00:00
|
|
|
unsigned IndentLevel, raw_ostream& O) const {
|
2010-10-23 07:32:46 +00:00
|
|
|
CheckNumberOfArguments(D, 2);
|
|
|
|
|
|
|
|
const std::string& OptName = InitPtrToString(D.getArg(0));
|
2009-12-17 07:49:16 +00:00
|
|
|
const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
|
2010-10-23 07:32:46 +00:00
|
|
|
const Init* Value = D.getArg(1);
|
2009-12-17 07:49:16 +00:00
|
|
|
|
2009-12-18 11:27:26 +00:00
|
|
|
if (OptDesc.isList()) {
|
|
|
|
const ListInit& List = InitPtrToList(Value);
|
|
|
|
|
|
|
|
O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
|
|
|
|
for (ListInit::const_iterator B = List.begin(), E = List.end();
|
|
|
|
B != E; ++B) {
|
2010-07-19 17:17:10 +00:00
|
|
|
const Init* CurElem = *B;
|
|
|
|
if (OptDesc.isSwitchList())
|
|
|
|
CheckBooleanConstant(CurElem);
|
|
|
|
|
|
|
|
O.indent(IndentLevel)
|
|
|
|
<< OptDesc.GenVariableName() << ".push_back(\""
|
|
|
|
<< (OptDesc.isSwitchList() ? CurElem->getAsString()
|
|
|
|
: InitPtrToString(CurElem))
|
|
|
|
<< "\");\n";
|
2009-12-18 11:27:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-23 12:49:30 +00:00
|
|
|
else if (OptDesc.isSwitch()) {
|
|
|
|
CheckBooleanConstant(Value);
|
|
|
|
O.indent(IndentLevel) << OptDesc.GenVariableName()
|
|
|
|
<< " = " << Value->getAsString() << ";\n";
|
|
|
|
}
|
2009-12-18 11:27:26 +00:00
|
|
|
else if (OptDesc.isParameter()) {
|
|
|
|
const std::string& Str = InitPtrToString(Value);
|
2009-12-17 07:49:16 +00:00
|
|
|
O.indent(IndentLevel) << OptDesc.GenVariableName()
|
2009-12-18 11:27:26 +00:00
|
|
|
<< " = \"" << Str << "\";\n";
|
|
|
|
}
|
|
|
|
else {
|
2010-10-23 07:32:46 +00:00
|
|
|
throw "Can't apply 'set_option' to alias option '" + OptName + "'!";
|
2009-12-18 11:27:26 +00:00
|
|
|
}
|
2009-12-17 07:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onSetSwitch(const Init* I,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const {
|
|
|
|
const std::string& OptName = InitPtrToString(I);
|
|
|
|
const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
|
|
|
|
|
|
|
|
if (OptDesc.isSwitch())
|
|
|
|
O.indent(IndentLevel) << OptDesc.GenVariableName() << " = true;\n";
|
|
|
|
else
|
2009-12-18 11:27:26 +00:00
|
|
|
throw "set_option: -" + OptName + " is not a switch option!";
|
2009-12-17 07:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onSetOption(const DagInit& d,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
2009-12-17 07:48:49 +00:00
|
|
|
{
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 07:48:49 +00:00
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
// 2-argument form: (set_option "A", true), (set_option "B", "C"),
|
|
|
|
// (set_option "D", ["E", "F"])
|
|
|
|
if (d.getNumArgs() == 2) {
|
|
|
|
const OptionDescription& OptDesc =
|
|
|
|
OptDescs_.FindOption(InitPtrToString(d.getArg(0)));
|
|
|
|
const Init* Opt2 = d.getArg(1);
|
|
|
|
|
|
|
|
if (!OptDesc.isSwitch() || typeid(*Opt2) != typeid(StringInit)) {
|
|
|
|
this->onSetOptionImpl(d, IndentLevel, O);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multiple argument form: (set_option "A"), (set_option "B", "C", "D")
|
|
|
|
this->onEachArgument(d, &EmitPreprocessOptionsCallback::onSetSwitch,
|
|
|
|
IndentLevel, O);
|
2009-10-17 20:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2009-10-19 21:24:28 +00:00
|
|
|
EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
|
2009-10-17 20:09:29 +00:00
|
|
|
: OptDescs_(OptDescs)
|
2009-12-17 07:48:49 +00:00
|
|
|
{
|
|
|
|
if (!staticMembersInitialized_) {
|
|
|
|
AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
|
|
|
|
AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
|
|
|
|
AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
|
2009-12-17 07:49:16 +00:00
|
|
|
AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
|
2009-12-17 07:48:49 +00:00
|
|
|
|
|
|
|
staticMembersInitialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(const Init* I,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
|
|
|
InvokeDagInitHandler(this, I, IndentLevel, O);
|
|
|
|
}
|
|
|
|
|
2009-10-17 20:09:29 +00:00
|
|
|
};
|
|
|
|
|
2010-08-15 07:07:12 +00:00
|
|
|
/// EmitPreprocessOptions - Emit the PreprocessOptions() function.
|
2009-10-17 20:09:29 +00:00
|
|
|
void EmitPreprocessOptions (const RecordKeeper& Records,
|
|
|
|
const OptionDescriptions& OptDecs, raw_ostream& O)
|
|
|
|
{
|
2010-08-15 07:07:12 +00:00
|
|
|
O << "int PreprocessOptions () {\n";
|
2009-10-17 20:09:29 +00:00
|
|
|
|
|
|
|
const RecordVector& OptionPreprocessors =
|
|
|
|
Records.getAllDerivedDefinitions("OptionPreprocessor");
|
|
|
|
|
|
|
|
for (RecordVector::const_iterator B = OptionPreprocessors.begin(),
|
|
|
|
E = OptionPreprocessors.end(); B!=E; ++B) {
|
|
|
|
DagInit* Case = (*B)->getValueAsDag("preprocessor");
|
2009-10-19 21:24:28 +00:00
|
|
|
EmitCaseConstructHandler(Case, Indent1,
|
|
|
|
EmitPreprocessOptionsCallback(OptDecs),
|
2009-10-17 20:09:29 +00:00
|
|
|
false, OptDecs, O);
|
|
|
|
}
|
|
|
|
|
2010-07-23 03:42:55 +00:00
|
|
|
O << '\n';
|
|
|
|
O.indent(Indent1) << "return 0;\n";
|
2009-10-17 20:09:29 +00:00
|
|
|
O << "}\n\n";
|
|
|
|
}
|
|
|
|
|
2010-08-23 23:21:23 +00:00
|
|
|
class DoEmitPopulateLanguageMap;
|
|
|
|
typedef void (DoEmitPopulateLanguageMap::* DoEmitPopulateLanguageMapHandler)
|
|
|
|
(const DagInit& D);
|
|
|
|
|
|
|
|
class DoEmitPopulateLanguageMap
|
|
|
|
: public HandlerTable<DoEmitPopulateLanguageMapHandler>
|
2008-03-23 08:57:20 +00:00
|
|
|
{
|
2010-08-23 23:21:23 +00:00
|
|
|
private:
|
|
|
|
raw_ostream& O_;
|
2008-11-17 17:29:18 +00:00
|
|
|
|
2010-08-23 23:21:23 +00:00
|
|
|
public:
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2010-08-23 23:21:23 +00:00
|
|
|
explicit DoEmitPopulateLanguageMap (raw_ostream& O) : O_(O) {
|
|
|
|
if (!staticMembersInitialized_) {
|
|
|
|
AddHandler("lang_to_suffixes",
|
|
|
|
&DoEmitPopulateLanguageMap::onLangToSuffixes);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2010-08-23 23:21:23 +00:00
|
|
|
staticMembersInitialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2010-08-23 23:21:23 +00:00
|
|
|
void operator() (Init* I) {
|
|
|
|
InvokeDagInitHandler(this, I);
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2010-08-23 23:21:23 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
void onLangToSuffixes (const DagInit& d) {
|
|
|
|
CheckNumberOfArguments(d, 2);
|
|
|
|
|
|
|
|
const std::string& Lang = InitPtrToString(d.getArg(0));
|
|
|
|
Init* Suffixes = d.getArg(1);
|
|
|
|
|
|
|
|
// Second argument to lang_to_suffixes is either a single string...
|
|
|
|
if (typeid(*Suffixes) == typeid(StringInit)) {
|
|
|
|
O_.indent(Indent1) << "langMap[\"" << InitPtrToString(Suffixes)
|
|
|
|
<< "\"] = \"" << Lang << "\";\n";
|
|
|
|
}
|
|
|
|
// ...or a list of strings.
|
|
|
|
else {
|
|
|
|
const ListInit& Lst = InitPtrToList(Suffixes);
|
|
|
|
assert(Lst.size() != 0);
|
|
|
|
for (ListInit::const_iterator B = Lst.begin(), E = Lst.end();
|
|
|
|
B != E; ++B) {
|
|
|
|
O_.indent(Indent1) << "langMap[\"" << InitPtrToString(*B)
|
|
|
|
<< "\"] = \"" << Lang << "\";\n";
|
|
|
|
}
|
2008-11-17 17:29:18 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2010-08-23 23:21:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
|
|
|
|
void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
|
|
|
|
{
|
|
|
|
O << "int PopulateLanguageMap (LanguageMap& langMap) {\n";
|
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
// For each LanguageMap:
|
2010-08-23 23:21:23 +00:00
|
|
|
const RecordVector& LangMaps =
|
|
|
|
Records.getAllDerivedDefinitions("LanguageMap");
|
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
// Call DoEmitPopulateLanguageMap.
|
2010-08-23 23:21:23 +00:00
|
|
|
for (RecordVector::const_iterator B = LangMaps.begin(),
|
|
|
|
E = LangMaps.end(); B!=E; ++B) {
|
|
|
|
ListInit* LangMap = (*B)->getValueAsListInit("map");
|
|
|
|
std::for_each(LangMap->begin(), LangMap->end(),
|
|
|
|
DoEmitPopulateLanguageMap(O));
|
|
|
|
}
|
|
|
|
|
2010-07-23 03:42:55 +00:00
|
|
|
O << '\n';
|
|
|
|
O.indent(Indent1) << "return 0;\n";
|
2008-12-11 10:34:18 +00:00
|
|
|
O << "}\n\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2010-08-23 19:24:08 +00:00
|
|
|
/// EmitEdgePropertyHandlerCallback - Emits code that handles edge
|
|
|
|
/// properties. Helper function passed to EmitCaseConstructHandler() by
|
|
|
|
/// EmitEdgeClass().
|
|
|
|
void EmitEdgePropertyHandlerCallback (const Init* i, unsigned IndentLevel,
|
|
|
|
raw_ostream& O) {
|
2008-05-30 06:18:16 +00:00
|
|
|
const DagInit& d = InitPtrToDag(i);
|
2009-10-18 22:51:30 +00:00
|
|
|
const std::string& OpName = GetOperatorName(d);
|
2008-05-30 06:08:50 +00:00
|
|
|
|
2008-12-17 02:47:01 +00:00
|
|
|
if (OpName == "inc_weight") {
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(IndentLevel) << "ret += ";
|
2008-12-17 02:47:01 +00:00
|
|
|
}
|
|
|
|
else if (OpName == "error") {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2010-07-23 03:42:55 +00:00
|
|
|
O.indent(IndentLevel) << "PrintError(\""
|
2009-10-17 20:09:29 +00:00
|
|
|
<< InitPtrToString(d.getArg(0))
|
|
|
|
<< "\");\n";
|
2010-07-23 03:42:55 +00:00
|
|
|
O.indent(IndentLevel) << "return -1;\n";
|
2008-12-17 02:47:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-10-17 20:09:29 +00:00
|
|
|
else {
|
|
|
|
throw "Unknown operator in edge properties list: '" + OpName + "'!"
|
2008-12-18 04:06:58 +00:00
|
|
|
"\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
|
2009-10-17 20:09:29 +00:00
|
|
|
}
|
2008-05-30 06:08:50 +00:00
|
|
|
|
|
|
|
if (d.getNumArgs() > 0)
|
|
|
|
O << InitPtrToInt(d.getArg(0)) << ";\n";
|
|
|
|
else
|
|
|
|
O << "2;\n";
|
|
|
|
|
2008-05-06 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitEdgeClass - Emit a single Edge# class.
|
2008-05-30 06:10:19 +00:00
|
|
|
void EmitEdgeClass (unsigned N, const std::string& Target,
|
2010-08-24 01:10:22 +00:00
|
|
|
const DagInit& Case, const OptionDescriptions& OptDescs,
|
2009-07-03 00:10:29 +00:00
|
|
|
raw_ostream& O) {
|
2008-05-06 17:23:14 +00:00
|
|
|
|
|
|
|
// Class constructor.
|
|
|
|
O << "class Edge" << N << ": public Edge {\n"
|
2009-09-21 15:53:44 +00:00
|
|
|
<< "public:\n";
|
|
|
|
O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
|
|
|
|
<< "\") {}\n\n";
|
2008-05-06 17:23:14 +00:00
|
|
|
|
2008-05-06 18:14:24 +00:00
|
|
|
// Function Weight().
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent1)
|
2010-08-23 19:24:08 +00:00
|
|
|
<< "int Weight(const InputLanguagesSet& InLangs) const {\n";
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent2) << "unsigned ret = 0;\n";
|
2008-05-06 17:23:14 +00:00
|
|
|
|
2008-05-30 06:08:50 +00:00
|
|
|
// Handle the 'case' construct.
|
2010-08-24 01:10:22 +00:00
|
|
|
EmitCaseConstructHandler(&Case, Indent2, EmitEdgePropertyHandlerCallback,
|
2010-08-23 19:24:08 +00:00
|
|
|
false, OptDescs, O);
|
2008-05-06 18:14:24 +00:00
|
|
|
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent2) << "return ret;\n";
|
2009-12-24 17:49:28 +00:00
|
|
|
O.indent(Indent1) << "}\n\n};\n\n";
|
2008-05-06 17:23:14 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 06:12:24 +00:00
|
|
|
/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
|
2010-08-23 23:21:23 +00:00
|
|
|
void EmitEdgeClasses (const DagVector& EdgeVector,
|
2008-12-07 16:41:11 +00:00
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 00:10:29 +00:00
|
|
|
raw_ostream& O) {
|
2008-11-28 00:13:47 +00:00
|
|
|
int i = 0;
|
2010-08-23 23:21:23 +00:00
|
|
|
for (DagVector::const_iterator B = EdgeVector.begin(),
|
2008-12-07 16:44:47 +00:00
|
|
|
E = EdgeVector.end(); B != E; ++B) {
|
2010-08-24 01:10:22 +00:00
|
|
|
const DagInit& Edge = **B;
|
|
|
|
const std::string& Name = GetOperatorName(Edge);
|
2010-08-23 23:21:23 +00:00
|
|
|
|
2010-08-24 01:10:22 +00:00
|
|
|
if (Name == "optional_edge") {
|
|
|
|
assert(IsOptionalEdge(Edge));
|
|
|
|
const std::string& NodeB = InitPtrToString(Edge.getArg(1));
|
|
|
|
|
|
|
|
const DagInit& Weight = InitPtrToDag(Edge.getArg(2));
|
2010-08-23 23:21:23 +00:00
|
|
|
EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
|
|
|
|
}
|
2010-08-24 01:10:22 +00:00
|
|
|
else if (Name != "edge") {
|
|
|
|
throw "Unknown edge class: '" + Name + "'!";
|
|
|
|
}
|
2008-05-06 16:36:50 +00:00
|
|
|
|
2008-11-28 00:13:47 +00:00
|
|
|
++i;
|
2008-05-06 16:36:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-15 07:07:12 +00:00
|
|
|
/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() function.
|
2010-08-23 23:21:23 +00:00
|
|
|
void EmitPopulateCompilationGraph (const DagVector& EdgeVector,
|
2008-12-07 16:41:11 +00:00
|
|
|
const ToolDescriptions& ToolDescs,
|
2009-07-03 00:10:29 +00:00
|
|
|
raw_ostream& O)
|
2008-03-23 08:57:20 +00:00
|
|
|
{
|
2010-08-15 07:07:12 +00:00
|
|
|
O << "int PopulateCompilationGraph (CompilationGraph& G) {\n";
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
|
|
|
|
E = ToolDescs.end(); B != E; ++B)
|
2009-09-21 15:53:44 +00:00
|
|
|
O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
|
2008-11-12 00:05:17 +00:00
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
O << '\n';
|
|
|
|
|
2008-11-12 00:05:17 +00:00
|
|
|
// Insert edges.
|
|
|
|
|
2008-11-28 00:13:47 +00:00
|
|
|
int i = 0;
|
2010-08-23 23:21:23 +00:00
|
|
|
for (DagVector::const_iterator B = EdgeVector.begin(),
|
2008-12-07 16:44:47 +00:00
|
|
|
E = EdgeVector.end(); B != E; ++B) {
|
2010-08-24 01:10:22 +00:00
|
|
|
const DagInit& Edge = **B;
|
|
|
|
const std::string& NodeA = InitPtrToString(Edge.getArg(0));
|
|
|
|
const std::string& NodeB = InitPtrToString(Edge.getArg(1));
|
2008-05-06 16:36:50 +00:00
|
|
|
|
2010-07-23 03:42:55 +00:00
|
|
|
O.indent(Indent1) << "if (int ret = G.insertEdge(\"" << NodeA << "\", ";
|
2008-05-06 16:36:50 +00:00
|
|
|
|
2010-08-24 01:10:22 +00:00
|
|
|
if (IsOptionalEdge(Edge))
|
2008-05-06 16:36:50 +00:00
|
|
|
O << "new Edge" << i << "()";
|
2010-08-23 23:21:23 +00:00
|
|
|
else
|
|
|
|
O << "new SimpleEdge(\"" << NodeB << "\")";
|
2008-05-06 16:36:50 +00:00
|
|
|
|
2010-07-23 03:42:55 +00:00
|
|
|
O << "))\n";
|
|
|
|
O.indent(Indent2) << "return ret;\n";
|
|
|
|
|
2008-11-28 00:13:47 +00:00
|
|
|
++i;
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2010-07-23 03:42:55 +00:00
|
|
|
O << '\n';
|
|
|
|
O.indent(Indent1) << "return 0;\n";
|
2008-12-11 10:34:18 +00:00
|
|
|
O << "}\n\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2009-12-07 17:03:05 +00:00
|
|
|
/// HookInfo - Information about the hook type and number of arguments.
|
|
|
|
struct HookInfo {
|
|
|
|
|
|
|
|
// A hook can either have a single parameter of type std::vector<std::string>,
|
|
|
|
// or NumArgs parameters of type const char*.
|
|
|
|
enum HookType { ListHook, ArgHook };
|
|
|
|
|
|
|
|
HookType Type;
|
|
|
|
unsigned NumArgs;
|
|
|
|
|
|
|
|
HookInfo() : Type(ArgHook), NumArgs(1)
|
|
|
|
{}
|
|
|
|
|
|
|
|
HookInfo(HookType T) : Type(T), NumArgs(1)
|
|
|
|
{}
|
|
|
|
|
|
|
|
HookInfo(unsigned N) : Type(ArgHook), NumArgs(N)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef llvm::StringMap<HookInfo> HookInfoMap;
|
|
|
|
|
2008-05-30 06:12:24 +00:00
|
|
|
/// ExtractHookNames - Extract the hook names from all instances of
|
2009-12-07 17:03:05 +00:00
|
|
|
/// $CALL(HookName) in the provided command line string/action. Helper
|
2008-05-30 06:12:24 +00:00
|
|
|
/// function used by FillInHookNames().
|
2008-12-07 16:41:11 +00:00
|
|
|
class ExtractHookNames {
|
2009-12-07 17:03:05 +00:00
|
|
|
HookInfoMap& HookNames_;
|
|
|
|
const OptionDescriptions& OptDescs_;
|
2008-12-07 16:41:11 +00:00
|
|
|
public:
|
2009-12-07 17:03:05 +00:00
|
|
|
ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
|
|
|
|
: HookNames_(HookNames), OptDescs_(OptDescs)
|
|
|
|
{}
|
2008-12-07 16:41:11 +00:00
|
|
|
|
2009-12-07 17:03:05 +00:00
|
|
|
void onAction (const DagInit& Dag) {
|
2009-12-15 07:20:50 +00:00
|
|
|
const std::string& Name = GetOperatorName(Dag);
|
|
|
|
|
|
|
|
if (Name == "forward_transformed_value") {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(Dag, 2);
|
2009-12-07 17:03:05 +00:00
|
|
|
const std::string& OptName = InitPtrToString(Dag.getArg(0));
|
|
|
|
const std::string& HookName = InitPtrToString(Dag.getArg(1));
|
2010-07-19 17:17:10 +00:00
|
|
|
const OptionDescription& D =
|
|
|
|
OptDescs_.FindParameterListOrParameter(OptName);
|
2009-10-18 22:51:30 +00:00
|
|
|
|
2009-12-07 17:03:05 +00:00
|
|
|
HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
|
|
|
|
: HookInfo::ArgHook);
|
|
|
|
}
|
2009-12-15 07:20:50 +00:00
|
|
|
else if (Name == "append_cmd" || Name == "output_suffix") {
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckNumberOfArguments(Dag, 1);
|
2009-12-15 07:20:50 +00:00
|
|
|
this->onCmdLine(InitPtrToString(Dag.getArg(0)));
|
2009-12-07 17:03:05 +00:00
|
|
|
}
|
2009-12-15 07:20:50 +00:00
|
|
|
}
|
2009-10-18 22:51:30 +00:00
|
|
|
|
2009-12-15 07:20:50 +00:00
|
|
|
void onCmdLine(const std::string& Cmd) {
|
2009-12-07 17:03:05 +00:00
|
|
|
StrVector cmds;
|
2009-12-17 07:48:34 +00:00
|
|
|
TokenizeCmdLine(Cmd, cmds);
|
2009-12-15 07:20:50 +00:00
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
|
|
|
|
B != E; ++B) {
|
|
|
|
const std::string& cmd = *B;
|
2009-01-21 13:04:00 +00:00
|
|
|
|
|
|
|
if (cmd == "$CALL") {
|
|
|
|
unsigned NumArgs = 0;
|
2009-12-23 12:49:41 +00:00
|
|
|
CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
|
2009-01-21 13:04:00 +00:00
|
|
|
const std::string& HookName = *B;
|
|
|
|
|
|
|
|
if (HookName.at(0) == ')')
|
|
|
|
throw "$CALL invoked with no arguments!";
|
|
|
|
|
|
|
|
while (++B != E && B->at(0) != ')') {
|
|
|
|
++NumArgs;
|
|
|
|
}
|
|
|
|
|
2009-12-07 17:03:05 +00:00
|
|
|
HookInfoMap::const_iterator H = HookNames_.find(HookName);
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-07 17:03:05 +00:00
|
|
|
if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
|
|
|
|
H->second.Type != HookInfo::ArgHook)
|
2009-01-21 13:04:00 +00:00
|
|
|
throw "Overloading of hooks is not allowed. Overloaded hook: "
|
|
|
|
+ HookName;
|
|
|
|
else
|
2009-12-07 17:03:05 +00:00
|
|
|
HookNames_[HookName] = HookInfo(NumArgs);
|
2009-12-15 07:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(const Init* Arg) {
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-15 07:20:50 +00:00
|
|
|
// We're invoked on an action (either a dag or a dag list).
|
|
|
|
if (typeid(*Arg) == typeid(DagInit)) {
|
|
|
|
const DagInit& Dag = InitPtrToDag(Arg);
|
|
|
|
this->onAction(Dag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (typeid(*Arg) == typeid(ListInit)) {
|
|
|
|
const ListInit& List = InitPtrToList(Arg);
|
|
|
|
for (ListInit::const_iterator B = List.begin(), E = List.end(); B != E;
|
|
|
|
++B) {
|
|
|
|
const DagInit& Dag = InitPtrToDag(*B);
|
|
|
|
this->onAction(Dag);
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
2009-12-15 07:20:50 +00:00
|
|
|
return;
|
2008-05-30 06:12:24 +00:00
|
|
|
}
|
2009-12-15 07:20:50 +00:00
|
|
|
|
2010-10-23 07:32:46 +00:00
|
|
|
// We're invoked on a command line string.
|
2009-12-15 07:20:50 +00:00
|
|
|
this->onCmdLine(InitPtrToString(Arg));
|
2008-05-30 06:12:24 +00:00
|
|
|
}
|
2009-10-18 22:51:30 +00:00
|
|
|
|
|
|
|
void operator()(const Init* Statement, unsigned) {
|
|
|
|
this->operator()(Statement);
|
|
|
|
}
|
2008-12-07 16:41:11 +00:00
|
|
|
};
|
2008-05-30 06:22:15 +00:00
|
|
|
|
2008-05-30 06:12:24 +00:00
|
|
|
/// FillInHookNames - Actually extract the hook names from all command
|
|
|
|
/// line strings. Helper function used by EmitHookDeclarations().
|
2008-12-07 16:41:11 +00:00
|
|
|
void FillInHookNames(const ToolDescriptions& ToolDescs,
|
2009-12-07 17:03:05 +00:00
|
|
|
const OptionDescriptions& OptDescs,
|
|
|
|
HookInfoMap& HookNames)
|
2008-12-07 16:41:11 +00:00
|
|
|
{
|
2009-12-07 17:03:05 +00:00
|
|
|
// For all tool descriptions:
|
2008-12-07 16:41:11 +00:00
|
|
|
for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
|
|
|
|
E = ToolDescs.end(); B != E; ++B) {
|
|
|
|
const ToolDescription& D = *(*B);
|
2009-12-07 17:03:05 +00:00
|
|
|
|
|
|
|
// Look for 'forward_transformed_value' in 'actions'.
|
|
|
|
if (D.Actions)
|
|
|
|
WalkCase(D.Actions, Id(), ExtractHookNames(HookNames, OptDescs));
|
|
|
|
|
|
|
|
// Look for hook invocations in 'cmd_line'.
|
2008-12-07 16:41:11 +00:00
|
|
|
if (!D.CmdLine)
|
2008-05-30 06:12:24 +00:00
|
|
|
continue;
|
2008-12-07 16:41:11 +00:00
|
|
|
if (dynamic_cast<StringInit*>(D.CmdLine))
|
2008-05-30 06:12:24 +00:00
|
|
|
// This is a string.
|
2009-12-07 17:03:05 +00:00
|
|
|
ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
|
2008-05-30 06:22:15 +00:00
|
|
|
else
|
2008-05-30 06:12:24 +00:00
|
|
|
// This is a 'case' construct.
|
2009-12-07 17:03:05 +00:00
|
|
|
WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
|
2008-05-30 06:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitHookDeclarations - Parse CmdLine fields of all the tool
|
|
|
|
/// property records and emit hook function declaration for each
|
|
|
|
/// instance of $CALL(HookName).
|
2009-12-07 17:03:05 +00:00
|
|
|
void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
|
|
|
|
const OptionDescriptions& OptDescs, raw_ostream& O) {
|
|
|
|
HookInfoMap HookNames;
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-07 17:03:05 +00:00
|
|
|
FillInHookNames(ToolDescs, OptDescs, HookNames);
|
2008-05-30 06:12:24 +00:00
|
|
|
if (HookNames.empty())
|
|
|
|
return;
|
|
|
|
|
2009-12-07 17:03:05 +00:00
|
|
|
for (HookInfoMap::const_iterator B = HookNames.begin(),
|
2009-01-21 13:04:00 +00:00
|
|
|
E = HookNames.end(); B != E; ++B) {
|
2009-12-07 17:03:05 +00:00
|
|
|
const char* HookName = B->first();
|
|
|
|
const HookInfo& Info = B->second;
|
|
|
|
|
|
|
|
O.indent(Indent1) << "std::string " << HookName << "(";
|
2009-01-21 13:04:00 +00:00
|
|
|
|
2009-12-07 17:03:05 +00:00
|
|
|
if (Info.Type == HookInfo::ArgHook) {
|
|
|
|
for (unsigned i = 0, j = Info.NumArgs; i < j; ++i) {
|
|
|
|
O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
O << "const std::vector<std::string>& Arg";
|
2009-01-21 13:04:00 +00:00
|
|
|
}
|
2008-05-30 06:12:24 +00:00
|
|
|
|
2009-01-21 13:04:00 +00:00
|
|
|
O <<");\n";
|
|
|
|
}
|
2008-05-30 06:12:24 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-11-12 12:41:18 +00:00
|
|
|
/// EmitIncludes - Emit necessary #include directives and some
|
|
|
|
/// additional declarations.
|
2009-07-03 00:10:29 +00:00
|
|
|
void EmitIncludes(raw_ostream& O) {
|
2009-09-28 01:16:42 +00:00
|
|
|
O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
|
|
|
|
<< "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
|
2010-07-23 03:42:55 +00:00
|
|
|
<< "#include \"llvm/CompilerDriver/Error.h\"\n"
|
2008-09-22 20:50:40 +00:00
|
|
|
<< "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
|
2008-09-22 20:49:34 +00:00
|
|
|
|
2009-10-17 20:09:29 +00:00
|
|
|
<< "#include \"llvm/Support/CommandLine.h\"\n"
|
|
|
|
<< "#include \"llvm/Support/raw_ostream.h\"\n\n"
|
2008-09-22 20:49:34 +00:00
|
|
|
|
2009-12-07 17:03:05 +00:00
|
|
|
<< "#include <algorithm>\n"
|
2008-09-22 20:49:34 +00:00
|
|
|
<< "#include <cstdlib>\n"
|
2009-12-07 17:03:05 +00:00
|
|
|
<< "#include <iterator>\n"
|
2008-09-22 20:49:34 +00:00
|
|
|
<< "#include <stdexcept>\n\n"
|
|
|
|
|
|
|
|
<< "using namespace llvm;\n"
|
|
|
|
<< "using namespace llvmc;\n\n"
|
|
|
|
|
2008-11-12 12:41:18 +00:00
|
|
|
<< "inline const char* checkCString(const char* s)\n"
|
|
|
|
<< "{ return s == NULL ? \"\" : s; }\n\n";
|
2008-09-22 20:49:34 +00:00
|
|
|
}
|
|
|
|
|
2008-11-17 17:29:42 +00:00
|
|
|
|
2010-08-23 19:24:00 +00:00
|
|
|
/// DriverData - Holds all information about the driver.
|
|
|
|
struct DriverData {
|
2008-12-07 16:41:11 +00:00
|
|
|
OptionDescriptions OptDescs;
|
|
|
|
ToolDescriptions ToolDescs;
|
2010-08-23 23:21:23 +00:00
|
|
|
DagVector Edges;
|
2010-08-23 19:24:00 +00:00
|
|
|
bool HasSink;
|
2008-11-17 17:29:42 +00:00
|
|
|
};
|
|
|
|
|
2008-12-07 16:41:11 +00:00
|
|
|
/// HasSink - Go through the list of tool descriptions and check if
|
2008-12-07 16:42:47 +00:00
|
|
|
/// there are any with the 'sink' property set.
|
2008-12-07 16:41:11 +00:00
|
|
|
bool HasSink(const ToolDescriptions& ToolDescs) {
|
|
|
|
for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
|
|
|
|
E = ToolDescs.end(); B != E; ++B)
|
|
|
|
if ((*B)->isSink())
|
|
|
|
return true;
|
2008-11-28 00:13:47 +00:00
|
|
|
|
2008-12-07 16:42:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-23 19:24:00 +00:00
|
|
|
/// CollectDriverData - Collect compilation graph edges, tool properties and
|
2010-08-15 07:07:12 +00:00
|
|
|
/// option properties from the parse tree.
|
2010-08-23 19:24:00 +00:00
|
|
|
void CollectDriverData (const RecordKeeper& Records, DriverData& Data) {
|
2008-11-28 00:13:47 +00:00
|
|
|
// Collect option properties.
|
2008-11-17 17:30:25 +00:00
|
|
|
const RecordVector& OptionLists =
|
|
|
|
Records.getAllDerivedDefinitions("OptionList");
|
2010-08-23 23:21:23 +00:00
|
|
|
CollectOptionDescriptions(OptionLists, Data.OptDescs);
|
2008-12-07 16:41:11 +00:00
|
|
|
|
|
|
|
// Collect tool properties.
|
|
|
|
const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
|
2010-08-23 23:21:23 +00:00
|
|
|
CollectToolDescriptions(Tools, Data.ToolDescs);
|
2008-12-07 16:41:11 +00:00
|
|
|
Data.HasSink = HasSink(Data.ToolDescs);
|
2008-11-17 17:29:42 +00:00
|
|
|
|
2008-11-28 00:13:47 +00:00
|
|
|
// Collect compilation graph edges.
|
|
|
|
const RecordVector& CompilationGraphs =
|
|
|
|
Records.getAllDerivedDefinitions("CompilationGraph");
|
2010-08-23 23:21:23 +00:00
|
|
|
FillInEdgeVector(CompilationGraphs, Data.Edges);
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
|
|
|
|
2010-08-23 19:24:00 +00:00
|
|
|
/// CheckDriverData - Perform some sanity checks on the collected data.
|
|
|
|
void CheckDriverData(DriverData& Data) {
|
2008-11-28 00:13:47 +00:00
|
|
|
// Filter out all tools not mentioned in the compilation graph.
|
2008-12-07 16:41:11 +00:00
|
|
|
FilterNotInGraph(Data.Edges, Data.ToolDescs);
|
2008-11-17 17:29:42 +00:00
|
|
|
|
|
|
|
// Typecheck the compilation graph.
|
2008-12-07 16:41:11 +00:00
|
|
|
TypecheckGraph(Data.Edges, Data.ToolDescs);
|
2008-05-30 06:26:08 +00:00
|
|
|
|
2008-05-30 06:28:37 +00:00
|
|
|
// Check that there are no options without side effects (specified
|
|
|
|
// only in the OptionList).
|
2008-12-07 16:41:11 +00:00
|
|
|
CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
|
|
|
|
}
|
|
|
|
|
2010-12-13 00:23:57 +00:00
|
|
|
void EmitDriverCode(const DriverData& Data,
|
|
|
|
raw_ostream& O, RecordKeeper &Records) {
|
2008-12-07 16:41:11 +00:00
|
|
|
// Emit file header.
|
|
|
|
EmitIncludes(O);
|
2008-05-30 06:28:37 +00:00
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Emit global option registration code.
|
2010-08-20 11:24:44 +00:00
|
|
|
O << "namespace llvmc {\n"
|
|
|
|
<< "namespace autogenerated {\n\n";
|
2010-08-15 07:07:12 +00:00
|
|
|
EmitOptionDefinitions(Data.OptDescs, Data.HasSink, O);
|
2010-08-20 11:24:44 +00:00
|
|
|
O << "} // End namespace autogenerated.\n"
|
|
|
|
<< "} // End namespace llvmc.\n\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-30 06:12:24 +00:00
|
|
|
// Emit hook declarations.
|
2010-08-20 11:24:44 +00:00
|
|
|
O << "namespace hooks {\n";
|
2009-12-07 17:03:05 +00:00
|
|
|
EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
|
2010-08-20 11:24:44 +00:00
|
|
|
O << "} // End namespace hooks.\n\n";
|
2008-05-30 06:12:24 +00:00
|
|
|
|
2008-12-11 10:34:18 +00:00
|
|
|
O << "namespace {\n\n";
|
2010-08-20 11:24:44 +00:00
|
|
|
O << "using namespace llvmc::autogenerated;\n\n";
|
2008-12-11 10:34:18 +00:00
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Emit Tool classes.
|
2008-12-07 16:41:11 +00:00
|
|
|
for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
|
|
|
|
E = Data.ToolDescs.end(); B!=E; ++B)
|
|
|
|
EmitToolClassDefinition(*(*B), Data.OptDescs, O);
|
2008-05-06 16:36:06 +00:00
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Emit Edge# classes.
|
2008-12-07 16:41:11 +00:00
|
|
|
EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2009-06-23 20:46:48 +00:00
|
|
|
O << "} // End anonymous namespace.\n\n";
|
|
|
|
|
|
|
|
O << "namespace llvmc {\n";
|
2010-08-15 07:07:12 +00:00
|
|
|
O << "namespace autogenerated {\n\n";
|
|
|
|
|
|
|
|
// Emit PreprocessOptions() function.
|
|
|
|
EmitPreprocessOptions(Records, Data.OptDescs, O);
|
|
|
|
|
|
|
|
// Emit PopulateLanguageMap() function
|
|
|
|
// (language map maps from file extensions to language names).
|
|
|
|
EmitPopulateLanguageMap(Records, O);
|
|
|
|
|
|
|
|
// Emit PopulateCompilationGraph() function.
|
|
|
|
EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
|
|
|
|
|
|
|
|
O << "} // End namespace autogenerated.\n";
|
|
|
|
O << "} // End namespace llvmc.\n\n";
|
2009-06-23 20:46:48 +00:00
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
// EOF
|
2008-12-07 16:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// End of anonymous namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
/// run - The back-end entry point.
|
2009-07-03 00:10:29 +00:00
|
|
|
void LLVMCConfigurationEmitter::run (raw_ostream &O) {
|
2008-12-07 16:41:11 +00:00
|
|
|
try {
|
2010-08-23 19:24:00 +00:00
|
|
|
DriverData Data;
|
2008-12-07 16:41:11 +00:00
|
|
|
|
2010-08-23 19:24:00 +00:00
|
|
|
CollectDriverData(Records, Data);
|
|
|
|
CheckDriverData(Data);
|
2008-12-07 16:41:11 +00:00
|
|
|
|
2010-08-23 19:24:00 +00:00
|
|
|
this->EmitSourceFileHeader("llvmc-based driver: auto-generated code", O);
|
2010-12-13 00:23:57 +00:00
|
|
|
EmitDriverCode(Data, O, Records);
|
2008-12-07 16:41:11 +00:00
|
|
|
|
2008-05-30 06:21:48 +00:00
|
|
|
} catch (std::exception& Error) {
|
|
|
|
throw Error.what() + std::string(" - usually this means a syntax error.");
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|