2008-05-30 08:20:54 +02:00
|
|
|
//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
|
2008-03-23 09:57:20 +01: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 20:08:59 +02:00
|
|
|
// This tablegen backend is responsible for emitting LLVMC configuration code.
|
2008-03-23 09:57:20 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-06 20:09:29 +02:00
|
|
|
#include "LLVMCConfigurationEmitter.h"
|
2008-03-23 09:57:20 +01:00
|
|
|
#include "Record.h"
|
|
|
|
|
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2008-05-30 08:18:16 +02:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2009-12-23 13:49:30 +01:00
|
|
|
|
2008-03-23 09:57:20 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <functional>
|
2008-05-30 08:21:48 +02:00
|
|
|
#include <stdexcept>
|
2008-03-23 09:57:20 +01:00
|
|
|
#include <string>
|
2008-06-04 06:46:14 +02:00
|
|
|
#include <typeinfo>
|
2008-11-12 01:04:46 +01:00
|
|
|
|
2008-03-23 09:57:20 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-12-17 08:48:34 +01:00
|
|
|
namespace {
|
2008-03-23 09:57:20 +01:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Typedefs
|
|
|
|
|
|
|
|
typedef std::vector<Record*> RecordVector;
|
|
|
|
typedef std::vector<std::string> StrVector;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Constants
|
|
|
|
|
2009-09-21 17:53:44 +02:00
|
|
|
// Indentation.
|
2009-12-17 08:48:34 +01:00
|
|
|
const unsigned TabWidth = 4;
|
|
|
|
const unsigned Indent1 = TabWidth*1;
|
|
|
|
const unsigned Indent2 = TabWidth*2;
|
|
|
|
const unsigned Indent3 = TabWidth*3;
|
2010-02-23 10:04:44 +01:00
|
|
|
const unsigned Indent4 = TabWidth*4;
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-05-07 23:50:19 +02:00
|
|
|
// Default help string.
|
2009-12-17 08:48:34 +01:00
|
|
|
const char * const DefaultHelpString = "NO HELP MESSAGE PROVIDED";
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-05-07 23:50:19 +02:00
|
|
|
// Name for the "sink" option.
|
2009-12-17 08:48:34 +01:00
|
|
|
const char * const SinkOptionName = "AutoGeneratedSinkOption";
|
2008-03-23 09:57:20 +01:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Helper functions
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// Id - An 'identity' function object.
|
|
|
|
struct Id {
|
2009-10-19 00:51:30 +02: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 17:41:11 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-05-30 08:18:16 +02:00
|
|
|
int InitPtrToInt(const Init* ptr) {
|
|
|
|
const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
|
|
|
|
return val.getValue();
|
|
|
|
}
|
|
|
|
|
2008-05-30 08:10:19 +02:00
|
|
|
const std::string& InitPtrToString(const Init* ptr) {
|
|
|
|
const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
|
2008-03-23 09:57:20 +01:00
|
|
|
return val.getValue();
|
|
|
|
}
|
|
|
|
|
2008-05-30 08:18:16 +02:00
|
|
|
const ListInit& InitPtrToList(const Init* ptr) {
|
|
|
|
const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
|
|
|
|
return val;
|
2008-05-06 20:18:20 +02:00
|
|
|
}
|
|
|
|
|
2008-05-30 08:18:16 +02:00
|
|
|
const DagInit& InitPtrToDag(const Init* ptr) {
|
2008-05-30 08:10:19 +02:00
|
|
|
const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
|
2008-05-06 20:18:20 +02:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2009-10-19 00:51:30 +02:00
|
|
|
const std::string GetOperatorName(const DagInit& D) {
|
2009-12-17 08:48:49 +01:00
|
|
|
return D.getOperator()->getAsString();
|
2009-10-19 00:51:30 +02:00
|
|
|
}
|
|
|
|
|
2009-12-23 13:49:30 +01: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 13:49:41 +01:00
|
|
|
// CheckNumberOfArguments - Ensure that the number of args in d is
|
2009-07-07 18:07:36 +02:00
|
|
|
// greater than or equal to min_arguments, otherwise throw an exception.
|
2009-12-23 13:49:41 +01:00
|
|
|
void CheckNumberOfArguments (const DagInit& d, unsigned minArgs) {
|
2009-12-17 08:48:49 +01:00
|
|
|
if (d.getNumArgs() < minArgs)
|
|
|
|
throw GetOperatorName(d) + ": too few arguments!";
|
2009-12-07 18:03:05 +01:00
|
|
|
}
|
2008-05-06 19:22:03 +02:00
|
|
|
|
2009-12-23 13:49:41 +01:00
|
|
|
// IsDagEmpty - is this DAG marked with an empty marker?
|
|
|
|
bool IsDagEmpty (const DagInit& d) {
|
2009-10-19 00:51:30 +02:00
|
|
|
return GetOperatorName(d) == "empty_dag_marker";
|
2008-05-30 08:08:50 +02:00
|
|
|
}
|
2008-05-06 19:22:03 +02:00
|
|
|
|
2008-12-07 17:41:11 +01: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 15:55:04 +01:00
|
|
|
std::string EscapeVariableName (const std::string& Var) {
|
2008-12-07 17:41:11 +01: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 15:55:04 +01: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 13:49:41 +01:00
|
|
|
/// OneOf - Does the input string contain this character?
|
|
|
|
bool OneOf(const char* lst, char c) {
|
2009-01-21 14:04:00 +01:00
|
|
|
while (*lst) {
|
|
|
|
if (*lst++ == c)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-01-28 04:47:20 +01:00
|
|
|
template <class I, class S>
|
2009-12-23 13:49:41 +01:00
|
|
|
void CheckedIncrement(I& P, I E, S ErrorString) {
|
2009-01-28 04:47:20 +01:00
|
|
|
++P;
|
|
|
|
if (P == E)
|
|
|
|
throw ErrorString;
|
|
|
|
}
|
|
|
|
|
2009-10-21 04:13:13 +02:00
|
|
|
// apply is needed because C++'s syntax doesn't let us construct a function
|
|
|
|
// object and call it in the same statement.
|
|
|
|
template<typename F, typename T0>
|
|
|
|
void apply(F Fun, T0& Arg0) {
|
|
|
|
return Fun(Arg0);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename F, typename T0, typename T1>
|
|
|
|
void apply(F Fun, T0& Arg0, T1& Arg1) {
|
|
|
|
return Fun(Arg0, Arg1);
|
|
|
|
}
|
|
|
|
|
2008-03-23 09:57:20 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Back-end specific code
|
|
|
|
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// OptionType - One of six different option types. See the
|
|
|
|
/// documentation for detailed description of differences.
|
2008-03-23 09:57:20 +01:00
|
|
|
namespace OptionType {
|
2009-07-07 18:08:11 +02:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
enum OptionType { Alias, Switch, Parameter, ParameterList,
|
2008-12-07 17:47:12 +01:00
|
|
|
Prefix, PrefixList};
|
2008-12-07 17:41:11 +01:00
|
|
|
|
2009-10-21 04:13:13 +02:00
|
|
|
bool IsAlias(OptionType t) {
|
|
|
|
return (t == Alias);
|
|
|
|
}
|
|
|
|
|
2009-07-07 18:08:11 +02:00
|
|
|
bool IsList (OptionType t) {
|
|
|
|
return (t == ParameterList || t == PrefixList);
|
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2009-07-07 18:08:11 +02:00
|
|
|
bool IsSwitch (OptionType t) {
|
|
|
|
return (t == Switch);
|
|
|
|
}
|
2008-12-07 17:41:11 +01:00
|
|
|
|
2009-07-07 18:08:11 +02:00
|
|
|
bool IsParameter (OptionType t) {
|
|
|
|
return (t == Parameter || t == Prefix);
|
|
|
|
}
|
2008-12-07 17:41:11 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionType::OptionType stringToOptionType(const std::string& T) {
|
|
|
|
if (T == "alias_option")
|
|
|
|
return OptionType::Alias;
|
|
|
|
else if (T == "switch_option")
|
|
|
|
return OptionType::Switch;
|
|
|
|
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 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
namespace OptionDescriptionFlags {
|
|
|
|
enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
|
2009-01-28 04:47:20 +01:00
|
|
|
ReallyHidden = 0x4, Extern = 0x8,
|
2009-12-15 04:04:52 +01:00
|
|
|
OneOrMore = 0x10, Optional = 0x20,
|
2010-03-05 05:46:39 +01:00
|
|
|
CommaSeparated = 0x40, ForwardNotSplit = 0x80,
|
|
|
|
ZeroOrMore = 0x100 };
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// OptionDescription - Represents data contained in a single
|
|
|
|
/// OptionList entry.
|
2008-03-23 09:57:20 +01:00
|
|
|
struct OptionDescription {
|
|
|
|
OptionType::OptionType Type;
|
|
|
|
std::string Name;
|
2008-12-07 17:41:11 +01:00
|
|
|
unsigned Flags;
|
|
|
|
std::string Help;
|
2009-01-28 04:47:20 +01:00
|
|
|
unsigned MultiVal;
|
2009-07-07 18:08:41 +02:00
|
|
|
Init* InitVal;
|
2008-03-23 09:57:20 +01:00
|
|
|
|
|
|
|
OptionDescription(OptionType::OptionType t = OptionType::Switch,
|
2008-12-07 17:41:11 +01:00
|
|
|
const std::string& n = "",
|
|
|
|
const std::string& h = DefaultHelpString)
|
2009-07-07 18:08:41 +02:00
|
|
|
: Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
|
2008-03-23 09:57:20 +01:00
|
|
|
{}
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// GenTypeDeclaration - Returns the C++ variable type of this
|
|
|
|
/// option.
|
|
|
|
const char* GenTypeDeclaration() const;
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// GenVariableName - Returns the variable name used in the
|
|
|
|
/// generated C++ code.
|
|
|
|
std::string GenVariableName() const;
|
2008-05-12 18:33:06 +02:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// Merge - Merge two option descriptions.
|
|
|
|
void Merge (const OptionDescription& other);
|
|
|
|
|
2010-03-05 05:46:39 +01:00
|
|
|
/// CheckConsistency - Check that the flags are consistent.
|
|
|
|
void CheckConsistency() const;
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
// Misc convenient getters/setters.
|
|
|
|
|
|
|
|
bool isAlias() const;
|
2008-12-07 17:47:12 +01:00
|
|
|
|
2009-01-28 04:47:20 +01:00
|
|
|
bool isMultiVal() const;
|
|
|
|
|
2009-12-07 19:25:54 +01:00
|
|
|
bool isCommaSeparated() const;
|
|
|
|
void setCommaSeparated();
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
bool isExtern() const;
|
2008-12-07 17:47:12 +01:00
|
|
|
void setExtern();
|
2008-12-07 17:41:11 +01:00
|
|
|
|
2010-02-23 10:04:13 +01:00
|
|
|
bool isForwardNotSplit() const;
|
|
|
|
void setForwardNotSplit();
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
bool isRequired() const;
|
|
|
|
void setRequired();
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2009-01-28 04:47:20 +01:00
|
|
|
bool isOneOrMore() const;
|
|
|
|
void setOneOrMore();
|
|
|
|
|
2010-03-05 05:46:39 +01:00
|
|
|
bool isZeroOrMore() const;
|
|
|
|
void setZeroOrMore();
|
|
|
|
|
2009-12-15 04:04:52 +01:00
|
|
|
bool isOptional() const;
|
|
|
|
void setOptional();
|
2009-01-28 04:47:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
bool isHidden() const;
|
|
|
|
void setHidden();
|
|
|
|
|
|
|
|
bool isReallyHidden() const;
|
|
|
|
void setReallyHidden();
|
2009-01-28 04:47:20 +01:00
|
|
|
|
2009-07-07 18:08:11 +02:00
|
|
|
bool isSwitch() const
|
|
|
|
{ return OptionType::IsSwitch(this->Type); }
|
|
|
|
|
2009-10-21 04:13:13 +02:00
|
|
|
bool isParameter() const
|
|
|
|
{ return OptionType::IsParameter(this->Type); }
|
|
|
|
|
2009-07-07 18:08:11 +02:00
|
|
|
bool isList() const
|
|
|
|
{ return OptionType::IsList(this->Type); }
|
|
|
|
|
2008-03-23 09:57:20 +01:00
|
|
|
};
|
|
|
|
|
2010-03-05 05:46:39 +01: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 17:41:11 +01:00
|
|
|
void OptionDescription::Merge (const OptionDescription& other)
|
|
|
|
{
|
|
|
|
if (other.Type != Type)
|
|
|
|
throw "Conflicting definitions for the option " + Name + "!";
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
if (Help == other.Help || Help == DefaultHelpString)
|
|
|
|
Help = other.Help;
|
|
|
|
else if (other.Help != DefaultHelpString) {
|
2009-07-03 02:10:29 +02:00
|
|
|
llvm::errs() << "Warning: several different help strings"
|
2008-12-07 17:41:11 +01:00
|
|
|
" defined for option " + Name + "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
Flags |= other.Flags;
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
bool OptionDescription::isAlias() const {
|
2009-10-21 04:13:13 +02:00
|
|
|
return OptionType::IsAlias(this->Type);
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2009-01-28 04:47:20 +01:00
|
|
|
bool OptionDescription::isMultiVal() const {
|
2009-01-28 04:47:58 +01:00
|
|
|
return MultiVal > 1;
|
2009-01-28 04:47:20 +01:00
|
|
|
}
|
|
|
|
|
2009-12-07 19:25:54 +01:00
|
|
|
bool OptionDescription::isCommaSeparated() const {
|
|
|
|
return Flags & OptionDescriptionFlags::CommaSeparated;
|
|
|
|
}
|
|
|
|
void OptionDescription::setCommaSeparated() {
|
|
|
|
Flags |= OptionDescriptionFlags::CommaSeparated;
|
|
|
|
}
|
|
|
|
|
2010-02-23 10:04:13 +01:00
|
|
|
bool OptionDescription::isForwardNotSplit() const {
|
|
|
|
return Flags & OptionDescriptionFlags::ForwardNotSplit;
|
|
|
|
}
|
|
|
|
void OptionDescription::setForwardNotSplit() {
|
|
|
|
Flags |= OptionDescriptionFlags::ForwardNotSplit;
|
|
|
|
}
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
bool OptionDescription::isExtern() const {
|
2008-12-07 17:47:12 +01:00
|
|
|
return Flags & OptionDescriptionFlags::Extern;
|
|
|
|
}
|
|
|
|
void OptionDescription::setExtern() {
|
|
|
|
Flags |= OptionDescriptionFlags::Extern;
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
bool OptionDescription::isRequired() const {
|
|
|
|
return Flags & OptionDescriptionFlags::Required;
|
|
|
|
}
|
|
|
|
void OptionDescription::setRequired() {
|
|
|
|
Flags |= OptionDescriptionFlags::Required;
|
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2009-01-28 04:47:20 +01:00
|
|
|
bool OptionDescription::isOneOrMore() const {
|
|
|
|
return Flags & OptionDescriptionFlags::OneOrMore;
|
|
|
|
}
|
|
|
|
void OptionDescription::setOneOrMore() {
|
|
|
|
Flags |= OptionDescriptionFlags::OneOrMore;
|
|
|
|
}
|
|
|
|
|
2010-03-05 05:46:39 +01:00
|
|
|
bool OptionDescription::isZeroOrMore() const {
|
|
|
|
return Flags & OptionDescriptionFlags::ZeroOrMore;
|
|
|
|
}
|
|
|
|
void OptionDescription::setZeroOrMore() {
|
|
|
|
Flags |= OptionDescriptionFlags::ZeroOrMore;
|
|
|
|
}
|
|
|
|
|
2009-12-15 04:04:52 +01:00
|
|
|
bool OptionDescription::isOptional() const {
|
|
|
|
return Flags & OptionDescriptionFlags::Optional;
|
2009-01-28 04:47:20 +01:00
|
|
|
}
|
2009-12-15 04:04:52 +01:00
|
|
|
void OptionDescription::setOptional() {
|
|
|
|
Flags |= OptionDescriptionFlags::Optional;
|
2009-01-28 04:47:20 +01:00
|
|
|
}
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
bool OptionDescription::isHidden() const {
|
|
|
|
return Flags & OptionDescriptionFlags::Hidden;
|
|
|
|
}
|
|
|
|
void OptionDescription::setHidden() {
|
|
|
|
Flags |= OptionDescriptionFlags::Hidden;
|
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
bool OptionDescription::isReallyHidden() const {
|
|
|
|
return Flags & OptionDescriptionFlags::ReallyHidden;
|
|
|
|
}
|
|
|
|
void OptionDescription::setReallyHidden() {
|
|
|
|
Flags |= OptionDescriptionFlags::ReallyHidden;
|
|
|
|
}
|
2008-11-28 01:13:25 +01:00
|
|
|
|
2008-12-07 17:41:11 +01: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>";
|
|
|
|
case OptionType::Parameter:
|
|
|
|
case OptionType::Prefix:
|
|
|
|
default:
|
|
|
|
return "cl::opt<std::string>";
|
2008-11-28 01:13:25 +01:00
|
|
|
}
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
2008-11-28 01:13:25 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
std::string OptionDescription::GenVariableName() const {
|
|
|
|
const std::string& EscapedName = EscapeVariableName(Name);
|
|
|
|
switch (Type) {
|
|
|
|
case OptionType::Alias:
|
|
|
|
return "AutoGeneratedAlias_" + EscapedName;
|
|
|
|
case OptionType::PrefixList:
|
|
|
|
case OptionType::ParameterList:
|
|
|
|
return "AutoGeneratedList_" + EscapedName;
|
|
|
|
case OptionType::Switch:
|
|
|
|
return "AutoGeneratedSwitch_" + EscapedName;
|
|
|
|
case OptionType::Prefix:
|
|
|
|
case OptionType::Parameter:
|
|
|
|
default:
|
|
|
|
return "AutoGeneratedParameter_" + EscapedName;
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// OptionDescriptions - An OptionDescription array plus some helper
|
|
|
|
/// functions.
|
|
|
|
class OptionDescriptions {
|
|
|
|
typedef StringMap<OptionDescription> container_type;
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// Descriptions - A list of OptionDescriptions.
|
2008-03-23 09:57:20 +01:00
|
|
|
container_type Descriptions;
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
public:
|
2008-05-30 08:28:37 +02:00
|
|
|
/// FindOption - exception-throwing wrapper for find().
|
2008-12-07 17:41:11 +01:00
|
|
|
const OptionDescription& FindOption(const std::string& OptName) const;
|
2009-10-21 04:13:13 +02:00
|
|
|
|
|
|
|
// Wrappers for FindOption that throw an exception in case the option has a
|
|
|
|
// wrong type.
|
2009-10-17 22:09:29 +02:00
|
|
|
const OptionDescription& FindSwitch(const std::string& OptName) const;
|
2009-10-21 04:13:13 +02:00
|
|
|
const OptionDescription& FindParameter(const std::string& OptName) const;
|
|
|
|
const OptionDescription& FindList(const std::string& OptName) const;
|
|
|
|
const OptionDescription&
|
|
|
|
FindListOrParameter(const std::string& OptName) const;
|
2008-05-06 19:22:03 +02:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// insertDescription - Insert new OptionDescription into
|
|
|
|
/// OptionDescriptions list
|
|
|
|
void InsertDescription (const OptionDescription& o);
|
2008-05-30 08:27:02 +02:00
|
|
|
|
2008-03-23 09:57:20 +01:00
|
|
|
// Support for STL-style iteration
|
2008-12-07 17:41:11 +01:00
|
|
|
typedef container_type::const_iterator const_iterator;
|
2008-03-23 09:57:20 +01:00
|
|
|
const_iterator begin() const { return Descriptions.begin(); }
|
|
|
|
const_iterator end() const { return Descriptions.end(); }
|
|
|
|
};
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
const OptionDescription&
|
2009-10-21 04:13:13 +02:00
|
|
|
OptionDescriptions::FindOption(const std::string& OptName) const {
|
2008-12-07 17:41:11 +01:00
|
|
|
const_iterator I = Descriptions.find(OptName);
|
|
|
|
if (I != Descriptions.end())
|
|
|
|
return I->second;
|
|
|
|
else
|
|
|
|
throw OptName + ": no such option!";
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2009-10-17 22:09:29 +02:00
|
|
|
const OptionDescription&
|
2009-10-21 04:13:13 +02:00
|
|
|
OptionDescriptions::FindSwitch(const std::string& OptName) const {
|
2009-10-17 22:09:29 +02:00
|
|
|
const OptionDescription& OptDesc = this->FindOption(OptName);
|
|
|
|
if (!OptDesc.isSwitch())
|
|
|
|
throw OptName + ": incorrect option type - should be a switch!";
|
|
|
|
return OptDesc;
|
|
|
|
}
|
|
|
|
|
2009-10-21 04:13:13 +02:00
|
|
|
const OptionDescription&
|
|
|
|
OptionDescriptions::FindList(const std::string& OptName) const {
|
|
|
|
const OptionDescription& OptDesc = this->FindOption(OptName);
|
|
|
|
if (!OptDesc.isList())
|
|
|
|
throw OptName + ": incorrect option type - should be a list!";
|
|
|
|
return OptDesc;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionDescriptions::InsertDescription (const OptionDescription& o) {
|
2008-12-07 17:41:11 +01:00
|
|
|
container_type::iterator I = Descriptions.find(o.Name);
|
|
|
|
if (I != Descriptions.end()) {
|
|
|
|
OptionDescription& D = I->second;
|
|
|
|
D.Merge(o);
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
2008-12-07 17:41:11 +01:00
|
|
|
else {
|
|
|
|
Descriptions[o.Name] = o;
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// HandlerTable - A base class for function objects implemented as
|
|
|
|
/// 'tables of handlers'.
|
2009-12-07 11:51:55 +01:00
|
|
|
template <typename Handler>
|
2008-12-07 17:41:11 +01:00
|
|
|
class HandlerTable {
|
|
|
|
protected:
|
|
|
|
// Implementation details.
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// HandlerMap - A map from property names to property handlers
|
|
|
|
typedef StringMap<Handler> HandlerMap;
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
static HandlerMap Handlers_;
|
|
|
|
static bool staticMembersInitialized_;
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
public:
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2009-12-07 11:51:55 +01:00
|
|
|
Handler GetHandler (const std::string& HandlerName) const {
|
|
|
|
typename HandlerMap::iterator method = Handlers_.find(HandlerName);
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
if (method != Handlers_.end()) {
|
|
|
|
Handler h = method->second;
|
2009-12-07 11:51:55 +01:00
|
|
|
return h;
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
|
|
|
else {
|
2009-12-07 11:51:55 +01:00
|
|
|
throw "No handler found for property " + HandlerName + "!";
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2009-12-07 11:51:55 +01:00
|
|
|
void AddHandler(const char* Property, Handler H) {
|
|
|
|
Handlers_[Property] = H;
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
2009-12-07 11:51:55 +01:00
|
|
|
|
2008-03-23 09:57:20 +01:00
|
|
|
};
|
|
|
|
|
2009-12-17 08:48:49 +01: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 11:51:55 +01:00
|
|
|
template <class FunctionObject>
|
2009-12-17 08:48:49 +01:00
|
|
|
void InvokeDagInitHandler(FunctionObject* Obj, Init* I) {
|
|
|
|
typedef void (FunctionObject::*Handler) (const DagInit&);
|
2009-12-07 11:51:55 +01:00
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
const DagInit& Dag = InitPtrToDag(I);
|
|
|
|
Handler h = GetHandler<Handler>(Obj, Dag);
|
2009-12-07 11:51:55 +01:00
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
((Obj)->*(h))(Dag);
|
2009-12-07 11:51:55 +01:00
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01: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 11:51:55 +01:00
|
|
|
template <typename H>
|
|
|
|
typename HandlerTable<H>::HandlerMap HandlerTable<H>::Handlers_;
|
|
|
|
|
|
|
|
template <typename H>
|
|
|
|
bool HandlerTable<H>::staticMembersInitialized_ = false;
|
2008-03-23 09:57:20 +01:00
|
|
|
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// CollectOptionProperties - Function object for iterating over an
|
|
|
|
/// option property list.
|
2009-12-07 11:51:55 +01:00
|
|
|
class CollectOptionProperties;
|
|
|
|
typedef void (CollectOptionProperties::* CollectOptionPropertiesHandler)
|
2009-12-17 08:48:49 +01:00
|
|
|
(const DagInit&);
|
2009-12-07 11:51:55 +01:00
|
|
|
|
|
|
|
class CollectOptionProperties
|
|
|
|
: public HandlerTable<CollectOptionPropertiesHandler>
|
|
|
|
{
|
2008-05-30 08:27:02 +02:00
|
|
|
private:
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// optDescs_ - OptionDescriptions table. This is where the
|
|
|
|
/// information is stored.
|
|
|
|
OptionDescription& optDesc_;
|
2008-05-30 08:27:02 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
explicit CollectOptionProperties(OptionDescription& OD)
|
2009-12-07 11:51:55 +01:00
|
|
|
: optDesc_(OD)
|
2008-05-30 08:27:02 +02:00
|
|
|
{
|
|
|
|
if (!staticMembersInitialized_) {
|
2008-12-07 17:47:12 +01:00
|
|
|
AddHandler("extern", &CollectOptionProperties::onExtern);
|
2008-12-07 17:41:11 +01:00
|
|
|
AddHandler("help", &CollectOptionProperties::onHelp);
|
|
|
|
AddHandler("hidden", &CollectOptionProperties::onHidden);
|
2009-07-07 18:08:41 +02:00
|
|
|
AddHandler("init", &CollectOptionProperties::onInit);
|
2009-01-28 04:47:20 +01:00
|
|
|
AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
|
|
|
|
AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
|
2010-03-05 05:46:39 +01:00
|
|
|
AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
|
2008-12-07 17:41:11 +01:00
|
|
|
AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
|
|
|
|
AddHandler("required", &CollectOptionProperties::onRequired);
|
2009-12-15 04:04:52 +01:00
|
|
|
AddHandler("optional", &CollectOptionProperties::onOptional);
|
2009-12-07 19:25:54 +01:00
|
|
|
AddHandler("comma_separated", &CollectOptionProperties::onCommaSeparated);
|
2010-02-23 10:04:13 +01:00
|
|
|
AddHandler("forward_not_split",
|
|
|
|
&CollectOptionProperties::onForwardNotSplit);
|
2008-05-30 08:27:02 +02:00
|
|
|
|
|
|
|
staticMembersInitialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-07 11:51:55 +01:00
|
|
|
/// operator() - Just forwards to the corresponding property
|
|
|
|
/// handler.
|
2009-12-17 08:48:49 +01:00
|
|
|
void operator() (Init* I) {
|
|
|
|
InvokeDagInitHandler(this, I);
|
2009-12-07 11:51:55 +01:00
|
|
|
}
|
|
|
|
|
2008-05-30 08:27:02 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
/// Option property handlers --
|
2008-12-07 17:41:11 +01:00
|
|
|
/// Methods that handle option properties such as (help) or (hidden).
|
2008-09-22 22:46:19 +02:00
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onExtern (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2008-12-07 17:47:12 +01:00
|
|
|
optDesc_.setExtern();
|
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onHelp (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2010-01-26 15:55:04 +01:00
|
|
|
optDesc_.Help = EscapeQuotes(InitPtrToString(d.getArg(0)));
|
2008-05-30 08:27:02 +02:00
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onHidden (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2008-11-28 01:13:25 +01:00
|
|
|
optDesc_.setHidden();
|
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onReallyHidden (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2008-11-28 01:13:25 +01:00
|
|
|
optDesc_.setReallyHidden();
|
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onCommaSeparated (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2009-12-07 19:25:54 +01:00
|
|
|
if (!optDesc_.isList())
|
|
|
|
throw "'comma_separated' is valid only on list options!";
|
|
|
|
optDesc_.setCommaSeparated();
|
|
|
|
}
|
|
|
|
|
2010-02-23 10:04:13 +01: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 08:48:49 +01:00
|
|
|
void onRequired (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2010-03-05 05:46:39 +01:00
|
|
|
|
2008-05-30 08:27:02 +02:00
|
|
|
optDesc_.setRequired();
|
2010-03-05 05:46:39 +01:00
|
|
|
optDesc_.CheckConsistency();
|
2008-05-30 08:27:02 +02:00
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onInit (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 08:48:49 +01:00
|
|
|
Init* i = d.getArg(0);
|
2009-07-07 18:08:41 +02: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 19:25:54 +01:00
|
|
|
throw "Incorrect usage of the 'init' option property!";
|
2009-07-07 18:08:41 +02:00
|
|
|
|
|
|
|
optDesc_.InitVal = i;
|
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onOneOrMore (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2010-03-05 05:46:39 +01:00
|
|
|
|
2009-01-28 04:47:20 +01:00
|
|
|
optDesc_.setOneOrMore();
|
2010-03-05 05:46:39 +01:00
|
|
|
optDesc_.CheckConsistency();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onZeroOrMore (const DagInit& d) {
|
|
|
|
CheckNumberOfArguments(d, 0);
|
|
|
|
|
|
|
|
if (OptionType::IsList(optDesc_.Type))
|
|
|
|
llvm::errs() << "Warning: specifying the 'zero_or_more' property "
|
|
|
|
"on a list option has no effect.\n";
|
|
|
|
|
|
|
|
optDesc_.setZeroOrMore();
|
|
|
|
optDesc_.CheckConsistency();
|
2009-01-28 04:47:20 +01:00
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onOptional (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2010-03-05 05:46:39 +01:00
|
|
|
|
2009-01-28 04:47:20 +01:00
|
|
|
if (!OptionType::IsList(optDesc_.Type))
|
2009-12-15 04:04:52 +01:00
|
|
|
llvm::errs() << "Warning: specifying the 'optional' property"
|
2010-03-05 05:46:39 +01:00
|
|
|
"on a non-list option has no effect.\n";
|
|
|
|
|
2009-12-15 04:04:52 +01:00
|
|
|
optDesc_.setOptional();
|
2010-03-05 05:46:39 +01:00
|
|
|
optDesc_.CheckConsistency();
|
2009-01-28 04:47:20 +01:00
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onMultiVal (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 08:48:49 +01:00
|
|
|
int val = InitPtrToInt(d.getArg(0));
|
2009-01-28 04:47:20 +01:00
|
|
|
if (val < 2)
|
2009-12-07 19:25:54 +01:00
|
|
|
throw "Error in the 'multi_val' property: "
|
|
|
|
"the value must be greater than 1!";
|
2009-01-28 04:47:20 +01:00
|
|
|
if (!OptionType::IsList(optDesc_.Type))
|
2009-12-07 19:25:54 +01:00
|
|
|
throw "The multi_val property is valid only on list options!";
|
2009-01-28 04:47:20 +01:00
|
|
|
optDesc_.MultiVal = val;
|
|
|
|
}
|
|
|
|
|
2008-05-30 08:27:02 +02:00
|
|
|
};
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// AddOption - A function object that is applied to every option
|
|
|
|
/// description. Used by CollectOptionDescriptions.
|
2008-05-30 08:27:29 +02:00
|
|
|
class AddOption {
|
|
|
|
private:
|
2008-12-07 17:41:11 +01:00
|
|
|
OptionDescriptions& OptDescs_;
|
2008-05-30 08:27:29 +02:00
|
|
|
|
|
|
|
public:
|
2008-12-07 17:41:11 +01:00
|
|
|
explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
|
2008-05-30 08:27:29 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
void operator()(const Init* i) {
|
|
|
|
const DagInit& d = InitPtrToDag(i);
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2008-05-30 08:27:29 +02:00
|
|
|
|
|
|
|
const OptionType::OptionType Type =
|
2009-10-19 00:51:30 +02:00
|
|
|
stringToOptionType(GetOperatorName(d));
|
2008-05-30 08:27:29 +02:00
|
|
|
const std::string& Name = InitPtrToString(d.getArg(0));
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
OptionDescription OD(Type, Name);
|
|
|
|
|
|
|
|
if (!OD.isExtern())
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 2);
|
2008-12-07 17:41:11 +01:00
|
|
|
|
|
|
|
if (OD.isAlias()) {
|
|
|
|
// Aliases store the aliased option name in the 'Help' field.
|
2008-05-30 08:27:29 +02:00
|
|
|
OD.Help = InitPtrToString(d.getArg(1));
|
|
|
|
}
|
2008-12-07 17:41:11 +01:00
|
|
|
else if (!OD.isExtern()) {
|
2009-12-17 08:48:49 +01:00
|
|
|
processOptionProperties(d, OD);
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
|
|
|
OptDescs_.InsertDescription(OD);
|
2008-05-30 08:27:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2008-12-07 17:41:11 +01:00
|
|
|
/// processOptionProperties - Go through the list of option
|
|
|
|
/// properties and call a corresponding handler for each.
|
2009-12-17 08:48:49 +01:00
|
|
|
static void processOptionProperties (const DagInit& d, OptionDescription& o) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 2);
|
2009-12-17 08:48:49 +01:00
|
|
|
DagInit::const_arg_iterator B = d.arg_begin();
|
2008-12-07 17:41:11 +01:00
|
|
|
// Skip the first argument: it's always the option name.
|
|
|
|
++B;
|
2009-12-17 08:48:49 +01:00
|
|
|
std::for_each(B, d.arg_end(), CollectOptionProperties(o));
|
2008-05-30 08:27:29 +02:00
|
|
|
}
|
2008-12-07 17:41:11 +01:00
|
|
|
|
2008-05-30 08:27:29 +02:00
|
|
|
};
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// CollectOptionDescriptions - Collects option properties from all
|
|
|
|
/// OptionLists.
|
|
|
|
void CollectOptionDescriptions (RecordVector::const_iterator B,
|
|
|
|
RecordVector::const_iterator E,
|
|
|
|
OptionDescriptions& OptDescs)
|
|
|
|
{
|
|
|
|
// For every OptionList:
|
|
|
|
for (; B!=E; ++B) {
|
|
|
|
RecordVector::value_type T = *B;
|
|
|
|
// Throws an exception if the value does not exist.
|
|
|
|
ListInit* PropList = T->getValueAsListInit("options");
|
2008-05-30 08:27:29 +02:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
// For every option description in this list:
|
|
|
|
// collect the information and
|
|
|
|
std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
|
|
|
|
}
|
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
// Tool information record
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
namespace ToolFlags {
|
|
|
|
enum ToolFlags { Join = 0x1, Sink = 0x2 };
|
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
struct ToolDescription : public RefCountedBase<ToolDescription> {
|
|
|
|
std::string Name;
|
|
|
|
Init* CmdLine;
|
|
|
|
Init* Actions;
|
|
|
|
StrVector InLanguage;
|
2010-02-23 10:04:44 +01:00
|
|
|
std::string InFileOption;
|
|
|
|
std::string OutFileOption;
|
2008-12-07 17:41:11 +01:00
|
|
|
std::string OutLanguage;
|
|
|
|
std::string OutputSuffix;
|
|
|
|
unsigned Flags;
|
2010-02-23 10:04:28 +01:00
|
|
|
const Init* OnEmpty;
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01: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 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
// Default ctor here is needed because StringMap can only store
|
|
|
|
// DefaultConstructible objects
|
2010-02-23 10:04:44 +01:00
|
|
|
ToolDescription ()
|
2010-02-23 10:05:01 +01:00
|
|
|
: CmdLine(0), Actions(0), OutFileOption("-o"),
|
2010-02-23 10:04:44 +01:00
|
|
|
Flags(0), OnEmpty(0)
|
|
|
|
{}
|
2008-12-07 17:41:11 +01:00
|
|
|
ToolDescription (const std::string& n)
|
2010-02-23 10:04:44 +01:00
|
|
|
: Name(n), CmdLine(0), Actions(0), OutFileOption("-o"),
|
|
|
|
Flags(0), OnEmpty(0)
|
2008-12-07 17:41:11 +01:00
|
|
|
{}
|
|
|
|
};
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// ToolDescriptions - A list of Tool information records.
|
|
|
|
typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
|
|
|
|
/// CollectToolProperties - Function object for iterating over a list of
|
|
|
|
/// tool property records.
|
2009-12-07 11:51:55 +01:00
|
|
|
|
|
|
|
class CollectToolProperties;
|
|
|
|
typedef void (CollectToolProperties::* CollectToolPropertiesHandler)
|
2009-12-17 08:48:49 +01:00
|
|
|
(const DagInit&);
|
2009-12-07 11:51:55 +01:00
|
|
|
|
|
|
|
class CollectToolProperties : public HandlerTable<CollectToolPropertiesHandler>
|
|
|
|
{
|
2008-12-07 17:41:11 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
/// toolDesc_ - Properties of the current Tool. This is where the
|
|
|
|
/// information is stored.
|
|
|
|
ToolDescription& toolDesc_;
|
2008-03-23 09:57:20 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
explicit CollectToolProperties (ToolDescription& d)
|
2009-12-07 11:51:55 +01:00
|
|
|
: toolDesc_(d)
|
2008-03-23 09:57:20 +01:00
|
|
|
{
|
|
|
|
if (!staticMembersInitialized_) {
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
AddHandler("actions", &CollectToolProperties::onActions);
|
2010-02-23 10:04:44 +01:00
|
|
|
AddHandler("command", &CollectToolProperties::onCommand);
|
2008-12-07 17:41:11 +01:00
|
|
|
AddHandler("in_language", &CollectToolProperties::onInLanguage);
|
|
|
|
AddHandler("join", &CollectToolProperties::onJoin);
|
|
|
|
AddHandler("out_language", &CollectToolProperties::onOutLanguage);
|
2010-02-23 10:04:44 +01:00
|
|
|
|
|
|
|
AddHandler("out_file_option", &CollectToolProperties::onOutFileOption);
|
|
|
|
AddHandler("in_file_option", &CollectToolProperties::onInFileOption);
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
|
|
|
|
AddHandler("sink", &CollectToolProperties::onSink);
|
2010-02-23 10:04:28 +01:00
|
|
|
AddHandler("works_on_empty", &CollectToolProperties::onWorksOnEmpty);
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
staticMembersInitialized_ = true;
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void operator() (Init* I) {
|
|
|
|
InvokeDagInitHandler(this, I);
|
2009-12-07 11:51:55 +01:00
|
|
|
}
|
|
|
|
|
2008-03-23 09:57:20 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
/// Property handlers --
|
|
|
|
/// Functions that extract information about tool properties from
|
|
|
|
/// DAG representation.
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onActions (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 08:48:49 +01:00
|
|
|
Init* Case = d.getArg(0);
|
2008-12-07 17:45:12 +01:00
|
|
|
if (typeid(*Case) != typeid(DagInit) ||
|
2009-12-17 08:48:49 +01:00
|
|
|
GetOperatorName(static_cast<DagInit&>(*Case)) != "case")
|
2009-12-07 20:15:57 +01:00
|
|
|
throw "The argument to (actions) should be a 'case' construct!";
|
2008-12-07 17:45:12 +01:00
|
|
|
toolDesc_.Actions = Case;
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
|
|
|
|
2010-02-23 10:04:44 +01:00
|
|
|
void onCommand (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 08:48:49 +01:00
|
|
|
toolDesc_.CmdLine = d.getArg(0);
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onInLanguage (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 08:48:49 +01:00
|
|
|
Init* arg = d.getArg(0);
|
2008-05-30 08:18:16 +02:00
|
|
|
|
|
|
|
// Find out the argument's type.
|
|
|
|
if (typeid(*arg) == typeid(StringInit)) {
|
|
|
|
// It's a string.
|
2008-12-07 17:41:11 +01:00
|
|
|
toolDesc_.InLanguage.push_back(InitPtrToString(arg));
|
2008-05-30 08:18:16 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// It's a list.
|
|
|
|
const ListInit& lst = InitPtrToList(arg);
|
2008-12-07 17:41:11 +01:00
|
|
|
StrVector& out = toolDesc_.InLanguage;
|
2008-05-30 08:18:16 +02:00
|
|
|
|
|
|
|
// Copy strings to the output vector.
|
|
|
|
for (ListInit::const_iterator B = lst.begin(), E = lst.end();
|
|
|
|
B != E; ++B) {
|
|
|
|
out.push_back(InitPtrToString(*B));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove duplicates.
|
|
|
|
std::sort(out.begin(), out.end());
|
|
|
|
StrVector::iterator newE = std::unique(out.begin(), out.end());
|
|
|
|
out.erase(newE, out.end());
|
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onJoin (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2008-12-07 17:41:11 +01:00
|
|
|
toolDesc_.setJoin();
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onOutLanguage (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 08:48:49 +01:00
|
|
|
toolDesc_.OutLanguage = InitPtrToString(d.getArg(0));
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2010-02-23 10:04:44 +01: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 08:48:49 +01:00
|
|
|
void onOutputSuffix (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 08:48:49 +01:00
|
|
|
toolDesc_.OutputSuffix = InitPtrToString(d.getArg(0));
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void onSink (const DagInit& d) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 0);
|
2008-12-07 17:41:11 +01:00
|
|
|
toolDesc_.setSink();
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2010-02-23 10:04:28 +01:00
|
|
|
void onWorksOnEmpty (const DagInit& d) {
|
|
|
|
toolDesc_.OnEmpty = d.getArg(0);
|
|
|
|
}
|
|
|
|
|
2008-03-23 09:57:20 +01:00
|
|
|
};
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// CollectToolDescriptions - Gather information about tool properties
|
2008-05-30 08:26:08 +02:00
|
|
|
/// from the parsed TableGen data (basically a wrapper for the
|
2008-12-07 17:41:11 +01:00
|
|
|
/// CollectToolProperties function object).
|
|
|
|
void CollectToolDescriptions (RecordVector::const_iterator B,
|
|
|
|
RecordVector::const_iterator E,
|
|
|
|
ToolDescriptions& ToolDescs)
|
2008-03-23 09:57:20 +01:00
|
|
|
{
|
|
|
|
// Iterate over a properties list of every Tool definition
|
|
|
|
for (;B!=E;++B) {
|
2008-11-17 18:29:42 +01:00
|
|
|
const Record* T = *B;
|
2008-05-30 08:26:08 +02:00
|
|
|
// Throws an exception if the value does not exist.
|
2008-03-23 09:57:20 +01:00
|
|
|
ListInit* PropList = T->getValueAsListInit("properties");
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
IntrusiveRefCntPtr<ToolDescription>
|
|
|
|
ToolDesc(new ToolDescription(T->getName()));
|
2008-03-23 09:57:20 +01:00
|
|
|
|
|
|
|
std::for_each(PropList->begin(), PropList->end(),
|
2008-12-07 17:41:11 +01:00
|
|
|
CollectToolProperties(*ToolDesc));
|
|
|
|
ToolDescs.push_back(ToolDesc);
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// FillInEdgeVector - Merge all compilation graph definitions into
|
|
|
|
/// one single edge list.
|
|
|
|
void FillInEdgeVector(RecordVector::const_iterator B,
|
|
|
|
RecordVector::const_iterator E, RecordVector& Out) {
|
|
|
|
for (; B != E; ++B) {
|
|
|
|
const ListInit* edges = (*B)->getValueAsListInit("edges");
|
2008-05-30 08:27:02 +02:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
for (unsigned i = 0; i < edges->size(); ++i)
|
|
|
|
Out.push_back(edges->getElementAsRecord(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CalculatePriority - Calculate the priority of this plugin.
|
|
|
|
int CalculatePriority(RecordVector::const_iterator B,
|
|
|
|
RecordVector::const_iterator E) {
|
2009-10-17 22:08:30 +02:00
|
|
|
int priority = 0;
|
|
|
|
|
|
|
|
if (B != E) {
|
|
|
|
priority = static_cast<int>((*B)->getValueAsInt("priority"));
|
|
|
|
|
|
|
|
if (++B != E)
|
2009-12-07 20:15:57 +01:00
|
|
|
throw "More than one 'PluginPriority' instance found: "
|
|
|
|
"most probably an error!";
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
2009-10-17 22:08:30 +02:00
|
|
|
|
|
|
|
return priority;
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
2008-05-30 08:26:08 +02:00
|
|
|
|
2008-12-07 17:41:11 +01: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.
|
|
|
|
void FilterNotInGraph (const RecordVector& EdgeVector,
|
|
|
|
ToolDescriptions& ToolDescs) {
|
|
|
|
|
|
|
|
// List all tools mentioned in the graph.
|
|
|
|
llvm::StringSet<> ToolsInGraph;
|
|
|
|
|
|
|
|
for (RecordVector::const_iterator B = EdgeVector.begin(),
|
|
|
|
E = EdgeVector.end(); B != E; ++B) {
|
|
|
|
|
|
|
|
const Record* Edge = *B;
|
2008-12-07 17:44:47 +01:00
|
|
|
const std::string& NodeA = Edge->getValueAsString("a");
|
|
|
|
const std::string& NodeB = Edge->getValueAsString("b");
|
2008-12-07 17:41:11 +01:00
|
|
|
|
2008-12-07 17:44:47 +01:00
|
|
|
if (NodeA != "root")
|
|
|
|
ToolsInGraph.insert(NodeA);
|
|
|
|
ToolsInGraph.insert(NodeB);
|
2008-05-30 08:26:08 +02:00
|
|
|
}
|
2008-12-07 17:41:11 +01: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 08:26:08 +02:00
|
|
|
}
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// FillInToolToLang - Fills in two tables that map tool names to
|
|
|
|
/// (input, output) languages. Helper function used by TypecheckGraph().
|
|
|
|
void FillInToolToLang (const ToolDescriptions& ToolDescs,
|
|
|
|
StringMap<StringSet<> >& ToolToInLang,
|
|
|
|
StringMap<std::string>& ToolToOutLang) {
|
|
|
|
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);
|
|
|
|
ToolToOutLang[D.Name] = D.OutLanguage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// TypecheckGraph - Check that names for output and input languages
|
|
|
|
/// on all edges do match. This doesn't do much when the information
|
|
|
|
/// about the whole graph is not available (i.e. when compiling most
|
|
|
|
/// plugins).
|
|
|
|
void TypecheckGraph (const RecordVector& EdgeVector,
|
|
|
|
const ToolDescriptions& ToolDescs) {
|
|
|
|
StringMap<StringSet<> > ToolToInLang;
|
|
|
|
StringMap<std::string> ToolToOutLang;
|
|
|
|
|
|
|
|
FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
|
|
|
|
StringMap<std::string>::iterator IAE = ToolToOutLang.end();
|
|
|
|
StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
|
|
|
|
|
|
|
|
for (RecordVector::const_iterator B = EdgeVector.begin(),
|
|
|
|
E = EdgeVector.end(); B != E; ++B) {
|
|
|
|
const Record* Edge = *B;
|
2008-12-07 17:44:47 +01:00
|
|
|
const std::string& NodeA = Edge->getValueAsString("a");
|
|
|
|
const std::string& NodeB = Edge->getValueAsString("b");
|
|
|
|
StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
|
|
|
|
StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
|
2008-12-07 17:41:11 +01:00
|
|
|
|
2008-12-07 17:44:47 +01:00
|
|
|
if (NodeA != "root") {
|
2008-12-07 17:41:11 +01:00
|
|
|
if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
|
2008-12-07 17:44:47 +01:00
|
|
|
throw "Edge " + NodeA + "->" + NodeB
|
2008-12-07 17:41:11 +01:00
|
|
|
+ ": output->input language mismatch";
|
|
|
|
}
|
|
|
|
|
2008-12-07 17:44:47 +01:00
|
|
|
if (NodeB == "root")
|
2009-12-07 20:15:57 +01:00
|
|
|
throw "Edges back to the root are not allowed!";
|
2008-12-07 17:41:11 +01: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-19 00:51:30 +02: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 17:41:11 +01:00
|
|
|
template <typename F1, typename F2>
|
2009-10-19 00:51:30 +02:00
|
|
|
void WalkCase(const Init* Case, F1 TestCallback, F2 StatementCallback,
|
|
|
|
unsigned IndentLevel = 0)
|
|
|
|
{
|
2008-12-07 17:41:11 +01:00
|
|
|
const DagInit& d = InitPtrToDag(Case);
|
2009-10-19 00:51:30 +02:00
|
|
|
|
|
|
|
// Error checks.
|
|
|
|
if (GetOperatorName(d) != "case")
|
2009-12-07 20:15:57 +01:00
|
|
|
throw "WalkCase should be invoked only on 'case' expressions!";
|
2009-10-19 00:51:30 +02: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 17:41:11 +01:00
|
|
|
bool even = false;
|
2009-10-19 00:51:30 +02:00
|
|
|
const unsigned numArgs = d.getNumArgs();
|
|
|
|
unsigned i = 1;
|
2008-12-07 17:41:11 +01:00
|
|
|
for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
|
|
|
|
B != E; ++B) {
|
|
|
|
Init* arg = *B;
|
2009-10-19 00:51:30 +02:00
|
|
|
|
|
|
|
if (!even)
|
|
|
|
{
|
|
|
|
// Handle test.
|
|
|
|
const DagInit& Test = InitPtrToDag(arg);
|
|
|
|
|
|
|
|
if (GetOperatorName(Test) == "default" && (i+1 != numArgs))
|
2009-12-07 20:15:57 +01:00
|
|
|
throw "The 'default' clause should be the last in the "
|
|
|
|
"'case' construct!";
|
2009-10-19 00:51:30 +02:00
|
|
|
if (i == numArgs)
|
|
|
|
throw "Case construct handler: no corresponding action "
|
|
|
|
"found for the test " + Test.getAsString() + '!';
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
TestCallback(Test, IndentLevel, (i == 1));
|
2009-10-19 00:51:30 +02:00
|
|
|
}
|
2008-12-07 17:41:11 +01:00
|
|
|
else
|
2009-10-19 00:51:30 +02:00
|
|
|
{
|
|
|
|
if (dynamic_cast<DagInit*>(arg)
|
2009-12-17 08:48:49 +01:00
|
|
|
&& GetOperatorName(static_cast<DagInit&>(*arg)) == "case") {
|
2009-10-19 00:51:30 +02:00
|
|
|
// Nested 'case'.
|
|
|
|
WalkCase(arg, TestCallback, StatementCallback, IndentLevel + Indent1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle statement.
|
|
|
|
StatementCallback(arg, IndentLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
2008-12-07 17:41:11 +01:00
|
|
|
even = !even;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ExtractOptionNames - A helper function object used by
|
|
|
|
/// CheckForSuperfluousOptions() to walk the 'case' DAG.
|
|
|
|
class ExtractOptionNames {
|
|
|
|
llvm::StringSet<>& OptionNames_;
|
|
|
|
|
2008-12-07 17:42:22 +01:00
|
|
|
void processDag(const Init* Statement) {
|
2008-12-07 17:41:11 +01:00
|
|
|
const DagInit& Stmt = InitPtrToDag(Statement);
|
2009-10-19 00:51:30 +02:00
|
|
|
const std::string& ActionName = GetOperatorName(Stmt);
|
2008-12-07 17:41:11 +01:00
|
|
|
if (ActionName == "forward" || ActionName == "forward_as" ||
|
2009-12-07 18:03:05 +01:00
|
|
|
ActionName == "forward_value" ||
|
|
|
|
ActionName == "forward_transformed_value" ||
|
2010-01-01 04:51:02 +01:00
|
|
|
ActionName == "switch_on" || ActionName == "any_switch_on" ||
|
|
|
|
ActionName == "parameter_equals" ||
|
2009-12-07 19:25:54 +01:00
|
|
|
ActionName == "element_in_list" || ActionName == "not_empty" ||
|
|
|
|
ActionName == "empty") {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(Stmt, 1);
|
2010-01-01 04:51:02 +01:00
|
|
|
|
|
|
|
Init* Arg = Stmt.getArg(0);
|
|
|
|
if (typeid(*Arg) == typeid(StringInit)) {
|
|
|
|
const std::string& Name = InitPtrToString(Arg);
|
|
|
|
OptionNames_.insert(Name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// It's a list.
|
|
|
|
const ListInit& List = InitPtrToList(Arg);
|
|
|
|
for (ListInit::const_iterator B = List.begin(), E = List.end();
|
|
|
|
B != E; ++B) {
|
|
|
|
const std::string& Name = InitPtrToString(*B);
|
|
|
|
OptionNames_.insert(Name);
|
|
|
|
}
|
|
|
|
}
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
2010-01-01 04:51:02 +01:00
|
|
|
else if (ActionName == "and" || ActionName == "or" || ActionName == "not") {
|
2008-12-07 17:41:11 +01:00
|
|
|
for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
|
2008-12-07 17:42:22 +01:00
|
|
|
this->processDag(Stmt.getArg(i));
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-07 17:42:22 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void operator()(const Init* Statement) {
|
|
|
|
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-19 00:51:30 +02:00
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void operator()(const DagInit& Test, unsigned, bool) {
|
|
|
|
this->operator()(&Test);
|
2009-10-19 00:51:30 +02:00
|
|
|
}
|
|
|
|
void operator()(const Init* Statement, unsigned) {
|
|
|
|
this->operator()(Statement);
|
|
|
|
}
|
2008-12-07 17:41:11 +01:00
|
|
|
};
|
|
|
|
|
2008-05-30 08:28:37 +02:00
|
|
|
/// CheckForSuperfluousOptions - Check that there are no side
|
|
|
|
/// effect-free options (specified only in the OptionList). Otherwise,
|
|
|
|
/// output a warning.
|
2008-12-07 17:41:11 +01:00
|
|
|
void CheckForSuperfluousOptions (const RecordVector& Edges,
|
|
|
|
const ToolDescriptions& ToolDescs,
|
|
|
|
const OptionDescriptions& OptDescs) {
|
2008-05-30 08:28:37 +02:00
|
|
|
llvm::StringSet<> nonSuperfluousOptions;
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
// Add all options mentioned in the ToolDesc.Actions to the set of
|
2008-05-30 08:28:37 +02:00
|
|
|
// non-superfluous options.
|
2008-12-07 17:41:11 +01: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.
|
|
|
|
for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
|
|
|
|
B != E; ++B) {
|
|
|
|
const Record* Edge = *B;
|
2009-12-17 08:48:49 +01:00
|
|
|
DagInit& Weight = *Edge->getValueAsDag("weight");
|
2008-12-07 17:41:11 +01:00
|
|
|
|
2009-12-23 13:49:41 +01:00
|
|
|
if (!IsDagEmpty(Weight))
|
2009-12-17 08:48:49 +01:00
|
|
|
WalkCase(&Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
|
2008-05-30 08:28:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all options in OptDescs belong to the set of
|
|
|
|
// non-superfluous options.
|
2008-12-07 17:41:11 +01:00
|
|
|
for (OptionDescriptions::const_iterator B = OptDescs.begin(),
|
2008-05-30 08:28:37 +02:00
|
|
|
E = OptDescs.end(); B != E; ++B) {
|
2008-12-07 17:41:11 +01:00
|
|
|
const OptionDescription& Val = B->second;
|
2008-05-30 08:28:37 +02:00
|
|
|
if (!nonSuperfluousOptions.count(Val.Name)
|
|
|
|
&& Val.Type != OptionType::Alias)
|
2009-07-03 02:10:29 +02:00
|
|
|
llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
|
2008-05-30 08:28:37 +02:00
|
|
|
"Probable cause: this option is specified only in the OptionList.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-28 03:16:42 +02: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;
|
|
|
|
}
|
|
|
|
|
2009-10-21 04:13:13 +02:00
|
|
|
/// EmitListTest - Helper function used by EmitCaseTest1ArgList().
|
|
|
|
template <typename F>
|
|
|
|
void EmitListTest(const ListInit& L, const char* LogicOp,
|
|
|
|
F Callback, raw_ostream& O)
|
|
|
|
{
|
|
|
|
// This is a lot like EmitLogicalOperationTest, but works on ListInits instead
|
|
|
|
// of Dags...
|
|
|
|
bool isFirst = true;
|
|
|
|
for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B) {
|
|
|
|
if (isFirst)
|
|
|
|
isFirst = false;
|
|
|
|
else
|
2010-01-01 05:40:54 +01:00
|
|
|
O << ' ' << LogicOp << ' ';
|
2009-10-21 04:13:13 +02:00
|
|
|
Callback(InitPtrToString(*B), O);
|
|
|
|
}
|
|
|
|
}
|
2008-12-17 03:47:01 +01:00
|
|
|
|
2009-10-21 04:13:13 +02:00
|
|
|
// Callbacks for use with EmitListTest.
|
|
|
|
|
|
|
|
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 08:10:19 +02:00
|
|
|
O << OptDesc.GenVariableName();
|
2009-10-17 22:09:29 +02:00
|
|
|
}
|
2009-10-21 04:13:13 +02:00
|
|
|
};
|
2008-09-22 22:48:22 +02:00
|
|
|
|
2009-10-21 04:13:13 +02:00
|
|
|
class EmitEmptyTest {
|
|
|
|
bool EmitNegate_;
|
|
|
|
const OptionDescriptions& OptDescs_;
|
|
|
|
public:
|
|
|
|
EmitEmptyTest(bool EmitNegate, const OptionDescriptions& OptDescs)
|
|
|
|
: EmitNegate_(EmitNegate), OptDescs_(OptDescs)
|
|
|
|
{}
|
2008-12-17 03:47:01 +01:00
|
|
|
|
2009-10-21 04:13:13 +02:00
|
|
|
void operator()(const std::string& OptName, raw_ostream& O) const {
|
|
|
|
const char* Neg = (EmitNegate_ ? "!" : "");
|
2008-05-30 08:24:07 +02:00
|
|
|
if (OptName == "o") {
|
2009-10-21 04:13:13 +02:00
|
|
|
O << Neg << "OutputFilename.empty()";
|
2008-05-30 08:24:07 +02:00
|
|
|
}
|
2009-12-01 07:51:30 +01:00
|
|
|
else if (OptName == "save-temps") {
|
|
|
|
O << Neg << "(SaveTemps == SaveTempsEnum::Unset)";
|
|
|
|
}
|
2008-05-30 08:24:07 +02:00
|
|
|
else {
|
2009-10-21 04:13:13 +02:00
|
|
|
const OptionDescription& OptDesc = OptDescs_.FindListOrParameter(OptName);
|
|
|
|
O << Neg << OptDesc.GenVariableName() << ".empty()";
|
2008-05-30 08:24:07 +02:00
|
|
|
}
|
2008-05-30 08:10:19 +02:00
|
|
|
}
|
2009-10-21 04:13:13 +02:00
|
|
|
};
|
2008-05-30 08:10:19 +02:00
|
|
|
|
|
|
|
|
2009-10-17 22:09:29 +02:00
|
|
|
/// EmitCaseTest1ArgList - Helper function used by EmitCaseTest1Arg();
|
|
|
|
bool EmitCaseTest1ArgList(const std::string& TestName,
|
|
|
|
const DagInit& d,
|
|
|
|
const OptionDescriptions& OptDescs,
|
|
|
|
raw_ostream& O) {
|
2010-01-01 04:50:51 +01:00
|
|
|
const ListInit& L = InitPtrToList(d.getArg(0));
|
2009-10-17 22:09:29 +02:00
|
|
|
|
|
|
|
if (TestName == "any_switch_on") {
|
2009-10-21 04:13:13 +02:00
|
|
|
EmitListTest(L, "||", EmitSwitchOn(OptDescs), O);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "switch_on") {
|
|
|
|
EmitListTest(L, "&&", EmitSwitchOn(OptDescs), O);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "any_not_empty") {
|
|
|
|
EmitListTest(L, "||", EmitEmptyTest(true, OptDescs), O);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "any_empty") {
|
|
|
|
EmitListTest(L, "||", EmitEmptyTest(false, OptDescs), O);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "not_empty") {
|
|
|
|
EmitListTest(L, "&&", EmitEmptyTest(true, OptDescs), O);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "empty") {
|
|
|
|
EmitListTest(L, "&&", EmitEmptyTest(false, OptDescs), O);
|
|
|
|
return true;
|
|
|
|
}
|
2009-10-17 22:09:29 +02:00
|
|
|
|
2009-10-21 04:13:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-10-17 22:09:29 +02:00
|
|
|
|
2009-10-21 04:13:13 +02:00
|
|
|
/// EmitCaseTest1ArgStr - Helper function used by EmitCaseTest1Arg();
|
|
|
|
bool EmitCaseTest1ArgStr(const std::string& TestName,
|
|
|
|
const DagInit& d,
|
|
|
|
const OptionDescriptions& OptDescs,
|
|
|
|
raw_ostream& O) {
|
|
|
|
const std::string& OptName = InitPtrToString(d.getArg(0));
|
2009-10-17 22:09:29 +02:00
|
|
|
|
2009-10-21 04:13:13 +02:00
|
|
|
if (TestName == "switch_on") {
|
|
|
|
apply(EmitSwitchOn(OptDescs), OptName, O);
|
2009-10-17 22:09:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
2009-10-21 04:13:13 +02:00
|
|
|
else if (TestName == "input_languages_contain") {
|
|
|
|
O << "InLangs.count(\"" << OptName << "\") != 0";
|
|
|
|
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 22:09:29 +02:00
|
|
|
|
2009-10-21 04:13:13 +02:00
|
|
|
// TODO: make this work with Edge::Weight (if possible).
|
|
|
|
O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "not_empty" || TestName == "empty") {
|
|
|
|
bool EmitNegate = (TestName == "not_empty");
|
|
|
|
apply(EmitEmptyTest(EmitNegate, OptDescs), OptName, O);
|
|
|
|
return true;
|
|
|
|
}
|
2009-10-17 22:09:29 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler();
|
|
|
|
bool EmitCaseTest1Arg(const std::string& TestName,
|
|
|
|
const DagInit& d,
|
|
|
|
const OptionDescriptions& OptDescs,
|
|
|
|
raw_ostream& O) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-10-17 22:09:29 +02:00
|
|
|
if (typeid(*d.getArg(0)) == typeid(ListInit))
|
|
|
|
return EmitCaseTest1ArgList(TestName, d, OptDescs, O);
|
|
|
|
else
|
|
|
|
return EmitCaseTest1ArgStr(TestName, d, OptDescs, O);
|
|
|
|
}
|
|
|
|
|
2009-09-28 03:16:42 +02:00
|
|
|
/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
|
2008-05-30 08:10:19 +02:00
|
|
|
bool EmitCaseTest2Args(const std::string& TestName,
|
|
|
|
const DagInit& d,
|
2009-09-21 17:53:44 +02:00
|
|
|
unsigned IndentLevel,
|
2008-12-07 17:41:11 +01:00
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 2);
|
2008-05-30 08:10:19 +02:00
|
|
|
const std::string& OptName = InitPtrToString(d.getArg(0));
|
|
|
|
const std::string& OptArg = InitPtrToString(d.getArg(1));
|
|
|
|
|
|
|
|
if (TestName == "parameter_equals") {
|
2009-10-21 04:13:13 +02:00
|
|
|
const OptionDescription& OptDesc = OptDescs.FindParameter(OptName);
|
2008-05-30 08:10:19 +02:00
|
|
|
O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "element_in_list") {
|
2009-10-21 04:13:13 +02:00
|
|
|
const OptionDescription& OptDesc = OptDescs.FindList(OptName);
|
2008-05-30 08:10:19 +02:00
|
|
|
const std::string& VarName = OptDesc.GenVariableName();
|
2009-09-21 17:53:44 +02:00
|
|
|
O << "std::find(" << VarName << ".begin(),\n";
|
|
|
|
O.indent(IndentLevel + Indent1)
|
|
|
|
<< VarName << ".end(), \""
|
2008-05-30 08:10:19 +02:00
|
|
|
<< OptArg << "\") != " << VarName << ".end()";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward declaration.
|
|
|
|
// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
|
2009-09-21 17:53:44 +02:00
|
|
|
void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
|
2008-12-07 17:41:11 +01:00
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O);
|
2008-05-30 08:10:19 +02:00
|
|
|
|
|
|
|
/// EmitLogicalOperationTest - Helper function used by
|
|
|
|
/// EmitCaseConstructHandler.
|
|
|
|
void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
|
2009-09-21 17:53:44 +02:00
|
|
|
unsigned IndentLevel,
|
2008-12-07 17:41:11 +01:00
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O) {
|
2008-05-30 08:10:19 +02:00
|
|
|
O << '(';
|
|
|
|
for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
|
2008-05-30 08:18:16 +02:00
|
|
|
const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
|
2008-05-30 08:10:19 +02:00
|
|
|
EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
|
2009-09-21 17:53:44 +02:00
|
|
|
if (j != NumArgs - 1) {
|
|
|
|
O << ")\n";
|
|
|
|
O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
|
|
|
|
}
|
|
|
|
else {
|
2008-05-30 08:10:19 +02:00
|
|
|
O << ')';
|
2009-09-21 17:53:44 +02:00
|
|
|
}
|
2008-05-30 08:10:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-21 17:53:44 +02:00
|
|
|
void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
|
2009-09-10 18:21:38 +02:00
|
|
|
const OptionDescriptions& OptDescs, raw_ostream& O)
|
|
|
|
{
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-09-10 18:21:38 +02:00
|
|
|
const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
|
|
|
|
O << "! (";
|
|
|
|
EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
|
|
|
|
O << ")";
|
|
|
|
}
|
|
|
|
|
2008-05-30 08:10:19 +02:00
|
|
|
/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
|
2009-09-21 17:53:44 +02:00
|
|
|
void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
|
2008-12-07 17:41:11 +01:00
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O) {
|
2009-10-19 00:51:30 +02:00
|
|
|
const std::string& TestName = GetOperatorName(d);
|
2008-05-30 08:10:19 +02:00
|
|
|
|
|
|
|
if (TestName == "and")
|
|
|
|
EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
|
|
|
|
else if (TestName == "or")
|
|
|
|
EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
|
2009-09-10 18:21:38 +02:00
|
|
|
else if (TestName == "not")
|
|
|
|
EmitLogicalNot(d, IndentLevel, OptDescs, O);
|
2009-09-28 03:16:42 +02:00
|
|
|
else if (EmitCaseTest0Args(TestName, O))
|
|
|
|
return;
|
2008-05-30 08:10:19 +02:00
|
|
|
else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
|
|
|
|
return;
|
|
|
|
else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
|
|
|
|
return;
|
|
|
|
else
|
2010-01-01 04:50:34 +01:00
|
|
|
throw "Unknown test '" + TestName + "' used in the 'case' construct!";
|
2008-05-30 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
2009-10-19 00:51:30 +02: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 08:48:49 +01:00
|
|
|
void operator()(const DagInit& Test, unsigned IndentLevel, bool FirstTest)
|
2009-10-19 00:51:30 +02:00
|
|
|
{
|
|
|
|
if (GetOperatorName(Test) == "default") {
|
|
|
|
O_.indent(IndentLevel) << "else {\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
O_.indent(IndentLevel)
|
|
|
|
<< ((!FirstTest && EmitElseIf_) ? "else if (" : "if (");
|
2009-12-17 08:48:49 +01:00
|
|
|
EmitCaseTest(Test, IndentLevel, OptDescs_, O_);
|
2009-10-19 00:51:30 +02: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) {
|
2009-10-19 23:24:28 +02:00
|
|
|
|
2009-10-19 00:51:30 +02:00
|
|
|
// Ignore nested 'case' DAG.
|
|
|
|
if (!(dynamic_cast<const DagInit*>(Statement) &&
|
2009-12-17 08:48:49 +01:00
|
|
|
GetOperatorName(static_cast<const DagInit&>(*Statement)) == "case")) {
|
2009-10-19 23:24:28 +02: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-19 00:51:30 +02:00
|
|
|
O_.indent(IndentLevel) << "}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-10-17 22:09:29 +02:00
|
|
|
/// EmitCaseConstructHandler - Emit code that handles the 'case'
|
|
|
|
/// construct. Takes a function object that should emit code for every case
|
2009-10-19 00:51:30 +02:00
|
|
|
/// clause. Implemented on top of WalkCase.
|
2010-02-23 10:04:28 +01:00
|
|
|
/// Callback's type is void F(const Init* Statement, unsigned IndentLevel,
|
2009-10-17 22:09:29 +02:00
|
|
|
/// raw_ostream& O).
|
|
|
|
/// EmitElseIf parameter controls the type of condition that is emitted ('if
|
2009-10-19 00:51:30 +02:00
|
|
|
/// (..) {..} else if (..) {} .. else {..}' vs. 'if (..) {..} if(..) {..}
|
|
|
|
/// .. else {..}').
|
2008-05-30 08:10:19 +02:00
|
|
|
template <typename F>
|
2009-10-19 00:51:30 +02:00
|
|
|
void EmitCaseConstructHandler(const Init* Case, unsigned IndentLevel,
|
2008-05-31 15:43:21 +02:00
|
|
|
F Callback, bool EmitElseIf,
|
2008-12-07 17:41:11 +01:00
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O) {
|
2009-10-19 00:51:30 +02:00
|
|
|
WalkCase(Case, EmitCaseTestCallback(EmitElseIf, OptDescs, O),
|
|
|
|
EmitCaseStatementCallback<F>(Callback, O), IndentLevel);
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2009-12-17 08:48:34 +01:00
|
|
|
/// TokenizeCmdLine - converts from
|
2009-12-15 08:20:50 +01:00
|
|
|
/// "$CALL(HookName, 'Arg1', 'Arg2')/path -arg1 -arg2" to
|
|
|
|
/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path", "-arg1", "-arg2"].
|
2009-12-17 08:48:34 +01:00
|
|
|
void TokenizeCmdLine(const std::string& CmdLine, StrVector& Out) {
|
2009-01-21 14:04:00 +01:00
|
|
|
const char* Delimiters = " \t\n\v\f\r";
|
|
|
|
enum TokenizerState
|
|
|
|
{ Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
|
|
|
|
cur_st = Normal;
|
2009-06-25 20:21:34 +02:00
|
|
|
|
|
|
|
if (CmdLine.empty())
|
|
|
|
return;
|
2009-01-21 14:04:00 +01:00
|
|
|
Out.push_back("");
|
|
|
|
|
|
|
|
std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
|
|
|
|
E = CmdLine.size();
|
2009-06-25 20:21:34 +02:00
|
|
|
|
2009-01-21 14:04:00 +01: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 13:49:41 +01:00
|
|
|
if (OneOf(Delimiters, cur_ch)) {
|
2009-01-21 14:04:00 +01: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 13:49:41 +01:00
|
|
|
if (OneOf(Delimiters, cur_ch)) {
|
2009-01-21 14:04:00 +01:00
|
|
|
cur_st = Normal;
|
|
|
|
Out.push_back("");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (cur_ch == '(') {
|
|
|
|
Out.push_back("");
|
|
|
|
cur_st = InsideSpecialCommand;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case InsideSpecialCommand:
|
2009-12-23 13:49:41 +01:00
|
|
|
if (OneOf(Delimiters, cur_ch)) {
|
2009-01-21 14:04:00 +01: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 04:04:02 +01: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 14:04:00 +01:00
|
|
|
{
|
2009-12-15 04:04:02 +01:00
|
|
|
const char* errorMessage = "Syntax error in $CALL invocation!";
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckedIncrement(Pos, End, errorMessage);
|
2009-12-15 04:04:02 +01:00
|
|
|
const std::string& CmdName = *Pos;
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-15 04:04:02 +01:00
|
|
|
if (CmdName == ")")
|
|
|
|
throw "$CALL invocation: empty argument list!";
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-15 04:04:02 +01:00
|
|
|
O << "hooks::";
|
|
|
|
O << CmdName << "(";
|
2008-05-30 08:13:29 +02:00
|
|
|
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-15 04:04:02 +01:00
|
|
|
bool firstIteration = true;
|
|
|
|
while (true) {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckedIncrement(Pos, End, errorMessage);
|
2009-12-15 04:04:02 +01:00
|
|
|
const std::string& Arg = *Pos;
|
|
|
|
assert(Arg.size() != 0);
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-15 04:04:02 +01:00
|
|
|
if (Arg[0] == ')')
|
|
|
|
break;
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-15 04:04:02 +01:00
|
|
|
if (firstIteration)
|
|
|
|
firstIteration = false;
|
|
|
|
else
|
|
|
|
O << ", ";
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-15 04:04:02 +01:00
|
|
|
if (Arg == "$INFILE") {
|
|
|
|
if (IsJoin)
|
|
|
|
throw "$CALL(Hook, $INFILE) can't be used with a Join tool!";
|
2009-01-21 14:04:00 +01:00
|
|
|
else
|
2009-12-15 04:04:02 +01:00
|
|
|
O << "inFile.c_str()";
|
|
|
|
}
|
|
|
|
else {
|
2009-01-21 14:04:00 +01:00
|
|
|
O << '"' << Arg << '"';
|
|
|
|
}
|
2009-12-15 04:04:02 +01:00
|
|
|
}
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-15 04:04:02 +01:00
|
|
|
O << ')';
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-15 04:04:02 +01:00
|
|
|
return Pos;
|
|
|
|
}
|
2008-05-30 08:13:29 +02:00
|
|
|
|
2009-12-15 04:04:02 +01: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 13:49:41 +01:00
|
|
|
CheckedIncrement(Pos, End, errorMessage);
|
2009-12-15 04:04:02 +01:00
|
|
|
const std::string& EnvName = *Pos;
|
|
|
|
|
|
|
|
if (EnvName == ")")
|
|
|
|
throw "$ENV invocation: empty argument list!";
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-15 04:04:02 +01:00
|
|
|
O << "checkCString(std::getenv(\"";
|
|
|
|
O << EnvName;
|
|
|
|
O << "\"))";
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckedIncrement(Pos, End, errorMessage);
|
2009-12-15 04:04:02 +01: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 08:13:29 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw "Unknown special command: " + cmd;
|
|
|
|
}
|
|
|
|
|
2009-12-15 04:04:02 +01:00
|
|
|
// Handle '$CMD(ARG)/additional/text'.
|
2009-01-21 14:04:00 +01:00
|
|
|
const std::string& Leftover = *Pos;
|
|
|
|
assert(Leftover.at(0) == ')');
|
|
|
|
if (Leftover.size() != 1)
|
|
|
|
O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
|
2008-05-30 08:13:29 +02:00
|
|
|
|
2009-01-21 14:04:00 +01:00
|
|
|
return Pos;
|
2008-05-30 08:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitCmdLineVecFill - Emit code that fills in the command line
|
|
|
|
/// vector. Helper function used by EmitGenerateActionMethod().
|
2008-05-30 08:10:19 +02:00
|
|
|
void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
|
2009-09-21 17:53:44 +02:00
|
|
|
bool IsJoin, unsigned IndentLevel,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O) {
|
2008-05-30 08:10:19 +02:00
|
|
|
StrVector StrVec;
|
2009-12-17 08:48:34 +01:00
|
|
|
TokenizeCmdLine(InitPtrToString(CmdLine), StrVec);
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2008-05-30 08:12:24 +02:00
|
|
|
if (StrVec.empty())
|
2009-06-25 20:21:34 +02:00
|
|
|
throw "Tool '" + ToolName + "' has empty command line!";
|
2008-05-06 18:35:25 +02:00
|
|
|
|
2010-02-23 10:04:44 +01:00
|
|
|
StrVector::const_iterator B = StrVec.begin(), E = StrVec.end();
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2010-02-23 10:04:44 +01:00
|
|
|
// Emit the command itself.
|
2009-04-19 02:22:35 +02:00
|
|
|
assert(!StrVec[0].empty());
|
2010-02-23 10:04:44 +01:00
|
|
|
O.indent(IndentLevel) << "cmd = ";
|
2009-01-21 14:04:00 +01:00
|
|
|
if (StrVec[0][0] == '$') {
|
2010-02-23 10:04:44 +01:00
|
|
|
B = SubstituteSpecialCommands(B, E, IsJoin, O);
|
|
|
|
++B;
|
2009-01-21 14:04:00 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-02-23 10:04:44 +01:00
|
|
|
O << '"' << StrVec[0] << '"';
|
|
|
|
++B;
|
2009-01-21 14:04:00 +01:00
|
|
|
}
|
2010-02-23 10:04:44 +01:00
|
|
|
O << ";\n";
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2010-02-23 10:04:44 +01:00
|
|
|
// Go through the command arguments.
|
|
|
|
assert(B <= E);
|
|
|
|
for (; B != E; ++B) {
|
|
|
|
const std::string& cmd = *B;
|
2009-10-08 06:40:08 +02:00
|
|
|
|
2009-04-19 02:22:35 +02:00
|
|
|
assert(!cmd.empty());
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(IndentLevel);
|
2010-02-23 10:04:44 +01:00
|
|
|
|
2008-05-30 08:12:24 +02:00
|
|
|
if (cmd.at(0) == '$') {
|
2010-02-23 10:04:44 +01:00
|
|
|
O << "vec.push_back(std::make_pair(0, ";
|
|
|
|
B = SubstituteSpecialCommands(B, E, IsJoin, O);
|
|
|
|
O << "));\n";
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-02-23 10:04:44 +01:00
|
|
|
O << "vec.push_back(std::make_pair(0, \"" << cmd << "\"));\n";
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2008-05-30 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
2008-12-07 17:41:11 +01: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 17:53:44 +02:00
|
|
|
unsigned IndentLevel,
|
2008-12-07 17:41:11 +01:00
|
|
|
const std::string& NewName,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O) {
|
2008-12-07 17:41:11 +01:00
|
|
|
const std::string& Name = NewName.empty()
|
|
|
|
? ("-" + D.Name)
|
|
|
|
: NewName;
|
2009-09-21 17:53:44 +02:00
|
|
|
unsigned IndentLevel1 = IndentLevel + Indent1;
|
2008-12-07 17:41:11 +01:00
|
|
|
|
|
|
|
switch (D.Type) {
|
|
|
|
case OptionType::Switch:
|
2010-02-23 10:04:44 +01:00
|
|
|
O.indent(IndentLevel)
|
2010-02-23 10:04:57 +01:00
|
|
|
<< "vec.push_back(std::make_pair(" << D.GenVariableName()
|
|
|
|
<< ".getPosition(), \"" << Name << "\"));\n";
|
2008-12-07 17:41:11 +01:00
|
|
|
break;
|
|
|
|
case OptionType::Parameter:
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(IndentLevel) << "vec.push_back(std::make_pair("
|
|
|
|
<< D.GenVariableName()
|
|
|
|
<<".getPosition(), \"" << Name;
|
2010-02-23 10:04:13 +01:00
|
|
|
|
|
|
|
if (!D.isForwardNotSplit()) {
|
2010-02-23 10:04:44 +01:00
|
|
|
O << "\"));\n";
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(IndentLevel) << "vec.push_back(std::make_pair("
|
|
|
|
<< D.GenVariableName() << ".getPosition(), "
|
2010-02-23 10:04:44 +01:00
|
|
|
<< D.GenVariableName() << "));\n";
|
2010-02-23 10:04:13 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-02-23 10:04:44 +01:00
|
|
|
O << "=\" + " << D.GenVariableName() << "));\n";
|
2010-02-23 10:04:13 +01:00
|
|
|
}
|
2008-12-07 17:41:11 +01:00
|
|
|
break;
|
|
|
|
case OptionType::Prefix:
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(IndentLevel) << "vec.push_back(std::make_pair("
|
|
|
|
<< D.GenVariableName() << ".getPosition(), \""
|
2010-02-23 10:04:44 +01:00
|
|
|
<< Name << "\" + "
|
|
|
|
<< D.GenVariableName() << "));\n";
|
2008-12-07 17:41:11 +01:00
|
|
|
break;
|
|
|
|
case OptionType::PrefixList:
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(IndentLevel)
|
|
|
|
<< "for (" << D.GenTypeDeclaration()
|
|
|
|
<< "::iterator B = " << D.GenVariableName() << ".begin(),\n";
|
|
|
|
O.indent(IndentLevel)
|
|
|
|
<< "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
|
|
|
|
<< ".getPosition(B - " << D.GenVariableName()
|
|
|
|
<< ".begin());\n";
|
|
|
|
O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
|
2010-02-23 10:04:44 +01:00
|
|
|
<< Name << "\" + " << "*B));\n";
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(IndentLevel1) << "++B;\n";
|
2009-01-28 04:47:20 +01:00
|
|
|
|
|
|
|
for (int i = 1, j = D.MultiVal; i < j; ++i) {
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(IndentLevel1) << "++B;\n";
|
2009-01-28 04:47:20 +01:00
|
|
|
}
|
|
|
|
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(IndentLevel) << "}\n";
|
2008-12-07 17:41:11 +01:00
|
|
|
break;
|
|
|
|
case OptionType::ParameterList:
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(IndentLevel)
|
|
|
|
<< "for (" << D.GenTypeDeclaration() << "::iterator B = "
|
|
|
|
<< D.GenVariableName() << ".begin(),\n";
|
|
|
|
O.indent(IndentLevel) << "E = " << D.GenVariableName()
|
|
|
|
<< ".end() ; B != E;) {\n";
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(IndentLevel1) << "unsigned pos = " << D.GenVariableName()
|
|
|
|
<< ".getPosition(B - " << D.GenVariableName()
|
|
|
|
<< ".begin());\n";
|
|
|
|
O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, \""
|
2010-02-23 10:04:44 +01:00
|
|
|
<< Name << "\"));\n";
|
2009-01-28 04:47:20 +01:00
|
|
|
|
|
|
|
for (int i = 0, j = D.MultiVal; i < j; ++i) {
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(IndentLevel1) << "vec.push_back(std::make_pair(pos, *B));\n";
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(IndentLevel1) << "++B;\n";
|
2009-01-28 04:47:20 +01:00
|
|
|
}
|
|
|
|
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(IndentLevel) << "}\n";
|
2008-12-07 17:41:11 +01:00
|
|
|
break;
|
|
|
|
case OptionType::Alias:
|
|
|
|
default:
|
2009-12-07 20:15:57 +01:00
|
|
|
throw "Aliases are not allowed in tool option descriptions!";
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-19 23:24:28 +02:00
|
|
|
/// ActionHandlingCallbackBase - Base class of EmitActionHandlersCallback and
|
|
|
|
/// EmitPreprocessOptionsCallback.
|
2009-12-17 08:48:49 +01:00
|
|
|
struct ActionHandlingCallbackBase
|
|
|
|
{
|
2009-10-19 23:24:28 +02:00
|
|
|
|
|
|
|
void onErrorDag(const DagInit& d,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
|
|
|
O.indent(IndentLevel)
|
|
|
|
<< "throw std::runtime_error(\"" <<
|
|
|
|
(d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
|
|
|
|
: "Unknown error!")
|
|
|
|
<< "\");\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void onWarningDag(const DagInit& d,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-10-19 23:24:28 +02: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-17 08:48:49 +01:00
|
|
|
|
2009-12-07 11:51:55 +01:00
|
|
|
class EmitActionHandlersCallback;
|
2009-12-17 08:48:49 +01:00
|
|
|
|
2009-12-07 11:51:55 +01:00
|
|
|
typedef void (EmitActionHandlersCallback::* EmitActionHandlersCallbackHandler)
|
|
|
|
(const DagInit&, unsigned, raw_ostream&) const;
|
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
class EmitActionHandlersCallback :
|
|
|
|
public ActionHandlingCallbackBase,
|
2009-12-07 11:51:55 +01:00
|
|
|
public HandlerTable<EmitActionHandlersCallbackHandler>
|
|
|
|
{
|
|
|
|
typedef EmitActionHandlersCallbackHandler Handler;
|
2008-05-30 08:10:19 +02:00
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
const OptionDescriptions& OptDescs;
|
|
|
|
|
2009-12-15 08:20:50 +01: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 08:10:19 +02:00
|
|
|
{
|
2009-12-07 11:51:55 +01:00
|
|
|
StrVector Out;
|
2009-12-17 08:48:34 +01:00
|
|
|
TokenizeCmdLine(Str, Out);
|
2008-12-07 17:41:11 +01:00
|
|
|
|
2009-12-07 11:51:55 +01:00
|
|
|
for (StrVector::const_iterator B = Out.begin(), E = Out.end();
|
2009-12-15 08:20:50 +01: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 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(Dag, 1);
|
2009-12-15 08:20:50 +01:00
|
|
|
this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
|
2010-02-23 10:04:57 +01:00
|
|
|
"vec.push_back(std::make_pair(65536, ", "));\n",
|
2010-02-23 10:04:44 +01:00
|
|
|
IndentLevel, O);
|
2009-12-07 11:51:55 +01:00
|
|
|
}
|
2009-02-27 07:46:55 +01:00
|
|
|
|
2009-12-07 11:51:55 +01:00
|
|
|
void onForward (const DagInit& Dag,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(Dag, 1);
|
2009-12-07 11:51:55 +01: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 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(Dag, 2);
|
2009-12-07 11:51:55 +01: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 18:03:05 +01:00
|
|
|
void onForwardValue (const DagInit& Dag,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(Dag, 1);
|
2009-12-07 18:03:05 +01:00
|
|
|
const std::string& Name = InitPtrToString(Dag.getArg(0));
|
2009-12-07 20:16:13 +01:00
|
|
|
const OptionDescription& D = OptDescs.FindListOrParameter(Name);
|
2009-12-07 18:03:05 +01:00
|
|
|
|
|
|
|
if (D.isParameter()) {
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(IndentLevel) << "vec.push_back(std::make_pair("
|
|
|
|
<< D.GenVariableName() << ".getPosition(), "
|
2010-02-23 10:04:44 +01:00
|
|
|
<< D.GenVariableName() << "));\n";
|
2009-12-07 18:03:05 +01:00
|
|
|
}
|
2009-12-07 20:16:13 +01:00
|
|
|
else {
|
2010-02-23 10:04:44 +01:00
|
|
|
O.indent(IndentLevel) << "for (cl::list<std::string>::iterator B = "
|
|
|
|
<< D.GenVariableName() << ".begin(), \n";
|
|
|
|
O.indent(IndentLevel + Indent1) << " E = " << D.GenVariableName()
|
|
|
|
<< ".end(); B != E; ++B)\n";
|
|
|
|
O.indent(IndentLevel) << "{\n";
|
|
|
|
O.indent(IndentLevel + Indent1)
|
2010-02-23 10:04:57 +01: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 10:04:44 +01:00
|
|
|
O.indent(IndentLevel) << "}\n";
|
2009-12-07 18:03:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onForwardTransformedValue (const DagInit& Dag,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(Dag, 2);
|
2009-12-07 18:03:05 +01:00
|
|
|
const std::string& Name = InitPtrToString(Dag.getArg(0));
|
|
|
|
const std::string& Hook = InitPtrToString(Dag.getArg(1));
|
2009-12-07 20:16:13 +01:00
|
|
|
const OptionDescription& D = OptDescs.FindListOrParameter(Name);
|
2009-12-07 18:03:05 +01:00
|
|
|
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(IndentLevel) << "vec.push_back(std::make_pair("
|
|
|
|
<< D.GenVariableName() << ".getPosition("
|
|
|
|
<< (D.isList() ? "0" : "") << "), "
|
|
|
|
<< "hooks::" << Hook << "(" << D.GenVariableName()
|
2010-02-23 10:04:44 +01: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 18:03:05 +01:00
|
|
|
}
|
|
|
|
|
2009-12-07 11:51:55 +01:00
|
|
|
void onOutputSuffix (const DagInit& Dag,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(Dag, 1);
|
2009-12-15 08:20:50 +01:00
|
|
|
this->EmitHookInvocation(InitPtrToString(Dag.getArg(0)),
|
|
|
|
"output_suffix = ", ";\n", IndentLevel, O);
|
2009-12-07 11:51:55 +01: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 19:25:54 +01:00
|
|
|
throw "'unpack_values' is deprecated. "
|
|
|
|
"Use 'comma_separated' + 'forward_value' instead!";
|
2008-05-30 08:10:19 +02:00
|
|
|
}
|
2009-12-07 11:51:55 +01:00
|
|
|
|
2008-12-07 17:42:22 +01:00
|
|
|
public:
|
2009-12-07 11:51:55 +01: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 18:03:05 +01:00
|
|
|
AddHandler("forward_value", &EmitActionHandlersCallback::onForwardValue);
|
|
|
|
AddHandler("forward_transformed_value",
|
|
|
|
&EmitActionHandlersCallback::onForwardTransformedValue);
|
2010-02-23 10:04:44 +01:00
|
|
|
AddHandler("no_out_file",
|
|
|
|
&EmitActionHandlersCallback::onNoOutFile);
|
2009-12-07 11:51:55 +01:00
|
|
|
AddHandler("output_suffix", &EmitActionHandlersCallback::onOutputSuffix);
|
|
|
|
AddHandler("stop_compilation",
|
|
|
|
&EmitActionHandlersCallback::onStopCompilation);
|
|
|
|
AddHandler("unpack_values",
|
|
|
|
&EmitActionHandlersCallback::onUnpackValues);
|
|
|
|
|
2010-02-23 10:04:44 +01:00
|
|
|
|
2009-12-07 11:51:55 +01:00
|
|
|
staticMembersInitialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
2008-12-07 17:42:22 +01:00
|
|
|
|
2009-12-17 08:48:49 +01:00
|
|
|
void operator()(const Init* I,
|
2009-10-19 23:24:28 +02:00
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
2008-12-07 17:42:22 +01:00
|
|
|
{
|
2009-12-17 08:48:49 +01:00
|
|
|
InvokeDagInitHandler(this, I, IndentLevel, O);
|
2008-12-07 17:42:22 +01:00
|
|
|
}
|
2008-05-30 08:10:19 +02:00
|
|
|
};
|
|
|
|
|
2009-12-07 11:51:55 +01:00
|
|
|
void EmitGenerateActionMethodHeader(const ToolDescription& D,
|
|
|
|
bool IsJoin, raw_ostream& O)
|
|
|
|
{
|
2008-12-07 17:41:11 +01:00
|
|
|
if (IsJoin)
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
|
2008-05-30 08:10:19 +02:00
|
|
|
else
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
|
|
|
|
|
|
|
|
O.indent(Indent2) << "bool HasChildren,\n";
|
|
|
|
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";
|
|
|
|
O.indent(Indent2) << "std::string cmd;\n";
|
2010-02-23 10:04:44 +01:00
|
|
|
O.indent(Indent2) << "std::string out_file;\n";
|
|
|
|
O.indent(Indent2) << "std::vector<std::pair<unsigned, std::string> > vec;\n";
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
|
2010-02-23 10:04:44 +01:00
|
|
|
O.indent(Indent2) << "bool no_out_file = false;\n";
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent2) << "const char* output_suffix = \""
|
|
|
|
<< D.OutputSuffix << "\";\n";
|
2009-12-07 11:51:55 +01: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) {
|
|
|
|
|
|
|
|
EmitGenerateActionMethodHeader(D, IsJoin, O);
|
2008-05-30 08:10:19 +02:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
if (!D.CmdLine)
|
|
|
|
throw "Tool " + D.Name + " has no cmd_line property!";
|
2009-10-09 07:45:21 +02:00
|
|
|
|
2010-02-23 10:04:44 +01:00
|
|
|
// Process the 'command' property.
|
|
|
|
O << '\n';
|
|
|
|
EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
|
|
|
|
O << '\n';
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2010-02-23 10:04:44 +01:00
|
|
|
// Process the 'actions' list of this tool.
|
2009-10-08 06:40:08 +02:00
|
|
|
if (D.Actions)
|
2009-10-27 10:02:49 +01:00
|
|
|
EmitCaseConstructHandler(D.Actions, Indent2,
|
|
|
|
EmitActionHandlersCallback(OptDescs),
|
2009-10-08 06:40:08 +02:00
|
|
|
false, OptDescs, O);
|
|
|
|
O << '\n';
|
2009-10-09 07:45:21 +02:00
|
|
|
|
2010-02-23 10:04:44 +01:00
|
|
|
// Input file (s)
|
|
|
|
if (!D.InFileOption.empty()) {
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(Indent2)
|
|
|
|
<< "vec.push_back(std::make_pair(InputFilenames.getPosition(0), \""
|
|
|
|
<< D.InFileOption << "\");\n";
|
2010-02-23 10:04:44 +01: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 10:04:57 +01:00
|
|
|
O.indent(Indent3) << "vec.push_back(std::make_pair("
|
|
|
|
<< "InputFilenames.getPosition(B - inFiles.begin()), "
|
|
|
|
<< "B->str()));\n";
|
2010-02-23 10:04:44 +01:00
|
|
|
O.indent(Indent2) << "}\n";
|
|
|
|
}
|
|
|
|
else {
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(Indent2) << "vec.push_back(std::make_pair("
|
|
|
|
<< "InputFilenames.getPosition(0), inFile.str()));\n";
|
2010-02-23 10:04:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output file
|
|
|
|
O.indent(Indent2) << "if (!no_out_file) {\n";
|
|
|
|
if (!D.OutFileOption.empty())
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(Indent3) << "vec.push_back(std::make_pair(65536, \""
|
2010-02-23 10:04:44 +01:00
|
|
|
<< D.OutFileOption << "\"));\n";
|
|
|
|
|
|
|
|
O.indent(Indent3) << "out_file = this->OutFilename("
|
|
|
|
<< (IsJoin ? "sys::Path(),\n" : "inFile,\n");
|
|
|
|
O.indent(Indent4) << "TempDir, stop_compilation, output_suffix).str();\n\n";
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(Indent3) << "vec.push_back(std::make_pair(65536, out_file));\n";
|
2010-02-23 10:04:44 +01:00
|
|
|
|
|
|
|
O.indent(Indent2) << "}\n\n";
|
2009-10-08 06:40:08 +02:00
|
|
|
|
2008-05-09 10:27:26 +02:00
|
|
|
// Handle the Sink property.
|
2008-12-07 17:41:11 +01:00
|
|
|
if (D.isSink()) {
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
|
2010-02-23 10:04:44 +01:00
|
|
|
O.indent(Indent3) << "for (cl::list<std::string>::iterator B = "
|
|
|
|
<< SinkOptionName << ".begin(), E = " << SinkOptionName
|
|
|
|
<< ".end(); B != E; ++B)\n";
|
2010-02-23 10:04:57 +01:00
|
|
|
O.indent(Indent4) << "vec.push_back(std::make_pair(" << SinkOptionName
|
|
|
|
<< ".getPosition(B - " << SinkOptionName
|
|
|
|
<< ".begin()), *B));\n";
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent2) << "}\n";
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2010-02-23 10:04:44 +01:00
|
|
|
O.indent(Indent2) << "return Action(cmd, this->SortArgs(vec), "
|
|
|
|
<< "stop_compilation, out_file);\n";
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent1) << "}\n\n";
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2008-05-09 10:27:26 +02:00
|
|
|
/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
|
|
|
|
/// a given Tool class.
|
2008-12-07 17:41:11 +01:00
|
|
|
void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
|
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O) {
|
2009-09-21 17:53:44 +02:00
|
|
|
if (!ToolDesc.isJoin()) {
|
|
|
|
O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
|
|
|
|
O.indent(Indent2) << "bool HasChildren,\n";
|
|
|
|
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";
|
|
|
|
O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
|
|
|
|
<< " is not a Join tool!\");\n";
|
|
|
|
O.indent(Indent1) << "}\n\n";
|
|
|
|
}
|
|
|
|
else {
|
2008-12-07 17:41:11 +01:00
|
|
|
EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
|
2009-09-21 17:53:44 +02:00
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2008-05-07 23:50:19 +02:00
|
|
|
/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
|
|
|
|
/// methods for a given Tool class.
|
2009-07-03 02:10:29 +02:00
|
|
|
void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent1) << "const char** InputLanguages() const {\n";
|
|
|
|
O.indent(Indent2) << "return InputLanguages_;\n";
|
|
|
|
O.indent(Indent1) << "}\n\n";
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
if (D.OutLanguage.empty())
|
|
|
|
throw "Tool " + D.Name + " has no 'out_language' property!";
|
2008-05-30 08:13:02 +02:00
|
|
|
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent1) << "const char* OutputLanguage() const {\n";
|
|
|
|
O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
|
|
|
|
O.indent(Indent1) << "}\n\n";
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2008-05-07 23:50:19 +02:00
|
|
|
/// EmitNameMethod - Emit the Name() method for a given Tool class.
|
2009-07-03 02:10:29 +02:00
|
|
|
void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent1) << "const char* Name() const {\n";
|
|
|
|
O.indent(Indent2) << "return \"" << D.Name << "\";\n";
|
|
|
|
O.indent(Indent1) << "}\n\n";
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2008-05-07 23:50:19 +02:00
|
|
|
/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
|
|
|
|
/// class.
|
2009-07-03 02:10:29 +02:00
|
|
|
void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent1) << "bool IsJoin() const {\n";
|
2008-12-07 17:41:11 +01:00
|
|
|
if (D.isJoin())
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent2) << "return true;\n";
|
2008-03-23 09:57:20 +01:00
|
|
|
else
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent2) << "return false;\n";
|
|
|
|
O.indent(Indent1) << "}\n\n";
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2010-02-23 10:04:28 +01: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";
|
|
|
|
}
|
|
|
|
|
2008-05-30 08:24:49 +02:00
|
|
|
/// EmitStaticMemberDefinitions - Emit static member definitions for a
|
|
|
|
/// given Tool class.
|
2009-07-03 02:10:29 +02:00
|
|
|
void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
|
2008-12-07 17:41:11 +01:00
|
|
|
if (D.InLanguage.empty())
|
|
|
|
throw "Tool " + D.Name + " has no 'in_language' property!";
|
|
|
|
|
|
|
|
O << "const char* " << D.Name << "::InputLanguages_[] = {";
|
|
|
|
for (StrVector::const_iterator B = D.InLanguage.begin(),
|
|
|
|
E = D.InLanguage.end(); B != E; ++B)
|
2008-05-30 08:24:49 +02:00
|
|
|
O << '\"' << *B << "\", ";
|
|
|
|
O << "0};\n\n";
|
|
|
|
}
|
|
|
|
|
2008-05-07 23:50:19 +02:00
|
|
|
/// EmitToolClassDefinition - Emit a Tool class definition.
|
2008-12-07 17:41:11 +01:00
|
|
|
void EmitToolClassDefinition (const ToolDescription& D,
|
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O) {
|
2008-12-07 17:41:11 +01:00
|
|
|
if (D.Name == "root")
|
2008-05-06 18:35:25 +02:00
|
|
|
return;
|
|
|
|
|
2008-03-23 09:57:20 +01:00
|
|
|
// Header
|
2008-12-07 17:41:11 +01:00
|
|
|
O << "class " << D.Name << " : public ";
|
|
|
|
if (D.isJoin())
|
2008-05-06 19:26:53 +02:00
|
|
|
O << "JoinTool";
|
|
|
|
else
|
|
|
|
O << "Tool";
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2009-12-15 08:21:14 +01:00
|
|
|
O << " {\nprivate:\n";
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
|
2008-05-30 08:24:49 +02:00
|
|
|
|
|
|
|
O << "public:\n";
|
2008-12-07 17:41:11 +01:00
|
|
|
EmitNameMethod(D, O);
|
|
|
|
EmitInOutLanguageMethods(D, O);
|
|
|
|
EmitIsJoinMethod(D, O);
|
2010-02-23 10:04:28 +01:00
|
|
|
EmitWorksOnEmptyMethod(D, OptDescs, O);
|
2008-12-07 17:41:11 +01:00
|
|
|
EmitGenerateActionMethods(D, OptDescs, O);
|
2008-03-23 09:57:20 +01:00
|
|
|
|
|
|
|
// Close class definition
|
2008-05-30 08:24:49 +02:00
|
|
|
O << "};\n";
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
EmitStaticMemberDefinitions(D, O);
|
2008-05-30 08:24:49 +02:00
|
|
|
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2009-06-23 22:45:07 +02:00
|
|
|
/// EmitOptionDefinitions - Iterate over a list of option descriptions
|
2008-12-07 17:41:11 +01:00
|
|
|
/// and emit registration code.
|
2009-06-23 22:45:07 +02:00
|
|
|
void EmitOptionDefinitions (const OptionDescriptions& descs,
|
|
|
|
bool HasSink, bool HasExterns,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O)
|
2008-03-23 09:57:20 +01:00
|
|
|
{
|
2008-12-07 17:41:11 +01:00
|
|
|
std::vector<OptionDescription> Aliases;
|
2008-05-30 08:22:52 +02:00
|
|
|
|
2008-05-30 08:23:29 +02:00
|
|
|
// Emit static cl::Option variables.
|
2008-12-07 17:41:11 +01:00
|
|
|
for (OptionDescriptions::const_iterator B = descs.begin(),
|
2008-03-23 09:57:20 +01:00
|
|
|
E = descs.end(); B!=E; ++B) {
|
2008-12-07 17:41:11 +01:00
|
|
|
const OptionDescription& val = B->second;
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-05-30 08:22:52 +02:00
|
|
|
if (val.Type == OptionType::Alias) {
|
|
|
|
Aliases.push_back(val);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
if (val.isExtern())
|
|
|
|
O << "extern ";
|
|
|
|
|
2008-03-23 09:57:20 +01:00
|
|
|
O << val.GenTypeDeclaration() << ' '
|
2008-12-07 17:41:11 +01:00
|
|
|
<< val.GenVariableName();
|
|
|
|
|
|
|
|
if (val.isExtern()) {
|
|
|
|
O << ";\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-06-23 22:45:31 +02:00
|
|
|
O << "(\"" << val.Name << "\"\n";
|
2008-03-23 09:57:20 +01:00
|
|
|
|
|
|
|
if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
|
|
|
|
O << ", cl::Prefix";
|
|
|
|
|
|
|
|
if (val.isRequired()) {
|
2009-07-07 18:08:11 +02:00
|
|
|
if (val.isList() && !val.isMultiVal())
|
2008-03-23 09:57:20 +01:00
|
|
|
O << ", cl::OneOrMore";
|
2009-01-28 04:47:20 +01:00
|
|
|
else
|
2008-03-23 09:57:20 +01:00
|
|
|
O << ", cl::Required";
|
2009-01-28 04:47:20 +01:00
|
|
|
}
|
2010-03-05 05:46:39 +01:00
|
|
|
|
|
|
|
if (val.isOptional())
|
2009-12-15 04:04:52 +01:00
|
|
|
O << ", cl::Optional";
|
2010-03-05 05:46:39 +01:00
|
|
|
|
|
|
|
if (val.isOneOrMore())
|
|
|
|
O << ", cl::OneOrMore";
|
|
|
|
|
|
|
|
if (val.isZeroOrMore())
|
|
|
|
O << ", cl::ZeroOrMore";
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2009-12-07 19:25:54 +01:00
|
|
|
if (val.isReallyHidden())
|
2009-01-28 04:47:20 +01:00
|
|
|
O << ", cl::ReallyHidden";
|
2009-12-07 19:25:54 +01:00
|
|
|
else if (val.isHidden())
|
2009-01-28 04:47:20 +01:00
|
|
|
O << ", cl::Hidden";
|
2009-12-07 19:25:54 +01:00
|
|
|
|
|
|
|
if (val.isCommaSeparated())
|
|
|
|
O << ", cl::CommaSeparated";
|
2008-11-28 01:13:25 +01:00
|
|
|
|
2009-01-28 04:47:20 +01:00
|
|
|
if (val.MultiVal > 1)
|
2009-07-07 18:08:41 +02:00
|
|
|
O << ", cl::multi_val(" << val.MultiVal << ')';
|
|
|
|
|
|
|
|
if (val.InitVal) {
|
|
|
|
const std::string& str = val.InitVal->getAsString();
|
|
|
|
O << ", cl::init(" << str << ')';
|
|
|
|
}
|
2009-01-28 04:47:20 +01:00
|
|
|
|
2008-05-30 08:22:52 +02:00
|
|
|
if (!val.Help.empty())
|
|
|
|
O << ", cl::desc(\"" << val.Help << "\")";
|
|
|
|
|
2009-06-23 22:45:31 +02:00
|
|
|
O << ");\n\n";
|
2008-05-30 08:22:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the aliases (they should go after all the 'proper' options).
|
2008-12-07 17:41:11 +01:00
|
|
|
for (std::vector<OptionDescription>::const_iterator
|
2008-05-30 08:22:52 +02:00
|
|
|
B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
|
2008-12-07 17:41:11 +01:00
|
|
|
const OptionDescription& val = *B;
|
2008-05-30 08:22:52 +02:00
|
|
|
|
|
|
|
O << val.GenTypeDeclaration() << ' '
|
|
|
|
<< val.GenVariableName()
|
|
|
|
<< "(\"" << val.Name << '\"';
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
const OptionDescription& D = descs.FindOption(val.Help);
|
|
|
|
O << ", cl::aliasopt(" << D.GenVariableName() << ")";
|
2008-05-30 08:22:52 +02:00
|
|
|
|
|
|
|
O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2008-05-30 08:22:52 +02:00
|
|
|
// Emit the sink option.
|
2008-12-07 17:41:11 +01:00
|
|
|
if (HasSink)
|
2008-12-07 17:42:47 +01:00
|
|
|
O << (HasExterns ? "extern cl" : "cl")
|
|
|
|
<< "::list<std::string> " << SinkOptionName
|
|
|
|
<< (HasExterns ? ";\n" : "(cl::Sink);\n");
|
2008-03-23 09:57:20 +01:00
|
|
|
|
|
|
|
O << '\n';
|
|
|
|
}
|
|
|
|
|
2009-10-19 23:24:28 +02:00
|
|
|
/// EmitPreprocessOptionsCallback - Helper function passed to
|
2009-10-17 22:09:29 +02:00
|
|
|
/// EmitCaseConstructHandler() by EmitPreprocessOptions().
|
2009-12-17 08:48:49 +01: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 08:49:16 +01:00
|
|
|
typedef void
|
|
|
|
(EmitPreprocessOptionsCallback::* HandlerImpl)
|
|
|
|
(const Init*, unsigned, raw_ostream&) const;
|
2009-12-17 08:48:49 +01:00
|
|
|
|
2009-10-17 22:09:29 +02:00
|
|
|
const OptionDescriptions& OptDescs_;
|
|
|
|
|
2009-12-23 13:49:30 +01:00
|
|
|
void onListOrDag(const DagInit& d, HandlerImpl h,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
2009-12-17 08:49:16 +01:00
|
|
|
{
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 08:49:16 +01:00
|
|
|
const Init* I = d.getArg(0);
|
|
|
|
|
|
|
|
// If I is a list, apply h to each element.
|
|
|
|
if (typeid(*I) == typeid(ListInit)) {
|
|
|
|
const ListInit& L = *static_cast<const ListInit*>(I);
|
|
|
|
for (ListInit::const_iterator B = L.begin(), E = L.end(); B != E; ++B)
|
|
|
|
((this)->*(h))(*B, IndentLevel, O);
|
|
|
|
}
|
|
|
|
// Otherwise, apply h to I.
|
|
|
|
else {
|
|
|
|
((this)->*(h))(I, IndentLevel, O);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onUnsetOptionImpl(const Init* I,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
2009-12-17 08:48:49 +01:00
|
|
|
{
|
|
|
|
const std::string& OptName = InitPtrToString(I);
|
2009-10-17 22:09:29 +02:00
|
|
|
const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
|
|
|
|
|
2009-10-22 06:15:07 +02:00
|
|
|
if (OptDesc.isSwitch()) {
|
2009-10-17 22:09:29 +02:00
|
|
|
O.indent(IndentLevel) << OptDesc.GenVariableName() << " = false;\n";
|
|
|
|
}
|
2009-10-22 06:15:07 +02:00
|
|
|
else if (OptDesc.isParameter()) {
|
2009-10-17 22:09:29 +02:00
|
|
|
O.indent(IndentLevel) << OptDesc.GenVariableName() << " = \"\";\n";
|
|
|
|
}
|
2009-10-22 06:15:07 +02:00
|
|
|
else if (OptDesc.isList()) {
|
|
|
|
O.indent(IndentLevel) << OptDesc.GenVariableName() << ".clear();\n";
|
|
|
|
}
|
2009-10-17 22:09:29 +02:00
|
|
|
else {
|
2009-12-07 18:03:05 +01:00
|
|
|
throw "Can't apply 'unset_option' to alias option '" + OptName + "'!";
|
2009-10-17 22:09:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-17 08:49:16 +01:00
|
|
|
void onUnsetOption(const DagInit& d,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
2009-10-17 22:09:29 +02:00
|
|
|
{
|
2009-12-23 13:49:30 +01:00
|
|
|
this->onListOrDag(d, &EmitPreprocessOptionsCallback::onUnsetOptionImpl,
|
|
|
|
IndentLevel, O);
|
2009-12-17 08:48:49 +01:00
|
|
|
}
|
2009-10-17 22:09:29 +02:00
|
|
|
|
2009-12-23 13:49:30 +01:00
|
|
|
void onSetOptionImpl(const DagInit& d,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 2);
|
2009-12-17 08:49:16 +01:00
|
|
|
const std::string& OptName = InitPtrToString(d.getArg(0));
|
2009-12-18 12:27:26 +01:00
|
|
|
const Init* Value = d.getArg(1);
|
2009-12-17 08:49:16 +01:00
|
|
|
const OptionDescription& OptDesc = OptDescs_.FindOption(OptName);
|
|
|
|
|
2009-12-18 12:27:26 +01: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) {
|
|
|
|
O.indent(IndentLevel) << OptDesc.GenVariableName() << ".push_back(\""
|
|
|
|
<< InitPtrToString(*B) << "\");\n";
|
|
|
|
}
|
|
|
|
}
|
2009-12-23 13:49:30 +01:00
|
|
|
else if (OptDesc.isSwitch()) {
|
|
|
|
CheckBooleanConstant(Value);
|
|
|
|
O.indent(IndentLevel) << OptDesc.GenVariableName()
|
|
|
|
<< " = " << Value->getAsString() << ";\n";
|
|
|
|
}
|
2009-12-18 12:27:26 +01:00
|
|
|
else if (OptDesc.isParameter()) {
|
|
|
|
const std::string& Str = InitPtrToString(Value);
|
2009-12-17 08:49:16 +01:00
|
|
|
O.indent(IndentLevel) << OptDesc.GenVariableName()
|
2009-12-18 12:27:26 +01:00
|
|
|
<< " = \"" << Str << "\";\n";
|
|
|
|
}
|
|
|
|
else {
|
2009-12-23 13:49:30 +01:00
|
|
|
throw "Can't apply 'set_option' to alias option -" + OptName + " !";
|
2009-12-18 12:27:26 +01:00
|
|
|
}
|
2009-12-17 08:49:16 +01: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 12:27:26 +01:00
|
|
|
throw "set_option: -" + OptName + " is not a switch option!";
|
2009-12-17 08:49:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void onSetOption(const DagInit& d,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
2009-12-17 08:48:49 +01:00
|
|
|
{
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-12-17 08:48:49 +01:00
|
|
|
|
2009-12-23 13:49:30 +01:00
|
|
|
// Two arguments: (set_option "parameter", VALUE), where VALUE can be a
|
|
|
|
// boolean, a string or a string list.
|
2009-12-17 08:49:16 +01:00
|
|
|
if (d.getNumArgs() > 1)
|
2009-12-23 13:49:30 +01:00
|
|
|
this->onSetOptionImpl(d, IndentLevel, O);
|
2009-12-17 08:49:16 +01:00
|
|
|
// One argument: (set_option "switch")
|
|
|
|
// or (set_option ["switch1", "switch2", ...])
|
|
|
|
else
|
2009-12-23 13:49:30 +01:00
|
|
|
this->onListOrDag(d, &EmitPreprocessOptionsCallback::onSetSwitch,
|
|
|
|
IndentLevel, O);
|
2009-10-17 22:09:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2009-10-19 23:24:28 +02:00
|
|
|
EmitPreprocessOptionsCallback(const OptionDescriptions& OptDescs)
|
2009-10-17 22:09:29 +02:00
|
|
|
: OptDescs_(OptDescs)
|
2009-12-17 08:48:49 +01:00
|
|
|
{
|
|
|
|
if (!staticMembersInitialized_) {
|
|
|
|
AddHandler("error", &EmitPreprocessOptionsCallback::onErrorDag);
|
|
|
|
AddHandler("warning", &EmitPreprocessOptionsCallback::onWarningDag);
|
|
|
|
AddHandler("unset_option", &EmitPreprocessOptionsCallback::onUnsetOption);
|
2009-12-17 08:49:16 +01:00
|
|
|
AddHandler("set_option", &EmitPreprocessOptionsCallback::onSetOption);
|
2009-12-17 08:48:49 +01:00
|
|
|
|
|
|
|
staticMembersInitialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(const Init* I,
|
|
|
|
unsigned IndentLevel, raw_ostream& O) const
|
|
|
|
{
|
|
|
|
InvokeDagInitHandler(this, I, IndentLevel, O);
|
|
|
|
}
|
|
|
|
|
2009-10-17 22:09:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// EmitPreprocessOptions - Emit the PreprocessOptionsLocal() function.
|
|
|
|
void EmitPreprocessOptions (const RecordKeeper& Records,
|
|
|
|
const OptionDescriptions& OptDecs, raw_ostream& O)
|
|
|
|
{
|
|
|
|
O << "void PreprocessOptionsLocal() {\n";
|
|
|
|
|
|
|
|
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 23:24:28 +02:00
|
|
|
EmitCaseConstructHandler(Case, Indent1,
|
|
|
|
EmitPreprocessOptionsCallback(OptDecs),
|
2009-10-17 22:09:29 +02:00
|
|
|
false, OptDecs, O);
|
|
|
|
}
|
|
|
|
|
|
|
|
O << "}\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitPopulateLanguageMap - Emit the PopulateLanguageMapLocal() function.
|
2009-07-03 02:10:29 +02:00
|
|
|
void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
|
2008-03-23 09:57:20 +01:00
|
|
|
{
|
2008-11-17 18:29:18 +01:00
|
|
|
O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
|
|
|
|
|
2008-03-23 09:57:20 +01:00
|
|
|
// Get the relevant field out of RecordKeeper
|
2008-11-17 18:29:42 +01:00
|
|
|
const Record* LangMapRecord = Records.getDef("LanguageMap");
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-11-17 18:29:18 +01:00
|
|
|
// It is allowed for a plugin to have no language map.
|
|
|
|
if (LangMapRecord) {
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-11-17 18:29:18 +01:00
|
|
|
ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
|
|
|
|
if (!LangsToSuffixesList)
|
2009-12-07 20:15:57 +01:00
|
|
|
throw "Error in the language map definition!";
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-11-17 18:29:18 +01:00
|
|
|
for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
|
2008-11-17 18:29:42 +01:00
|
|
|
const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-11-17 18:29:18 +01:00
|
|
|
const std::string& Lang = LangToSuffixes->getValueAsString("lang");
|
|
|
|
const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-11-17 18:29:18 +01:00
|
|
|
for (unsigned i = 0; i < Suffixes->size(); ++i)
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent1) << "langMap[\""
|
|
|
|
<< InitPtrToString(Suffixes->getElement(i))
|
|
|
|
<< "\"] = \"" << Lang << "\";\n";
|
2008-11-17 18:29:18 +01:00
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2008-12-11 11:34:18 +01:00
|
|
|
O << "}\n\n";
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2008-05-30 08:12:24 +02:00
|
|
|
/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
|
|
|
|
/// by EmitEdgeClass().
|
2009-09-21 17:53:44 +02:00
|
|
|
void IncDecWeight (const Init* i, unsigned IndentLevel,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O) {
|
2008-05-30 08:18:16 +02:00
|
|
|
const DagInit& d = InitPtrToDag(i);
|
2009-10-19 00:51:30 +02:00
|
|
|
const std::string& OpName = GetOperatorName(d);
|
2008-05-30 08:08:50 +02:00
|
|
|
|
2008-12-17 03:47:01 +01:00
|
|
|
if (OpName == "inc_weight") {
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(IndentLevel) << "ret += ";
|
2008-12-17 03:47:01 +01:00
|
|
|
}
|
|
|
|
else if (OpName == "dec_weight") {
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(IndentLevel) << "ret -= ";
|
2008-12-17 03:47:01 +01:00
|
|
|
}
|
|
|
|
else if (OpName == "error") {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(d, 1);
|
2009-10-17 22:09:29 +02:00
|
|
|
O.indent(IndentLevel) << "throw std::runtime_error(\""
|
|
|
|
<< InitPtrToString(d.getArg(0))
|
|
|
|
<< "\");\n";
|
2008-12-17 03:47:01 +01:00
|
|
|
return;
|
|
|
|
}
|
2009-10-17 22:09:29 +02:00
|
|
|
else {
|
|
|
|
throw "Unknown operator in edge properties list: '" + OpName + "'!"
|
2008-12-18 05:06:58 +01:00
|
|
|
"\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
|
2009-10-17 22:09:29 +02:00
|
|
|
}
|
2008-05-30 08:08:50 +02:00
|
|
|
|
|
|
|
if (d.getNumArgs() > 0)
|
|
|
|
O << InitPtrToInt(d.getArg(0)) << ";\n";
|
|
|
|
else
|
|
|
|
O << "2;\n";
|
|
|
|
|
2008-05-06 20:18:20 +02:00
|
|
|
}
|
|
|
|
|
2008-05-07 23:50:19 +02:00
|
|
|
/// EmitEdgeClass - Emit a single Edge# class.
|
2008-05-30 08:10:19 +02:00
|
|
|
void EmitEdgeClass (unsigned N, const std::string& Target,
|
2008-12-07 17:41:11 +01:00
|
|
|
DagInit* Case, const OptionDescriptions& OptDescs,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O) {
|
2008-05-06 19:23:14 +02:00
|
|
|
|
|
|
|
// Class constructor.
|
|
|
|
O << "class Edge" << N << ": public Edge {\n"
|
2009-09-21 17:53:44 +02:00
|
|
|
<< "public:\n";
|
|
|
|
O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
|
|
|
|
<< "\") {}\n\n";
|
2008-05-06 19:23:14 +02:00
|
|
|
|
2008-05-06 20:14:24 +02:00
|
|
|
// Function Weight().
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent1)
|
|
|
|
<< "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
|
|
|
|
O.indent(Indent2) << "unsigned ret = 0;\n";
|
2008-05-06 19:23:14 +02:00
|
|
|
|
2008-05-30 08:08:50 +02:00
|
|
|
// Handle the 'case' construct.
|
2008-05-30 08:22:15 +02:00
|
|
|
EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
|
2008-05-06 20:14:24 +02:00
|
|
|
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent2) << "return ret;\n";
|
2009-12-24 18:49:28 +01:00
|
|
|
O.indent(Indent1) << "}\n\n};\n\n";
|
2008-05-06 19:23:14 +02:00
|
|
|
}
|
|
|
|
|
2008-05-30 08:12:24 +02:00
|
|
|
/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
|
2008-11-28 01:13:47 +01:00
|
|
|
void EmitEdgeClasses (const RecordVector& EdgeVector,
|
2008-12-07 17:41:11 +01:00
|
|
|
const OptionDescriptions& OptDescs,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O) {
|
2008-11-28 01:13:47 +01:00
|
|
|
int i = 0;
|
2008-12-07 17:44:47 +01:00
|
|
|
for (RecordVector::const_iterator B = EdgeVector.begin(),
|
|
|
|
E = EdgeVector.end(); B != E; ++B) {
|
|
|
|
const Record* Edge = *B;
|
|
|
|
const std::string& NodeB = Edge->getValueAsString("b");
|
2009-12-17 08:48:49 +01:00
|
|
|
DagInit& Weight = *Edge->getValueAsDag("weight");
|
2008-05-06 18:36:50 +02:00
|
|
|
|
2009-12-23 13:49:41 +01:00
|
|
|
if (!IsDagEmpty(Weight))
|
2009-12-17 08:48:49 +01:00
|
|
|
EmitEdgeClass(i, NodeB, &Weight, OptDescs, O);
|
2008-11-28 01:13:47 +01:00
|
|
|
++i;
|
2008-05-06 18:36:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-17 22:09:29 +02:00
|
|
|
/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraphLocal()
|
2008-05-07 23:50:19 +02:00
|
|
|
/// function.
|
2008-11-28 01:13:47 +01:00
|
|
|
void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
|
2008-12-07 17:41:11 +01:00
|
|
|
const ToolDescriptions& ToolDescs,
|
2009-07-03 02:10:29 +02:00
|
|
|
raw_ostream& O)
|
2008-03-23 09:57:20 +01:00
|
|
|
{
|
2008-09-22 22:49:34 +02:00
|
|
|
O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
|
2008-05-06 18:35:25 +02:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
|
|
|
|
E = ToolDescs.end(); B != E; ++B)
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
|
2008-11-12 01:05:17 +01:00
|
|
|
|
2008-05-06 18:35:25 +02:00
|
|
|
O << '\n';
|
|
|
|
|
2008-11-12 01:05:17 +01:00
|
|
|
// Insert edges.
|
|
|
|
|
2008-11-28 01:13:47 +01:00
|
|
|
int i = 0;
|
2008-12-07 17:44:47 +01:00
|
|
|
for (RecordVector::const_iterator B = EdgeVector.begin(),
|
|
|
|
E = EdgeVector.end(); B != E; ++B) {
|
|
|
|
const Record* Edge = *B;
|
|
|
|
const std::string& NodeA = Edge->getValueAsString("a");
|
|
|
|
const std::string& NodeB = Edge->getValueAsString("b");
|
2009-12-17 08:48:49 +01:00
|
|
|
DagInit& Weight = *Edge->getValueAsDag("weight");
|
2008-05-06 18:36:50 +02:00
|
|
|
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
|
2008-05-06 18:36:50 +02:00
|
|
|
|
2009-12-23 13:49:41 +01:00
|
|
|
if (IsDagEmpty(Weight))
|
2008-12-07 17:44:47 +01:00
|
|
|
O << "new SimpleEdge(\"" << NodeB << "\")";
|
2008-05-06 18:36:50 +02:00
|
|
|
else
|
|
|
|
O << "new Edge" << i << "()";
|
|
|
|
|
|
|
|
O << ");\n";
|
2008-11-28 01:13:47 +01:00
|
|
|
++i;
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2008-12-11 11:34:18 +01:00
|
|
|
O << "}\n\n";
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|
|
|
|
|
2009-12-07 18:03:05 +01: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 08:12:24 +02:00
|
|
|
/// ExtractHookNames - Extract the hook names from all instances of
|
2009-12-07 18:03:05 +01:00
|
|
|
/// $CALL(HookName) in the provided command line string/action. Helper
|
2008-05-30 08:12:24 +02:00
|
|
|
/// function used by FillInHookNames().
|
2008-12-07 17:41:11 +01:00
|
|
|
class ExtractHookNames {
|
2009-12-07 18:03:05 +01:00
|
|
|
HookInfoMap& HookNames_;
|
|
|
|
const OptionDescriptions& OptDescs_;
|
2008-12-07 17:41:11 +01:00
|
|
|
public:
|
2009-12-07 18:03:05 +01:00
|
|
|
ExtractHookNames(HookInfoMap& HookNames, const OptionDescriptions& OptDescs)
|
|
|
|
: HookNames_(HookNames), OptDescs_(OptDescs)
|
|
|
|
{}
|
2008-12-07 17:41:11 +01:00
|
|
|
|
2009-12-07 18:03:05 +01:00
|
|
|
void onAction (const DagInit& Dag) {
|
2009-12-15 08:20:50 +01:00
|
|
|
const std::string& Name = GetOperatorName(Dag);
|
|
|
|
|
|
|
|
if (Name == "forward_transformed_value") {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(Dag, 2);
|
2009-12-07 18:03:05 +01:00
|
|
|
const std::string& OptName = InitPtrToString(Dag.getArg(0));
|
|
|
|
const std::string& HookName = InitPtrToString(Dag.getArg(1));
|
|
|
|
const OptionDescription& D = OptDescs_.FindOption(OptName);
|
2009-10-19 00:51:30 +02:00
|
|
|
|
2009-12-07 18:03:05 +01:00
|
|
|
HookNames_[HookName] = HookInfo(D.isList() ? HookInfo::ListHook
|
|
|
|
: HookInfo::ArgHook);
|
|
|
|
}
|
2009-12-15 08:20:50 +01:00
|
|
|
else if (Name == "append_cmd" || Name == "output_suffix") {
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckNumberOfArguments(Dag, 1);
|
2009-12-15 08:20:50 +01:00
|
|
|
this->onCmdLine(InitPtrToString(Dag.getArg(0)));
|
2009-12-07 18:03:05 +01:00
|
|
|
}
|
2009-12-15 08:20:50 +01:00
|
|
|
}
|
2009-10-19 00:51:30 +02:00
|
|
|
|
2009-12-15 08:20:50 +01:00
|
|
|
void onCmdLine(const std::string& Cmd) {
|
2009-12-07 18:03:05 +01:00
|
|
|
StrVector cmds;
|
2009-12-17 08:48:34 +01:00
|
|
|
TokenizeCmdLine(Cmd, cmds);
|
2009-12-15 08:20:50 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
|
|
|
|
B != E; ++B) {
|
|
|
|
const std::string& cmd = *B;
|
2009-01-21 14:04:00 +01:00
|
|
|
|
|
|
|
if (cmd == "$CALL") {
|
|
|
|
unsigned NumArgs = 0;
|
2009-12-23 13:49:41 +01:00
|
|
|
CheckedIncrement(B, E, "Syntax error in $CALL invocation!");
|
2009-01-21 14:04:00 +01: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 18:03:05 +01:00
|
|
|
HookInfoMap::const_iterator H = HookNames_.find(HookName);
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-07 18:03:05 +01:00
|
|
|
if (H != HookNames_.end() && H->second.NumArgs != NumArgs &&
|
|
|
|
H->second.Type != HookInfo::ArgHook)
|
2009-01-21 14:04:00 +01:00
|
|
|
throw "Overloading of hooks is not allowed. Overloaded hook: "
|
|
|
|
+ HookName;
|
|
|
|
else
|
2009-12-07 18:03:05 +01:00
|
|
|
HookNames_[HookName] = HookInfo(NumArgs);
|
2009-12-15 08:20:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(const Init* Arg) {
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-15 08:20:50 +01: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 17:41:11 +01:00
|
|
|
}
|
2009-12-15 08:20:50 +01:00
|
|
|
return;
|
2008-05-30 08:12:24 +02:00
|
|
|
}
|
2009-12-15 08:20:50 +01:00
|
|
|
|
|
|
|
// We're invoked on a command line.
|
|
|
|
this->onCmdLine(InitPtrToString(Arg));
|
2008-05-30 08:12:24 +02:00
|
|
|
}
|
2009-10-19 00:51:30 +02:00
|
|
|
|
|
|
|
void operator()(const DagInit* Test, unsigned, bool) {
|
|
|
|
this->operator()(Test);
|
|
|
|
}
|
|
|
|
void operator()(const Init* Statement, unsigned) {
|
|
|
|
this->operator()(Statement);
|
|
|
|
}
|
2008-12-07 17:41:11 +01:00
|
|
|
};
|
2008-05-30 08:22:15 +02:00
|
|
|
|
2008-05-30 08:12:24 +02:00
|
|
|
/// FillInHookNames - Actually extract the hook names from all command
|
|
|
|
/// line strings. Helper function used by EmitHookDeclarations().
|
2008-12-07 17:41:11 +01:00
|
|
|
void FillInHookNames(const ToolDescriptions& ToolDescs,
|
2009-12-07 18:03:05 +01:00
|
|
|
const OptionDescriptions& OptDescs,
|
|
|
|
HookInfoMap& HookNames)
|
2008-12-07 17:41:11 +01:00
|
|
|
{
|
2009-12-07 18:03:05 +01:00
|
|
|
// For all tool descriptions:
|
2008-12-07 17:41:11 +01:00
|
|
|
for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
|
|
|
|
E = ToolDescs.end(); B != E; ++B) {
|
|
|
|
const ToolDescription& D = *(*B);
|
2009-12-07 18:03:05 +01: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 17:41:11 +01:00
|
|
|
if (!D.CmdLine)
|
2008-05-30 08:12:24 +02:00
|
|
|
continue;
|
2008-12-07 17:41:11 +01:00
|
|
|
if (dynamic_cast<StringInit*>(D.CmdLine))
|
2008-05-30 08:12:24 +02:00
|
|
|
// This is a string.
|
2009-12-07 18:03:05 +01:00
|
|
|
ExtractHookNames(HookNames, OptDescs).operator()(D.CmdLine);
|
2008-05-30 08:22:15 +02:00
|
|
|
else
|
2008-05-30 08:12:24 +02:00
|
|
|
// This is a 'case' construct.
|
2009-12-07 18:03:05 +01:00
|
|
|
WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames, OptDescs));
|
2008-05-30 08:12:24 +02: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 18:03:05 +01:00
|
|
|
void EmitHookDeclarations(const ToolDescriptions& ToolDescs,
|
|
|
|
const OptionDescriptions& OptDescs, raw_ostream& O) {
|
|
|
|
HookInfoMap HookNames;
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-07 18:03:05 +01:00
|
|
|
FillInHookNames(ToolDescs, OptDescs, HookNames);
|
2008-05-30 08:12:24 +02:00
|
|
|
if (HookNames.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
O << "namespace hooks {\n";
|
2009-12-07 18:03:05 +01:00
|
|
|
for (HookInfoMap::const_iterator B = HookNames.begin(),
|
2009-01-21 14:04:00 +01:00
|
|
|
E = HookNames.end(); B != E; ++B) {
|
2009-12-07 18:03:05 +01:00
|
|
|
const char* HookName = B->first();
|
|
|
|
const HookInfo& Info = B->second;
|
|
|
|
|
|
|
|
O.indent(Indent1) << "std::string " << HookName << "(";
|
2009-01-21 14:04:00 +01:00
|
|
|
|
2009-12-07 18:03:05 +01: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 14:04:00 +01:00
|
|
|
}
|
2008-05-30 08:12:24 +02:00
|
|
|
|
2009-01-21 14:04:00 +01:00
|
|
|
O <<");\n";
|
|
|
|
}
|
2008-05-30 08:12:24 +02:00
|
|
|
O << "}\n\n";
|
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-09-22 22:49:34 +02:00
|
|
|
/// EmitRegisterPlugin - Emit code to register this plugin.
|
2009-07-03 02:10:29 +02:00
|
|
|
void EmitRegisterPlugin(int Priority, raw_ostream& O) {
|
2009-09-21 17:53:44 +02:00
|
|
|
O << "struct Plugin : public llvmc::BasePlugin {\n\n";
|
|
|
|
O.indent(Indent1) << "int Priority() const { return "
|
|
|
|
<< Priority << "; }\n\n";
|
2009-10-17 22:09:29 +02:00
|
|
|
O.indent(Indent1) << "void PreprocessOptions() const\n";
|
|
|
|
O.indent(Indent1) << "{ PreprocessOptionsLocal(); }\n\n";
|
2009-09-21 17:53:44 +02:00
|
|
|
O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
|
|
|
|
O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
|
|
|
|
O.indent(Indent1)
|
|
|
|
<< "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
|
|
|
|
O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
|
|
|
|
<< "};\n\n"
|
|
|
|
<< "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
|
2008-09-22 22:49:34 +02:00
|
|
|
}
|
|
|
|
|
2008-11-12 13:41:18 +01:00
|
|
|
/// EmitIncludes - Emit necessary #include directives and some
|
|
|
|
/// additional declarations.
|
2009-07-03 02:10:29 +02:00
|
|
|
void EmitIncludes(raw_ostream& O) {
|
2009-09-28 03:16:42 +02:00
|
|
|
O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
|
|
|
|
<< "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
|
2009-06-23 22:46:48 +02:00
|
|
|
<< "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
|
2008-09-22 22:50:40 +02:00
|
|
|
<< "#include \"llvm/CompilerDriver/Plugin.h\"\n"
|
|
|
|
<< "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
|
2008-09-22 22:49:34 +02:00
|
|
|
|
2009-10-17 22:09:29 +02:00
|
|
|
<< "#include \"llvm/Support/CommandLine.h\"\n"
|
|
|
|
<< "#include \"llvm/Support/raw_ostream.h\"\n\n"
|
2008-09-22 22:49:34 +02:00
|
|
|
|
2009-12-07 18:03:05 +01:00
|
|
|
<< "#include <algorithm>\n"
|
2008-09-22 22:49:34 +02:00
|
|
|
<< "#include <cstdlib>\n"
|
2009-12-07 18:03:05 +01:00
|
|
|
<< "#include <iterator>\n"
|
2008-09-22 22:49:34 +02:00
|
|
|
<< "#include <stdexcept>\n\n"
|
|
|
|
|
|
|
|
<< "using namespace llvm;\n"
|
|
|
|
<< "using namespace llvmc;\n\n"
|
|
|
|
|
2008-11-12 13:41:18 +01:00
|
|
|
<< "extern cl::opt<std::string> OutputFilename;\n\n"
|
|
|
|
|
|
|
|
<< "inline const char* checkCString(const char* s)\n"
|
|
|
|
<< "{ return s == NULL ? \"\" : s; }\n\n";
|
2008-09-22 22:49:34 +02:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:42 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// PluginData - Holds all information about a plugin.
|
|
|
|
struct PluginData {
|
|
|
|
OptionDescriptions OptDescs;
|
|
|
|
bool HasSink;
|
2008-12-07 17:42:47 +01:00
|
|
|
bool HasExterns;
|
2008-12-07 17:41:11 +01:00
|
|
|
ToolDescriptions ToolDescs;
|
|
|
|
RecordVector Edges;
|
|
|
|
int Priority;
|
2008-11-17 18:29:42 +01:00
|
|
|
};
|
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// HasSink - Go through the list of tool descriptions and check if
|
2008-12-07 17:42:47 +01:00
|
|
|
/// there are any with the 'sink' property set.
|
2008-12-07 17:41:11 +01: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 01:13:47 +01:00
|
|
|
|
2008-12-07 17:42:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HasExterns - Go through the list of option descriptions and check
|
|
|
|
/// if there are any external options.
|
|
|
|
bool HasExterns(const OptionDescriptions& OptDescs) {
|
|
|
|
for (OptionDescriptions::const_iterator B = OptDescs.begin(),
|
|
|
|
E = OptDescs.end(); B != E; ++B)
|
|
|
|
if (B->second.isExtern())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2008-05-06 20:12:03 +02:00
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
/// CollectPluginData - Collect tool and option properties,
|
|
|
|
/// compilation graph edges and plugin priority from the parse tree.
|
|
|
|
void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
|
2008-11-28 01:13:47 +01:00
|
|
|
// Collect option properties.
|
2008-11-17 18:30:25 +01:00
|
|
|
const RecordVector& OptionLists =
|
|
|
|
Records.getAllDerivedDefinitions("OptionList");
|
2008-12-07 17:41:11 +01:00
|
|
|
CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
|
|
|
|
Data.OptDescs);
|
|
|
|
|
|
|
|
// Collect tool properties.
|
|
|
|
const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
|
|
|
|
CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
|
|
|
|
Data.HasSink = HasSink(Data.ToolDescs);
|
2008-12-07 17:42:47 +01:00
|
|
|
Data.HasExterns = HasExterns(Data.OptDescs);
|
2008-11-17 18:29:42 +01:00
|
|
|
|
2008-11-28 01:13:47 +01:00
|
|
|
// Collect compilation graph edges.
|
|
|
|
const RecordVector& CompilationGraphs =
|
|
|
|
Records.getAllDerivedDefinitions("CompilationGraph");
|
|
|
|
FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
|
2008-12-07 17:41:11 +01:00
|
|
|
Data.Edges);
|
2008-11-17 18:29:42 +01:00
|
|
|
|
2008-12-07 17:41:11 +01:00
|
|
|
// Calculate the priority of this plugin.
|
|
|
|
const RecordVector& Priorities =
|
|
|
|
Records.getAllDerivedDefinitions("PluginPriority");
|
|
|
|
Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckPluginData - Perform some sanity checks on the collected data.
|
|
|
|
void CheckPluginData(PluginData& Data) {
|
2008-11-28 01:13:47 +01:00
|
|
|
// Filter out all tools not mentioned in the compilation graph.
|
2008-12-07 17:41:11 +01:00
|
|
|
FilterNotInGraph(Data.Edges, Data.ToolDescs);
|
2008-11-17 18:29:42 +01:00
|
|
|
|
|
|
|
// Typecheck the compilation graph.
|
2008-12-07 17:41:11 +01:00
|
|
|
TypecheckGraph(Data.Edges, Data.ToolDescs);
|
2008-05-30 08:26:08 +02:00
|
|
|
|
2008-05-30 08:28:37 +02:00
|
|
|
// Check that there are no options without side effects (specified
|
|
|
|
// only in the OptionList).
|
2008-12-07 17:41:11 +01:00
|
|
|
CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
|
|
|
|
}
|
|
|
|
|
2009-07-03 02:10:29 +02:00
|
|
|
void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
|
2008-12-07 17:41:11 +01:00
|
|
|
// Emit file header.
|
|
|
|
EmitIncludes(O);
|
2008-05-30 08:28:37 +02:00
|
|
|
|
2008-05-07 23:50:19 +02:00
|
|
|
// Emit global option registration code.
|
2009-06-23 22:45:07 +02:00
|
|
|
EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-05-30 08:12:24 +02:00
|
|
|
// Emit hook declarations.
|
2009-12-07 18:03:05 +01:00
|
|
|
EmitHookDeclarations(Data.ToolDescs, Data.OptDescs, O);
|
2008-05-30 08:12:24 +02:00
|
|
|
|
2008-12-11 11:34:18 +01:00
|
|
|
O << "namespace {\n\n";
|
|
|
|
|
2009-10-17 22:09:29 +02:00
|
|
|
// Emit PreprocessOptionsLocal() function.
|
|
|
|
EmitPreprocessOptions(Records, Data.OptDescs, O);
|
|
|
|
|
|
|
|
// Emit PopulateLanguageMapLocal() function
|
|
|
|
// (language map maps from file extensions to language names).
|
2008-03-23 09:57:20 +01:00
|
|
|
EmitPopulateLanguageMap(Records, O);
|
|
|
|
|
2008-05-07 23:50:19 +02:00
|
|
|
// Emit Tool classes.
|
2008-12-07 17:41:11 +01:00
|
|
|
for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
|
|
|
|
E = Data.ToolDescs.end(); B!=E; ++B)
|
|
|
|
EmitToolClassDefinition(*(*B), Data.OptDescs, O);
|
2008-05-06 18:36:06 +02:00
|
|
|
|
2008-05-07 23:50:19 +02:00
|
|
|
// Emit Edge# classes.
|
2008-12-07 17:41:11 +01:00
|
|
|
EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2009-10-17 22:09:29 +02:00
|
|
|
// Emit PopulateCompilationGraphLocal() function.
|
2008-12-07 17:41:11 +01:00
|
|
|
EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
|
2008-03-23 09:57:20 +01:00
|
|
|
|
2008-09-22 22:49:34 +02:00
|
|
|
// Emit code for plugin registration.
|
2008-12-07 17:41:11 +01:00
|
|
|
EmitRegisterPlugin(Data.Priority, O);
|
2008-09-22 22:49:34 +02:00
|
|
|
|
2009-06-23 22:46:48 +02:00
|
|
|
O << "} // End anonymous namespace.\n\n";
|
|
|
|
|
|
|
|
// Force linkage magic.
|
|
|
|
O << "namespace llvmc {\n";
|
|
|
|
O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
|
|
|
|
O << "}\n";
|
|
|
|
|
2008-03-23 09:57:20 +01:00
|
|
|
// EOF
|
2008-12-07 17:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// End of anonymous namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
/// run - The back-end entry point.
|
2009-07-03 02:10:29 +02:00
|
|
|
void LLVMCConfigurationEmitter::run (raw_ostream &O) {
|
2008-12-07 17:41:11 +01:00
|
|
|
try {
|
|
|
|
PluginData Data;
|
|
|
|
|
|
|
|
CollectPluginData(Records, Data);
|
|
|
|
CheckPluginData(Data);
|
|
|
|
|
2010-02-23 10:04:44 +01:00
|
|
|
this->EmitSourceFileHeader("LLVMC Configuration Library", O);
|
2008-12-07 17:41:11 +01:00
|
|
|
EmitPluginCode(Data, O);
|
|
|
|
|
2008-05-30 08:21:48 +02:00
|
|
|
} catch (std::exception& Error) {
|
|
|
|
throw Error.what() + std::string(" - usually this means a syntax error.");
|
|
|
|
}
|
2008-03-23 09:57:20 +01:00
|
|
|
}
|