Home | History | Annotate | Download | only in TableGen
      1 //===- ClangAttrEmitter.cpp - Generate Clang attribute handling =-*- C++ -*--=//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // These tablegen backends emit Clang attribute processing code
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/ADT/SmallString.h"
     15 #include "llvm/ADT/STLExtras.h"
     16 #include "llvm/ADT/SmallSet.h"
     17 #include "llvm/ADT/StringExtras.h"
     18 #include "llvm/ADT/StringSwitch.h"
     19 #include "llvm/TableGen/Error.h"
     20 #include "llvm/TableGen/Record.h"
     21 #include "llvm/TableGen/StringMatcher.h"
     22 #include "llvm/TableGen/TableGenBackend.h"
     23 #include <algorithm>
     24 #include <cctype>
     25 #include <memory>
     26 #include <set>
     27 #include <sstream>
     28 
     29 using namespace llvm;
     30 
     31 namespace {
     32 class FlattenedSpelling {
     33   std::string V, N, NS;
     34   bool K;
     35 
     36 public:
     37   FlattenedSpelling(const std::string &Variety, const std::string &Name,
     38                     const std::string &Namespace, bool KnownToGCC) :
     39     V(Variety), N(Name), NS(Namespace), K(KnownToGCC) {}
     40   explicit FlattenedSpelling(const Record &Spelling) :
     41     V(Spelling.getValueAsString("Variety")),
     42     N(Spelling.getValueAsString("Name")) {
     43 
     44     assert(V != "GCC" && "Given a GCC spelling, which means this hasn't been"
     45            "flattened!");
     46     if (V == "CXX11" || V == "Pragma")
     47       NS = Spelling.getValueAsString("Namespace");
     48     bool Unset;
     49     K = Spelling.getValueAsBitOrUnset("KnownToGCC", Unset);
     50   }
     51 
     52   const std::string &variety() const { return V; }
     53   const std::string &name() const { return N; }
     54   const std::string &nameSpace() const { return NS; }
     55   bool knownToGCC() const { return K; }
     56 };
     57 } // namespace
     58 
     59 static std::vector<FlattenedSpelling>
     60 GetFlattenedSpellings(const Record &Attr) {
     61   std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings");
     62   std::vector<FlattenedSpelling> Ret;
     63 
     64   for (const auto &Spelling : Spellings) {
     65     if (Spelling->getValueAsString("Variety") == "GCC") {
     66       // Gin up two new spelling objects to add into the list.
     67       Ret.push_back(FlattenedSpelling("GNU", Spelling->getValueAsString("Name"),
     68                                       "", true));
     69       Ret.push_back(FlattenedSpelling(
     70           "CXX11", Spelling->getValueAsString("Name"), "gnu", true));
     71     } else
     72       Ret.push_back(FlattenedSpelling(*Spelling));
     73   }
     74 
     75   return Ret;
     76 }
     77 
     78 static std::string ReadPCHRecord(StringRef type) {
     79   return StringSwitch<std::string>(type)
     80     .EndsWith("Decl *", "GetLocalDeclAs<"
     81               + std::string(type, 0, type.size()-1) + ">(F, Record[Idx++])")
     82     .Case("TypeSourceInfo *", "GetTypeSourceInfo(F, Record, Idx)")
     83     .Case("Expr *", "ReadExpr(F)")
     84     .Case("IdentifierInfo *", "GetIdentifierInfo(F, Record, Idx)")
     85     .Default("Record[Idx++]");
     86 }
     87 
     88 // Assumes that the way to get the value is SA->getname()
     89 static std::string WritePCHRecord(StringRef type, StringRef name) {
     90   return StringSwitch<std::string>(type)
     91     .EndsWith("Decl *", "AddDeclRef(" + std::string(name) +
     92                         ", Record);\n")
     93     .Case("TypeSourceInfo *",
     94           "AddTypeSourceInfo(" + std::string(name) + ", Record);\n")
     95     .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
     96     .Case("IdentifierInfo *",
     97           "AddIdentifierRef(" + std::string(name) + ", Record);\n")
     98     .Default("Record.push_back(" + std::string(name) + ");\n");
     99 }
    100 
    101 // Normalize attribute name by removing leading and trailing
    102 // underscores. For example, __foo, foo__, __foo__ would
    103 // become foo.
    104 static StringRef NormalizeAttrName(StringRef AttrName) {
    105   if (AttrName.startswith("__"))
    106     AttrName = AttrName.substr(2, AttrName.size());
    107 
    108   if (AttrName.endswith("__"))
    109     AttrName = AttrName.substr(0, AttrName.size() - 2);
    110 
    111   return AttrName;
    112 }
    113 
    114 // Normalize the name by removing any and all leading and trailing underscores.
    115 // This is different from NormalizeAttrName in that it also handles names like
    116 // _pascal and __pascal.
    117 static StringRef NormalizeNameForSpellingComparison(StringRef Name) {
    118   return Name.trim("_");
    119 }
    120 
    121 // Normalize attribute spelling only if the spelling has both leading
    122 // and trailing underscores. For example, __ms_struct__ will be
    123 // normalized to "ms_struct"; __cdecl will remain intact.
    124 static StringRef NormalizeAttrSpelling(StringRef AttrSpelling) {
    125   if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
    126     AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
    127   }
    128 
    129   return AttrSpelling;
    130 }
    131 
    132 typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap;
    133 
    134 static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
    135                                        ParsedAttrMap *Dupes = nullptr) {
    136   std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
    137   std::set<std::string> Seen;
    138   ParsedAttrMap R;
    139   for (const auto *Attr : Attrs) {
    140     if (Attr->getValueAsBit("SemaHandler")) {
    141       std::string AN;
    142       if (Attr->isSubClassOf("TargetSpecificAttr") &&
    143           !Attr->isValueUnset("ParseKind")) {
    144         AN = Attr->getValueAsString("ParseKind");
    145 
    146         // If this attribute has already been handled, it does not need to be
    147         // handled again.
    148         if (Seen.find(AN) != Seen.end()) {
    149           if (Dupes)
    150             Dupes->push_back(std::make_pair(AN, Attr));
    151           continue;
    152         }
    153         Seen.insert(AN);
    154       } else
    155         AN = NormalizeAttrName(Attr->getName()).str();
    156 
    157       R.push_back(std::make_pair(AN, Attr));
    158     }
    159   }
    160   return R;
    161 }
    162 
    163 namespace {
    164   class Argument {
    165     std::string lowerName, upperName;
    166     StringRef attrName;
    167     bool isOpt;
    168 
    169   public:
    170     Argument(const Record &Arg, StringRef Attr)
    171       : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
    172         attrName(Attr), isOpt(false) {
    173       if (!lowerName.empty()) {
    174         lowerName[0] = std::tolower(lowerName[0]);
    175         upperName[0] = std::toupper(upperName[0]);
    176       }
    177     }
    178     virtual ~Argument() {}
    179 
    180     StringRef getLowerName() const { return lowerName; }
    181     StringRef getUpperName() const { return upperName; }
    182     StringRef getAttrName() const { return attrName; }
    183 
    184     bool isOptional() const { return isOpt; }
    185     void setOptional(bool set) { isOpt = set; }
    186 
    187     // These functions print the argument contents formatted in different ways.
    188     virtual void writeAccessors(raw_ostream &OS) const = 0;
    189     virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
    190     virtual void writeASTVisitorTraversal(raw_ostream &OS) const {}
    191     virtual void writeCloneArgs(raw_ostream &OS) const = 0;
    192     virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
    193     virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
    194     virtual void writeCtorBody(raw_ostream &OS) const {}
    195     virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
    196     virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0;
    197     virtual void writeCtorParameters(raw_ostream &OS) const = 0;
    198     virtual void writeDeclarations(raw_ostream &OS) const = 0;
    199     virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
    200     virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
    201     virtual void writePCHWrite(raw_ostream &OS) const = 0;
    202     virtual void writeValue(raw_ostream &OS) const = 0;
    203     virtual void writeDump(raw_ostream &OS) const = 0;
    204     virtual void writeDumpChildren(raw_ostream &OS) const {}
    205     virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
    206 
    207     virtual bool isEnumArg() const { return false; }
    208     virtual bool isVariadicEnumArg() const { return false; }
    209     virtual bool isVariadic() const { return false; }
    210 
    211     virtual void writeImplicitCtorArgs(raw_ostream &OS) const {
    212       OS << getUpperName();
    213     }
    214   };
    215 
    216   class SimpleArgument : public Argument {
    217     std::string type;
    218 
    219   public:
    220     SimpleArgument(const Record &Arg, StringRef Attr, std::string T)
    221       : Argument(Arg, Attr), type(T)
    222     {}
    223 
    224     std::string getType() const { return type; }
    225 
    226     void writeAccessors(raw_ostream &OS) const override {
    227       OS << "  " << type << " get" << getUpperName() << "() const {\n";
    228       OS << "    return " << getLowerName() << ";\n";
    229       OS << "  }";
    230     }
    231     void writeCloneArgs(raw_ostream &OS) const override {
    232       OS << getLowerName();
    233     }
    234     void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
    235       OS << "A->get" << getUpperName() << "()";
    236     }
    237     void writeCtorInitializers(raw_ostream &OS) const override {
    238       OS << getLowerName() << "(" << getUpperName() << ")";
    239     }
    240     void writeCtorDefaultInitializers(raw_ostream &OS) const override {
    241       OS << getLowerName() << "()";
    242     }
    243     void writeCtorParameters(raw_ostream &OS) const override {
    244       OS << type << " " << getUpperName();
    245     }
    246     void writeDeclarations(raw_ostream &OS) const override {
    247       OS << type << " " << getLowerName() << ";";
    248     }
    249     void writePCHReadDecls(raw_ostream &OS) const override {
    250       std::string read = ReadPCHRecord(type);
    251       OS << "    " << type << " " << getLowerName() << " = " << read << ";\n";
    252     }
    253     void writePCHReadArgs(raw_ostream &OS) const override {
    254       OS << getLowerName();
    255     }
    256     void writePCHWrite(raw_ostream &OS) const override {
    257       OS << "    " << WritePCHRecord(type, "SA->get" +
    258                                            std::string(getUpperName()) + "()");
    259     }
    260     void writeValue(raw_ostream &OS) const override {
    261       if (type == "FunctionDecl *") {
    262         OS << "\" << get" << getUpperName()
    263            << "()->getNameInfo().getAsString() << \"";
    264       } else if (type == "IdentifierInfo *") {
    265         OS << "\" << get" << getUpperName() << "()->getName() << \"";
    266       } else if (type == "TypeSourceInfo *") {
    267         OS << "\" << get" << getUpperName() << "().getAsString() << \"";
    268       } else {
    269         OS << "\" << get" << getUpperName() << "() << \"";
    270       }
    271     }
    272     void writeDump(raw_ostream &OS) const override {
    273       if (type == "FunctionDecl *") {
    274         OS << "    OS << \" \";\n";
    275         OS << "    dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
    276       } else if (type == "IdentifierInfo *") {
    277         OS << "    OS << \" \" << SA->get" << getUpperName()
    278            << "()->getName();\n";
    279       } else if (type == "TypeSourceInfo *") {
    280         OS << "    OS << \" \" << SA->get" << getUpperName()
    281            << "().getAsString();\n";
    282       } else if (type == "bool") {
    283         OS << "    if (SA->get" << getUpperName() << "()) OS << \" "
    284            << getUpperName() << "\";\n";
    285       } else if (type == "int" || type == "unsigned") {
    286         OS << "    OS << \" \" << SA->get" << getUpperName() << "();\n";
    287       } else {
    288         llvm_unreachable("Unknown SimpleArgument type!");
    289       }
    290     }
    291   };
    292 
    293   class DefaultSimpleArgument : public SimpleArgument {
    294     int64_t Default;
    295 
    296   public:
    297     DefaultSimpleArgument(const Record &Arg, StringRef Attr,
    298                           std::string T, int64_t Default)
    299       : SimpleArgument(Arg, Attr, T), Default(Default) {}
    300 
    301     void writeAccessors(raw_ostream &OS) const override {
    302       SimpleArgument::writeAccessors(OS);
    303 
    304       OS << "\n\n  static const " << getType() << " Default" << getUpperName()
    305          << " = " << Default << ";";
    306     }
    307   };
    308 
    309   class StringArgument : public Argument {
    310   public:
    311     StringArgument(const Record &Arg, StringRef Attr)
    312       : Argument(Arg, Attr)
    313     {}
    314 
    315     void writeAccessors(raw_ostream &OS) const override {
    316       OS << "  llvm::StringRef get" << getUpperName() << "() const {\n";
    317       OS << "    return llvm::StringRef(" << getLowerName() << ", "
    318          << getLowerName() << "Length);\n";
    319       OS << "  }\n";
    320       OS << "  unsigned get" << getUpperName() << "Length() const {\n";
    321       OS << "    return " << getLowerName() << "Length;\n";
    322       OS << "  }\n";
    323       OS << "  void set" << getUpperName()
    324          << "(ASTContext &C, llvm::StringRef S) {\n";
    325       OS << "    " << getLowerName() << "Length = S.size();\n";
    326       OS << "    this->" << getLowerName() << " = new (C, 1) char ["
    327          << getLowerName() << "Length];\n";
    328       OS << "    std::memcpy(this->" << getLowerName() << ", S.data(), "
    329          << getLowerName() << "Length);\n";
    330       OS << "  }";
    331     }
    332     void writeCloneArgs(raw_ostream &OS) const override {
    333       OS << "get" << getUpperName() << "()";
    334     }
    335     void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
    336       OS << "A->get" << getUpperName() << "()";
    337     }
    338     void writeCtorBody(raw_ostream &OS) const override {
    339       OS << "      std::memcpy(" << getLowerName() << ", " << getUpperName()
    340          << ".data(), " << getLowerName() << "Length);";
    341     }
    342     void writeCtorInitializers(raw_ostream &OS) const override {
    343       OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
    344          << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
    345          << "Length])";
    346     }
    347     void writeCtorDefaultInitializers(raw_ostream &OS) const override {
    348       OS << getLowerName() << "Length(0)," << getLowerName() << "(0)";
    349     }
    350     void writeCtorParameters(raw_ostream &OS) const override {
    351       OS << "llvm::StringRef " << getUpperName();
    352     }
    353     void writeDeclarations(raw_ostream &OS) const override {
    354       OS << "unsigned " << getLowerName() << "Length;\n";
    355       OS << "char *" << getLowerName() << ";";
    356     }
    357     void writePCHReadDecls(raw_ostream &OS) const override {
    358       OS << "    std::string " << getLowerName()
    359          << "= ReadString(Record, Idx);\n";
    360     }
    361     void writePCHReadArgs(raw_ostream &OS) const override {
    362       OS << getLowerName();
    363     }
    364     void writePCHWrite(raw_ostream &OS) const override {
    365       OS << "    AddString(SA->get" << getUpperName() << "(), Record);\n";
    366     }
    367     void writeValue(raw_ostream &OS) const override {
    368       OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
    369     }
    370     void writeDump(raw_ostream &OS) const override {
    371       OS << "    OS << \" \\\"\" << SA->get" << getUpperName()
    372          << "() << \"\\\"\";\n";
    373     }
    374   };
    375 
    376   class AlignedArgument : public Argument {
    377   public:
    378     AlignedArgument(const Record &Arg, StringRef Attr)
    379       : Argument(Arg, Attr)
    380     {}
    381 
    382     void writeAccessors(raw_ostream &OS) const override {
    383       OS << "  bool is" << getUpperName() << "Dependent() const;\n";
    384 
    385       OS << "  unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
    386 
    387       OS << "  bool is" << getUpperName() << "Expr() const {\n";
    388       OS << "    return is" << getLowerName() << "Expr;\n";
    389       OS << "  }\n";
    390 
    391       OS << "  Expr *get" << getUpperName() << "Expr() const {\n";
    392       OS << "    assert(is" << getLowerName() << "Expr);\n";
    393       OS << "    return " << getLowerName() << "Expr;\n";
    394       OS << "  }\n";
    395 
    396       OS << "  TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
    397       OS << "    assert(!is" << getLowerName() << "Expr);\n";
    398       OS << "    return " << getLowerName() << "Type;\n";
    399       OS << "  }";
    400     }
    401     void writeAccessorDefinitions(raw_ostream &OS) const override {
    402       OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
    403          << "Dependent() const {\n";
    404       OS << "  if (is" << getLowerName() << "Expr)\n";
    405       OS << "    return " << getLowerName() << "Expr && (" << getLowerName()
    406          << "Expr->isValueDependent() || " << getLowerName()
    407          << "Expr->isTypeDependent());\n";
    408       OS << "  else\n";
    409       OS << "    return " << getLowerName()
    410          << "Type->getType()->isDependentType();\n";
    411       OS << "}\n";
    412 
    413       // FIXME: Do not do the calculation here
    414       // FIXME: Handle types correctly
    415       // A null pointer means maximum alignment
    416       // FIXME: Load the platform-specific maximum alignment, rather than
    417       //        16, the x86 max.
    418       OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
    419          << "(ASTContext &Ctx) const {\n";
    420       OS << "  assert(!is" << getUpperName() << "Dependent());\n";
    421       OS << "  if (is" << getLowerName() << "Expr)\n";
    422       OS << "    return (" << getLowerName() << "Expr ? " << getLowerName()
    423          << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue() : 16)"
    424          << "* Ctx.getCharWidth();\n";
    425       OS << "  else\n";
    426       OS << "    return 0; // FIXME\n";
    427       OS << "}\n";
    428     }
    429     void writeCloneArgs(raw_ostream &OS) const override {
    430       OS << "is" << getLowerName() << "Expr, is" << getLowerName()
    431          << "Expr ? static_cast<void*>(" << getLowerName()
    432          << "Expr) : " << getLowerName()
    433          << "Type";
    434     }
    435     void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
    436       // FIXME: move the definition in Sema::InstantiateAttrs to here.
    437       // In the meantime, aligned attributes are cloned.
    438     }
    439     void writeCtorBody(raw_ostream &OS) const override {
    440       OS << "    if (is" << getLowerName() << "Expr)\n";
    441       OS << "       " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
    442          << getUpperName() << ");\n";
    443       OS << "    else\n";
    444       OS << "       " << getLowerName()
    445          << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
    446          << ");";
    447     }
    448     void writeCtorInitializers(raw_ostream &OS) const override {
    449       OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
    450     }
    451     void writeCtorDefaultInitializers(raw_ostream &OS) const override {
    452       OS << "is" << getLowerName() << "Expr(false)";
    453     }
    454     void writeCtorParameters(raw_ostream &OS) const override {
    455       OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
    456     }
    457     void writeImplicitCtorArgs(raw_ostream &OS) const override {
    458       OS << "Is" << getUpperName() << "Expr, " << getUpperName();
    459     }
    460     void writeDeclarations(raw_ostream &OS) const override {
    461       OS << "bool is" << getLowerName() << "Expr;\n";
    462       OS << "union {\n";
    463       OS << "Expr *" << getLowerName() << "Expr;\n";
    464       OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
    465       OS << "};";
    466     }
    467     void writePCHReadArgs(raw_ostream &OS) const override {
    468       OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
    469     }
    470     void writePCHReadDecls(raw_ostream &OS) const override {
    471       OS << "    bool is" << getLowerName() << "Expr = Record[Idx++];\n";
    472       OS << "    void *" << getLowerName() << "Ptr;\n";
    473       OS << "    if (is" << getLowerName() << "Expr)\n";
    474       OS << "      " << getLowerName() << "Ptr = ReadExpr(F);\n";
    475       OS << "    else\n";
    476       OS << "      " << getLowerName()
    477          << "Ptr = GetTypeSourceInfo(F, Record, Idx);\n";
    478     }
    479     void writePCHWrite(raw_ostream &OS) const override {
    480       OS << "    Record.push_back(SA->is" << getUpperName() << "Expr());\n";
    481       OS << "    if (SA->is" << getUpperName() << "Expr())\n";
    482       OS << "      AddStmt(SA->get" << getUpperName() << "Expr());\n";
    483       OS << "    else\n";
    484       OS << "      AddTypeSourceInfo(SA->get" << getUpperName()
    485          << "Type(), Record);\n";
    486     }
    487     void writeValue(raw_ostream &OS) const override {
    488       OS << "\";\n";
    489       // The aligned attribute argument expression is optional.
    490       OS << "    if (is" << getLowerName() << "Expr && "
    491          << getLowerName() << "Expr)\n";
    492       OS << "      " << getLowerName() << "Expr->printPretty(OS, 0, Policy);\n";
    493       OS << "    OS << \"";
    494     }
    495     void writeDump(raw_ostream &OS) const override {
    496     }
    497     void writeDumpChildren(raw_ostream &OS) const override {
    498       OS << "    if (SA->is" << getUpperName() << "Expr())\n";
    499       OS << "      dumpStmt(SA->get" << getUpperName() << "Expr());\n";
    500       OS << "    else\n";
    501       OS << "      dumpType(SA->get" << getUpperName()
    502          << "Type()->getType());\n";
    503     }
    504     void writeHasChildren(raw_ostream &OS) const override {
    505       OS << "SA->is" << getUpperName() << "Expr()";
    506     }
    507   };
    508 
    509   class VariadicArgument : public Argument {
    510     std::string Type, ArgName, ArgSizeName, RangeName;
    511 
    512   protected:
    513     // Assumed to receive a parameter: raw_ostream OS.
    514     virtual void writeValueImpl(raw_ostream &OS) const {
    515       OS << "    OS << Val;\n";
    516     }
    517 
    518   public:
    519     VariadicArgument(const Record &Arg, StringRef Attr, std::string T)
    520         : Argument(Arg, Attr), Type(T), ArgName(getLowerName().str() + "_"),
    521           ArgSizeName(ArgName + "Size"), RangeName(getLowerName()) {}
    522 
    523     std::string getType() const { return Type; }
    524     bool isVariadic() const override { return true; }
    525 
    526     void writeAccessors(raw_ostream &OS) const override {
    527       std::string IteratorType = getLowerName().str() + "_iterator";
    528       std::string BeginFn = getLowerName().str() + "_begin()";
    529       std::string EndFn = getLowerName().str() + "_end()";
    530 
    531       OS << "  typedef " << Type << "* " << IteratorType << ";\n";
    532       OS << "  " << IteratorType << " " << BeginFn << " const {"
    533          << " return " << ArgName << "; }\n";
    534       OS << "  " << IteratorType << " " << EndFn << " const {"
    535          << " return " << ArgName << " + " << ArgSizeName << "; }\n";
    536       OS << "  unsigned " << getLowerName() << "_size() const {"
    537          << " return " << ArgSizeName << "; }\n";
    538       OS << "  llvm::iterator_range<" << IteratorType << "> " << RangeName
    539          << "() const { return llvm::make_range(" << BeginFn << ", " << EndFn
    540          << "); }\n";
    541     }
    542     void writeCloneArgs(raw_ostream &OS) const override {
    543       OS << ArgName << ", " << ArgSizeName;
    544     }
    545     void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
    546       // This isn't elegant, but we have to go through public methods...
    547       OS << "A->" << getLowerName() << "_begin(), "
    548          << "A->" << getLowerName() << "_size()";
    549     }
    550     void writeCtorBody(raw_ostream &OS) const override {
    551       OS << "    std::copy(" << getUpperName() << ", " << getUpperName()
    552          << " + " << ArgSizeName << ", " << ArgName << ");";
    553     }
    554     void writeCtorInitializers(raw_ostream &OS) const override {
    555       OS << ArgSizeName << "(" << getUpperName() << "Size), "
    556          << ArgName << "(new (Ctx, 16) " << getType() << "["
    557          << ArgSizeName << "])";
    558     }
    559     void writeCtorDefaultInitializers(raw_ostream &OS) const override {
    560       OS << ArgSizeName << "(0), " << ArgName << "(nullptr)";
    561     }
    562     void writeCtorParameters(raw_ostream &OS) const override {
    563       OS << getType() << " *" << getUpperName() << ", unsigned "
    564          << getUpperName() << "Size";
    565     }
    566     void writeImplicitCtorArgs(raw_ostream &OS) const override {
    567       OS << getUpperName() << ", " << getUpperName() << "Size";
    568     }
    569     void writeDeclarations(raw_ostream &OS) const override {
    570       OS << "  unsigned " << ArgSizeName << ";\n";
    571       OS << "  " << getType() << " *" << ArgName << ";";
    572     }
    573     void writePCHReadDecls(raw_ostream &OS) const override {
    574       OS << "  unsigned " << getLowerName() << "Size = Record[Idx++];\n";
    575       OS << "  SmallVector<" << Type << ", 4> " << getLowerName()
    576          << ";\n";
    577       OS << "  " << getLowerName() << ".reserve(" << getLowerName()
    578          << "Size);\n";
    579       OS << "    for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
    580 
    581       std::string read = ReadPCHRecord(Type);
    582       OS << "    " << getLowerName() << ".push_back(" << read << ");\n";
    583     }
    584     void writePCHReadArgs(raw_ostream &OS) const override {
    585       OS << getLowerName() << ".data(), " << getLowerName() << "Size";
    586     }
    587     void writePCHWrite(raw_ostream &OS) const override {
    588       OS << "    Record.push_back(SA->" << getLowerName() << "_size());\n";
    589       OS << "    for (auto &Val : SA->" << RangeName << "())\n";
    590       OS << "      " << WritePCHRecord(Type, "Val");
    591     }
    592     void writeValue(raw_ostream &OS) const override {
    593       OS << "\";\n";
    594       OS << "  bool isFirst = true;\n"
    595          << "  for (const auto &Val : " << RangeName << "()) {\n"
    596          << "    if (isFirst) isFirst = false;\n"
    597          << "    else OS << \", \";\n";
    598       writeValueImpl(OS);
    599       OS << "  }\n";
    600       OS << "  OS << \"";
    601     }
    602     void writeDump(raw_ostream &OS) const override {
    603       OS << "    for (const auto &Val : SA->" << RangeName << "())\n";
    604       OS << "      OS << \" \" << Val;\n";
    605     }
    606   };
    607 
    608   // Unique the enums, but maintain the original declaration ordering.
    609   std::vector<std::string>
    610   uniqueEnumsInOrder(const std::vector<std::string> &enums) {
    611     std::vector<std::string> uniques;
    612     std::set<std::string> unique_set(enums.begin(), enums.end());
    613     for (const auto &i : enums) {
    614       std::set<std::string>::iterator set_i = unique_set.find(i);
    615       if (set_i != unique_set.end()) {
    616         uniques.push_back(i);
    617         unique_set.erase(set_i);
    618       }
    619     }
    620     return uniques;
    621   }
    622 
    623   class EnumArgument : public Argument {
    624     std::string type;
    625     std::vector<std::string> values, enums, uniques;
    626   public:
    627     EnumArgument(const Record &Arg, StringRef Attr)
    628       : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
    629         values(Arg.getValueAsListOfStrings("Values")),
    630         enums(Arg.getValueAsListOfStrings("Enums")),
    631         uniques(uniqueEnumsInOrder(enums))
    632     {
    633       // FIXME: Emit a proper error
    634       assert(!uniques.empty());
    635     }
    636 
    637     bool isEnumArg() const override { return true; }
    638 
    639     void writeAccessors(raw_ostream &OS) const override {
    640       OS << "  " << type << " get" << getUpperName() << "() const {\n";
    641       OS << "    return " << getLowerName() << ";\n";
    642       OS << "  }";
    643     }
    644     void writeCloneArgs(raw_ostream &OS) const override {
    645       OS << getLowerName();
    646     }
    647     void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
    648       OS << "A->get" << getUpperName() << "()";
    649     }
    650     void writeCtorInitializers(raw_ostream &OS) const override {
    651       OS << getLowerName() << "(" << getUpperName() << ")";
    652     }
    653     void writeCtorDefaultInitializers(raw_ostream &OS) const override {
    654       OS << getLowerName() << "(" << type << "(0))";
    655     }
    656     void writeCtorParameters(raw_ostream &OS) const override {
    657       OS << type << " " << getUpperName();
    658     }
    659     void writeDeclarations(raw_ostream &OS) const override {
    660       std::vector<std::string>::const_iterator i = uniques.begin(),
    661                                                e = uniques.end();
    662       // The last one needs to not have a comma.
    663       --e;
    664 
    665       OS << "public:\n";
    666       OS << "  enum " << type << " {\n";
    667       for (; i != e; ++i)
    668         OS << "    " << *i << ",\n";
    669       OS << "    " << *e << "\n";
    670       OS << "  };\n";
    671       OS << "private:\n";
    672       OS << "  " << type << " " << getLowerName() << ";";
    673     }
    674     void writePCHReadDecls(raw_ostream &OS) const override {
    675       OS << "    " << getAttrName() << "Attr::" << type << " " << getLowerName()
    676          << "(static_cast<" << getAttrName() << "Attr::" << type
    677          << ">(Record[Idx++]));\n";
    678     }
    679     void writePCHReadArgs(raw_ostream &OS) const override {
    680       OS << getLowerName();
    681     }
    682     void writePCHWrite(raw_ostream &OS) const override {
    683       OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
    684     }
    685     void writeValue(raw_ostream &OS) const override {
    686       // FIXME: this isn't 100% correct -- some enum arguments require printing
    687       // as a string literal, while others require printing as an identifier.
    688       // Tablegen currently does not distinguish between the two forms.
    689       OS << "\\\"\" << " << getAttrName() << "Attr::Convert" << type << "ToStr(get"
    690          << getUpperName() << "()) << \"\\\"";
    691     }
    692     void writeDump(raw_ostream &OS) const override {
    693       OS << "    switch(SA->get" << getUpperName() << "()) {\n";
    694       for (const auto &I : uniques) {
    695         OS << "    case " << getAttrName() << "Attr::" << I << ":\n";
    696         OS << "      OS << \" " << I << "\";\n";
    697         OS << "      break;\n";
    698       }
    699       OS << "    }\n";
    700     }
    701 
    702     void writeConversion(raw_ostream &OS) const {
    703       OS << "  static bool ConvertStrTo" << type << "(StringRef Val, ";
    704       OS << type << " &Out) {\n";
    705       OS << "    Optional<" << type << "> R = llvm::StringSwitch<Optional<";
    706       OS << type << ">>(Val)\n";
    707       for (size_t I = 0; I < enums.size(); ++I) {
    708         OS << "      .Case(\"" << values[I] << "\", ";
    709         OS << getAttrName() << "Attr::" << enums[I] << ")\n";
    710       }
    711       OS << "      .Default(Optional<" << type << ">());\n";
    712       OS << "    if (R) {\n";
    713       OS << "      Out = *R;\n      return true;\n    }\n";
    714       OS << "    return false;\n";
    715       OS << "  }\n\n";
    716 
    717       // Mapping from enumeration values back to enumeration strings isn't
    718       // trivial because some enumeration values have multiple named
    719       // enumerators, such as type_visibility(internal) and
    720       // type_visibility(hidden) both mapping to TypeVisibilityAttr::Hidden.
    721       OS << "  static const char *Convert" << type << "ToStr("
    722          << type << " Val) {\n"
    723          << "    switch(Val) {\n";
    724       std::set<std::string> Uniques;
    725       for (size_t I = 0; I < enums.size(); ++I) {
    726         if (Uniques.insert(enums[I]).second)
    727           OS << "    case " << getAttrName() << "Attr::" << enums[I]
    728              << ": return \"" << values[I] << "\";\n";
    729       }
    730       OS << "    }\n"
    731          << "    llvm_unreachable(\"No enumerator with that value\");\n"
    732          << "  }\n";
    733     }
    734   };
    735 
    736   class VariadicEnumArgument: public VariadicArgument {
    737     std::string type, QualifiedTypeName;
    738     std::vector<std::string> values, enums, uniques;
    739 
    740   protected:
    741     void writeValueImpl(raw_ostream &OS) const override {
    742       // FIXME: this isn't 100% correct -- some enum arguments require printing
    743       // as a string literal, while others require printing as an identifier.
    744       // Tablegen currently does not distinguish between the two forms.
    745       OS << "    OS << \"\\\"\" << " << getAttrName() << "Attr::Convert" << type
    746          << "ToStr(Val)" << "<< \"\\\"\";\n";
    747     }
    748 
    749   public:
    750     VariadicEnumArgument(const Record &Arg, StringRef Attr)
    751       : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")),
    752         type(Arg.getValueAsString("Type")),
    753         values(Arg.getValueAsListOfStrings("Values")),
    754         enums(Arg.getValueAsListOfStrings("Enums")),
    755         uniques(uniqueEnumsInOrder(enums))
    756     {
    757       QualifiedTypeName = getAttrName().str() + "Attr::" + type;
    758 
    759       // FIXME: Emit a proper error
    760       assert(!uniques.empty());
    761     }
    762 
    763     bool isVariadicEnumArg() const override { return true; }
    764 
    765     void writeDeclarations(raw_ostream &OS) const override {
    766       std::vector<std::string>::const_iterator i = uniques.begin(),
    767                                                e = uniques.end();
    768       // The last one needs to not have a comma.
    769       --e;
    770 
    771       OS << "public:\n";
    772       OS << "  enum " << type << " {\n";
    773       for (; i != e; ++i)
    774         OS << "    " << *i << ",\n";
    775       OS << "    " << *e << "\n";
    776       OS << "  };\n";
    777       OS << "private:\n";
    778 
    779       VariadicArgument::writeDeclarations(OS);
    780     }
    781     void writeDump(raw_ostream &OS) const override {
    782       OS << "    for (" << getAttrName() << "Attr::" << getLowerName()
    783          << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
    784          << getLowerName() << "_end(); I != E; ++I) {\n";
    785       OS << "      switch(*I) {\n";
    786       for (const auto &UI : uniques) {
    787         OS << "    case " << getAttrName() << "Attr::" << UI << ":\n";
    788         OS << "      OS << \" " << UI << "\";\n";
    789         OS << "      break;\n";
    790       }
    791       OS << "      }\n";
    792       OS << "    }\n";
    793     }
    794     void writePCHReadDecls(raw_ostream &OS) const override {
    795       OS << "    unsigned " << getLowerName() << "Size = Record[Idx++];\n";
    796       OS << "    SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName()
    797          << ";\n";
    798       OS << "    " << getLowerName() << ".reserve(" << getLowerName()
    799          << "Size);\n";
    800       OS << "    for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
    801       OS << "      " << getLowerName() << ".push_back(" << "static_cast<"
    802          << QualifiedTypeName << ">(Record[Idx++]));\n";
    803     }
    804     void writePCHWrite(raw_ostream &OS) const override {
    805       OS << "    Record.push_back(SA->" << getLowerName() << "_size());\n";
    806       OS << "    for (" << getAttrName() << "Attr::" << getLowerName()
    807          << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
    808          << getLowerName() << "_end(); i != e; ++i)\n";
    809       OS << "      " << WritePCHRecord(QualifiedTypeName, "(*i)");
    810     }
    811     void writeConversion(raw_ostream &OS) const {
    812       OS << "  static bool ConvertStrTo" << type << "(StringRef Val, ";
    813       OS << type << " &Out) {\n";
    814       OS << "    Optional<" << type << "> R = llvm::StringSwitch<Optional<";
    815       OS << type << ">>(Val)\n";
    816       for (size_t I = 0; I < enums.size(); ++I) {
    817         OS << "      .Case(\"" << values[I] << "\", ";
    818         OS << getAttrName() << "Attr::" << enums[I] << ")\n";
    819       }
    820       OS << "      .Default(Optional<" << type << ">());\n";
    821       OS << "    if (R) {\n";
    822       OS << "      Out = *R;\n      return true;\n    }\n";
    823       OS << "    return false;\n";
    824       OS << "  }\n\n";
    825 
    826       OS << "  static const char *Convert" << type << "ToStr("
    827         << type << " Val) {\n"
    828         << "    switch(Val) {\n";
    829       std::set<std::string> Uniques;
    830       for (size_t I = 0; I < enums.size(); ++I) {
    831         if (Uniques.insert(enums[I]).second)
    832           OS << "    case " << getAttrName() << "Attr::" << enums[I]
    833           << ": return \"" << values[I] << "\";\n";
    834       }
    835       OS << "    }\n"
    836         << "    llvm_unreachable(\"No enumerator with that value\");\n"
    837         << "  }\n";
    838     }
    839   };
    840 
    841   class VersionArgument : public Argument {
    842   public:
    843     VersionArgument(const Record &Arg, StringRef Attr)
    844       : Argument(Arg, Attr)
    845     {}
    846 
    847     void writeAccessors(raw_ostream &OS) const override {
    848       OS << "  VersionTuple get" << getUpperName() << "() const {\n";
    849       OS << "    return " << getLowerName() << ";\n";
    850       OS << "  }\n";
    851       OS << "  void set" << getUpperName()
    852          << "(ASTContext &C, VersionTuple V) {\n";
    853       OS << "    " << getLowerName() << " = V;\n";
    854       OS << "  }";
    855     }
    856     void writeCloneArgs(raw_ostream &OS) const override {
    857       OS << "get" << getUpperName() << "()";
    858     }
    859     void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
    860       OS << "A->get" << getUpperName() << "()";
    861     }
    862     void writeCtorInitializers(raw_ostream &OS) const override {
    863       OS << getLowerName() << "(" << getUpperName() << ")";
    864     }
    865     void writeCtorDefaultInitializers(raw_ostream &OS) const override {
    866       OS << getLowerName() << "()";
    867     }
    868     void writeCtorParameters(raw_ostream &OS) const override {
    869       OS << "VersionTuple " << getUpperName();
    870     }
    871     void writeDeclarations(raw_ostream &OS) const override {
    872       OS << "VersionTuple " << getLowerName() << ";\n";
    873     }
    874     void writePCHReadDecls(raw_ostream &OS) const override {
    875       OS << "    VersionTuple " << getLowerName()
    876          << "= ReadVersionTuple(Record, Idx);\n";
    877     }
    878     void writePCHReadArgs(raw_ostream &OS) const override {
    879       OS << getLowerName();
    880     }
    881     void writePCHWrite(raw_ostream &OS) const override {
    882       OS << "    AddVersionTuple(SA->get" << getUpperName() << "(), Record);\n";
    883     }
    884     void writeValue(raw_ostream &OS) const override {
    885       OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
    886     }
    887     void writeDump(raw_ostream &OS) const override {
    888       OS << "    OS << \" \" << SA->get" << getUpperName() << "();\n";
    889     }
    890   };
    891 
    892   class ExprArgument : public SimpleArgument {
    893   public:
    894     ExprArgument(const Record &Arg, StringRef Attr)
    895       : SimpleArgument(Arg, Attr, "Expr *")
    896     {}
    897 
    898     void writeASTVisitorTraversal(raw_ostream &OS) const override {
    899       OS << "  if (!"
    900          << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n";
    901       OS << "    return false;\n";
    902     }
    903 
    904     void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
    905       OS << "tempInst" << getUpperName();
    906     }
    907 
    908     void writeTemplateInstantiation(raw_ostream &OS) const override {
    909       OS << "      " << getType() << " tempInst" << getUpperName() << ";\n";
    910       OS << "      {\n";
    911       OS << "        EnterExpressionEvaluationContext "
    912          << "Unevaluated(S, Sema::Unevaluated);\n";
    913       OS << "        ExprResult " << "Result = S.SubstExpr("
    914          << "A->get" << getUpperName() << "(), TemplateArgs);\n";
    915       OS << "        tempInst" << getUpperName() << " = "
    916          << "Result.getAs<Expr>();\n";
    917       OS << "      }\n";
    918     }
    919 
    920     void writeDump(raw_ostream &OS) const override {}
    921 
    922     void writeDumpChildren(raw_ostream &OS) const override {
    923       OS << "    dumpStmt(SA->get" << getUpperName() << "());\n";
    924     }
    925     void writeHasChildren(raw_ostream &OS) const override { OS << "true"; }
    926   };
    927 
    928   class VariadicExprArgument : public VariadicArgument {
    929   public:
    930     VariadicExprArgument(const Record &Arg, StringRef Attr)
    931       : VariadicArgument(Arg, Attr, "Expr *")
    932     {}
    933 
    934     void writeASTVisitorTraversal(raw_ostream &OS) const override {
    935       OS << "  {\n";
    936       OS << "    " << getType() << " *I = A->" << getLowerName()
    937          << "_begin();\n";
    938       OS << "    " << getType() << " *E = A->" << getLowerName()
    939          << "_end();\n";
    940       OS << "    for (; I != E; ++I) {\n";
    941       OS << "      if (!getDerived().TraverseStmt(*I))\n";
    942       OS << "        return false;\n";
    943       OS << "    }\n";
    944       OS << "  }\n";
    945     }
    946 
    947     void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
    948       OS << "tempInst" << getUpperName() << ", "
    949          << "A->" << getLowerName() << "_size()";
    950     }
    951 
    952     void writeTemplateInstantiation(raw_ostream &OS) const override {
    953       OS << "      " << getType() << " *tempInst" << getUpperName()
    954          << " = new (C, 16) " << getType()
    955          << "[A->" << getLowerName() << "_size()];\n";
    956       OS << "      {\n";
    957       OS << "        EnterExpressionEvaluationContext "
    958          << "Unevaluated(S, Sema::Unevaluated);\n";
    959       OS << "        " << getType() << " *TI = tempInst" << getUpperName()
    960          << ";\n";
    961       OS << "        " << getType() << " *I = A->" << getLowerName()
    962          << "_begin();\n";
    963       OS << "        " << getType() << " *E = A->" << getLowerName()
    964          << "_end();\n";
    965       OS << "        for (; I != E; ++I, ++TI) {\n";
    966       OS << "          ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
    967       OS << "          *TI = Result.getAs<Expr>();\n";
    968       OS << "        }\n";
    969       OS << "      }\n";
    970     }
    971 
    972     void writeDump(raw_ostream &OS) const override {}
    973 
    974     void writeDumpChildren(raw_ostream &OS) const override {
    975       OS << "    for (" << getAttrName() << "Attr::" << getLowerName()
    976          << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
    977          << getLowerName() << "_end(); I != E; ++I)\n";
    978       OS << "      dumpStmt(*I);\n";
    979     }
    980 
    981     void writeHasChildren(raw_ostream &OS) const override {
    982       OS << "SA->" << getLowerName() << "_begin() != "
    983          << "SA->" << getLowerName() << "_end()";
    984     }
    985   };
    986 
    987   class TypeArgument : public SimpleArgument {
    988   public:
    989     TypeArgument(const Record &Arg, StringRef Attr)
    990       : SimpleArgument(Arg, Attr, "TypeSourceInfo *")
    991     {}
    992 
    993     void writeAccessors(raw_ostream &OS) const override {
    994       OS << "  QualType get" << getUpperName() << "() const {\n";
    995       OS << "    return " << getLowerName() << "->getType();\n";
    996       OS << "  }";
    997       OS << "  " << getType() << " get" << getUpperName() << "Loc() const {\n";
    998       OS << "    return " << getLowerName() << ";\n";
    999       OS << "  }";
   1000     }
   1001     void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
   1002       OS << "A->get" << getUpperName() << "Loc()";
   1003     }
   1004     void writePCHWrite(raw_ostream &OS) const override {
   1005       OS << "    " << WritePCHRecord(
   1006           getType(), "SA->get" + std::string(getUpperName()) + "Loc()");
   1007     }
   1008   };
   1009 }
   1010 
   1011 static std::unique_ptr<Argument>
   1012 createArgument(const Record &Arg, StringRef Attr,
   1013                const Record *Search = nullptr) {
   1014   if (!Search)
   1015     Search = &Arg;
   1016 
   1017   std::unique_ptr<Argument> Ptr;
   1018   llvm::StringRef ArgName = Search->getName();
   1019 
   1020   if (ArgName == "AlignedArgument")
   1021     Ptr = llvm::make_unique<AlignedArgument>(Arg, Attr);
   1022   else if (ArgName == "EnumArgument")
   1023     Ptr = llvm::make_unique<EnumArgument>(Arg, Attr);
   1024   else if (ArgName == "ExprArgument")
   1025     Ptr = llvm::make_unique<ExprArgument>(Arg, Attr);
   1026   else if (ArgName == "FunctionArgument")
   1027     Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "FunctionDecl *");
   1028   else if (ArgName == "IdentifierArgument")
   1029     Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "IdentifierInfo *");
   1030   else if (ArgName == "DefaultBoolArgument")
   1031     Ptr = llvm::make_unique<DefaultSimpleArgument>(
   1032         Arg, Attr, "bool", Arg.getValueAsBit("Default"));
   1033   else if (ArgName == "BoolArgument")
   1034     Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "bool");
   1035   else if (ArgName == "DefaultIntArgument")
   1036     Ptr = llvm::make_unique<DefaultSimpleArgument>(
   1037         Arg, Attr, "int", Arg.getValueAsInt("Default"));
   1038   else if (ArgName == "IntArgument")
   1039     Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "int");
   1040   else if (ArgName == "StringArgument")
   1041     Ptr = llvm::make_unique<StringArgument>(Arg, Attr);
   1042   else if (ArgName == "TypeArgument")
   1043     Ptr = llvm::make_unique<TypeArgument>(Arg, Attr);
   1044   else if (ArgName == "UnsignedArgument")
   1045     Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "unsigned");
   1046   else if (ArgName == "VariadicUnsignedArgument")
   1047     Ptr = llvm::make_unique<VariadicArgument>(Arg, Attr, "unsigned");
   1048   else if (ArgName == "VariadicEnumArgument")
   1049     Ptr = llvm::make_unique<VariadicEnumArgument>(Arg, Attr);
   1050   else if (ArgName == "VariadicExprArgument")
   1051     Ptr = llvm::make_unique<VariadicExprArgument>(Arg, Attr);
   1052   else if (ArgName == "VersionArgument")
   1053     Ptr = llvm::make_unique<VersionArgument>(Arg, Attr);
   1054 
   1055   if (!Ptr) {
   1056     // Search in reverse order so that the most-derived type is handled first.
   1057     std::vector<Record*> Bases = Search->getSuperClasses();
   1058     for (const auto *Base : llvm::make_range(Bases.rbegin(), Bases.rend())) {
   1059       if ((Ptr = createArgument(Arg, Attr, Base)))
   1060         break;
   1061     }
   1062   }
   1063 
   1064   if (Ptr && Arg.getValueAsBit("Optional"))
   1065     Ptr->setOptional(true);
   1066 
   1067   return Ptr;
   1068 }
   1069 
   1070 static void writeAvailabilityValue(raw_ostream &OS) {
   1071   OS << "\" << getPlatform()->getName();\n"
   1072      << "  if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
   1073      << "  if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
   1074      << "  if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
   1075      << "  if (getUnavailable()) OS << \", unavailable\";\n"
   1076      << "  OS << \"";
   1077 }
   1078 
   1079 static void writeGetSpellingFunction(Record &R, raw_ostream &OS) {
   1080   std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
   1081 
   1082   OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n";
   1083   if (Spellings.empty()) {
   1084     OS << "  return \"(No spelling)\";\n}\n\n";
   1085     return;
   1086   }
   1087 
   1088   OS << "  switch (SpellingListIndex) {\n"
   1089         "  default:\n"
   1090         "    llvm_unreachable(\"Unknown attribute spelling!\");\n"
   1091         "    return \"(No spelling)\";\n";
   1092 
   1093   for (unsigned I = 0; I < Spellings.size(); ++I)
   1094     OS << "  case " << I << ":\n"
   1095           "    return \"" << Spellings[I].name() << "\";\n";
   1096   // End of the switch statement.
   1097   OS << "  }\n";
   1098   // End of the getSpelling function.
   1099   OS << "}\n\n";
   1100 }
   1101 
   1102 static void
   1103 writePrettyPrintFunction(Record &R,
   1104                          const std::vector<std::unique_ptr<Argument>> &Args,
   1105                          raw_ostream &OS) {
   1106   std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
   1107 
   1108   OS << "void " << R.getName() << "Attr::printPretty("
   1109     << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
   1110 
   1111   if (Spellings.empty()) {
   1112     OS << "}\n\n";
   1113     return;
   1114   }
   1115 
   1116   OS <<
   1117     "  switch (SpellingListIndex) {\n"
   1118     "  default:\n"
   1119     "    llvm_unreachable(\"Unknown attribute spelling!\");\n"
   1120     "    break;\n";
   1121 
   1122   for (unsigned I = 0; I < Spellings.size(); ++ I) {
   1123     llvm::SmallString<16> Prefix;
   1124     llvm::SmallString<8> Suffix;
   1125     // The actual spelling of the name and namespace (if applicable)
   1126     // of an attribute without considering prefix and suffix.
   1127     llvm::SmallString<64> Spelling;
   1128     std::string Name = Spellings[I].name();
   1129     std::string Variety = Spellings[I].variety();
   1130 
   1131     if (Variety == "GNU") {
   1132       Prefix = " __attribute__((";
   1133       Suffix = "))";
   1134     } else if (Variety == "CXX11") {
   1135       Prefix = " [[";
   1136       Suffix = "]]";
   1137       std::string Namespace = Spellings[I].nameSpace();
   1138       if (!Namespace.empty()) {
   1139         Spelling += Namespace;
   1140         Spelling += "::";
   1141       }
   1142     } else if (Variety == "Declspec") {
   1143       Prefix = " __declspec(";
   1144       Suffix = ")";
   1145     } else if (Variety == "Keyword") {
   1146       Prefix = " ";
   1147       Suffix = "";
   1148     } else if (Variety == "Pragma") {
   1149       Prefix = "#pragma ";
   1150       Suffix = "\n";
   1151       std::string Namespace = Spellings[I].nameSpace();
   1152       if (!Namespace.empty()) {
   1153         Spelling += Namespace;
   1154         Spelling += " ";
   1155       }
   1156     } else {
   1157       llvm_unreachable("Unknown attribute syntax variety!");
   1158     }
   1159 
   1160     Spelling += Name;
   1161 
   1162     OS <<
   1163       "  case " << I << " : {\n"
   1164       "    OS << \"" << Prefix << Spelling;
   1165 
   1166     if (Variety == "Pragma") {
   1167       OS << " \";\n";
   1168       OS << "    printPrettyPragma(OS, Policy);\n";
   1169       OS << "    break;\n";
   1170       OS << "  }\n";
   1171       continue;
   1172     }
   1173 
   1174     // FIXME: always printing the parenthesis isn't the correct behavior for
   1175     // attributes which have optional arguments that were not provided. For
   1176     // instance: __attribute__((aligned)) will be pretty printed as
   1177     // __attribute__((aligned())). The logic should check whether there is only
   1178     // a single argument, and if it is optional, whether it has been provided.
   1179     if (!Args.empty())
   1180       OS << "(";
   1181     if (Spelling == "availability") {
   1182       writeAvailabilityValue(OS);
   1183     } else {
   1184       for (auto I = Args.begin(), E = Args.end(); I != E; ++ I) {
   1185         if (I != Args.begin()) OS << ", ";
   1186         (*I)->writeValue(OS);
   1187       }
   1188     }
   1189 
   1190     if (!Args.empty())
   1191       OS << ")";
   1192     OS << Suffix + "\";\n";
   1193 
   1194     OS <<
   1195       "    break;\n"
   1196       "  }\n";
   1197   }
   1198 
   1199   // End of the switch statement.
   1200   OS << "}\n";
   1201   // End of the print function.
   1202   OS << "}\n\n";
   1203 }
   1204 
   1205 /// \brief Return the index of a spelling in a spelling list.
   1206 static unsigned
   1207 getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList,
   1208                      const FlattenedSpelling &Spelling) {
   1209   assert(!SpellingList.empty() && "Spelling list is empty!");
   1210 
   1211   for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
   1212     const FlattenedSpelling &S = SpellingList[Index];
   1213     if (S.variety() != Spelling.variety())
   1214       continue;
   1215     if (S.nameSpace() != Spelling.nameSpace())
   1216       continue;
   1217     if (S.name() != Spelling.name())
   1218       continue;
   1219 
   1220     return Index;
   1221   }
   1222 
   1223   llvm_unreachable("Unknown spelling!");
   1224 }
   1225 
   1226 static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) {
   1227   std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
   1228   for (const auto *Accessor : Accessors) {
   1229     std::string Name = Accessor->getValueAsString("Name");
   1230     std::vector<FlattenedSpelling> Spellings =
   1231       GetFlattenedSpellings(*Accessor);
   1232     std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R);
   1233     assert(!SpellingList.empty() &&
   1234            "Attribute with empty spelling list can't have accessors!");
   1235 
   1236     OS << "  bool " << Name << "() const { return SpellingListIndex == ";
   1237     for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
   1238       OS << getSpellingListIndex(SpellingList, Spellings[Index]);
   1239       if (Index != Spellings.size() -1)
   1240         OS << " ||\n    SpellingListIndex == ";
   1241       else
   1242         OS << "; }\n";
   1243     }
   1244   }
   1245 }
   1246 
   1247 static bool
   1248 SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) {
   1249   assert(!Spellings.empty() && "An empty list of spellings was provided");
   1250   std::string FirstName = NormalizeNameForSpellingComparison(
   1251     Spellings.front().name());
   1252   for (const auto &Spelling :
   1253        llvm::make_range(std::next(Spellings.begin()), Spellings.end())) {
   1254     std::string Name = NormalizeNameForSpellingComparison(Spelling.name());
   1255     if (Name != FirstName)
   1256       return false;
   1257   }
   1258   return true;
   1259 }
   1260 
   1261 typedef std::map<unsigned, std::string> SemanticSpellingMap;
   1262 static std::string
   1263 CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings,
   1264                         SemanticSpellingMap &Map) {
   1265   // The enumerants are automatically generated based on the variety,
   1266   // namespace (if present) and name for each attribute spelling. However,
   1267   // care is taken to avoid trampling on the reserved namespace due to
   1268   // underscores.
   1269   std::string Ret("  enum Spelling {\n");
   1270   std::set<std::string> Uniques;
   1271   unsigned Idx = 0;
   1272   for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) {
   1273     const FlattenedSpelling &S = *I;
   1274     std::string Variety = S.variety();
   1275     std::string Spelling = S.name();
   1276     std::string Namespace = S.nameSpace();
   1277     std::string EnumName = "";
   1278 
   1279     EnumName += (Variety + "_");
   1280     if (!Namespace.empty())
   1281       EnumName += (NormalizeNameForSpellingComparison(Namespace).str() +
   1282       "_");
   1283     EnumName += NormalizeNameForSpellingComparison(Spelling);
   1284 
   1285     // Even if the name is not unique, this spelling index corresponds to a
   1286     // particular enumerant name that we've calculated.
   1287     Map[Idx] = EnumName;
   1288 
   1289     // Since we have been stripping underscores to avoid trampling on the
   1290     // reserved namespace, we may have inadvertently created duplicate
   1291     // enumerant names. These duplicates are not considered part of the
   1292     // semantic spelling, and can be elided.
   1293     if (Uniques.find(EnumName) != Uniques.end())
   1294       continue;
   1295 
   1296     Uniques.insert(EnumName);
   1297     if (I != Spellings.begin())
   1298       Ret += ",\n";
   1299     // Duplicate spellings are not considered part of the semantic spelling
   1300     // enumeration, but the spelling index and semantic spelling values are
   1301     // meant to be equivalent, so we must specify a concrete value for each
   1302     // enumerator.
   1303     Ret += "    " + EnumName + " = " + llvm::utostr(Idx);
   1304   }
   1305   Ret += "\n  };\n\n";
   1306   return Ret;
   1307 }
   1308 
   1309 void WriteSemanticSpellingSwitch(const std::string &VarName,
   1310                                  const SemanticSpellingMap &Map,
   1311                                  raw_ostream &OS) {
   1312   OS << "  switch (" << VarName << ") {\n    default: "
   1313     << "llvm_unreachable(\"Unknown spelling list index\");\n";
   1314   for (const auto &I : Map)
   1315     OS << "    case " << I.first << ": return " << I.second << ";\n";
   1316   OS << "  }\n";
   1317 }
   1318 
   1319 // Emits the LateParsed property for attributes.
   1320 static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
   1321   OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n";
   1322   std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
   1323 
   1324   for (const auto *Attr : Attrs) {
   1325     bool LateParsed = Attr->getValueAsBit("LateParsed");
   1326 
   1327     if (LateParsed) {
   1328       std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
   1329 
   1330       // FIXME: Handle non-GNU attributes
   1331       for (const auto &I : Spellings) {
   1332         if (I.variety() != "GNU")
   1333           continue;
   1334         OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n";
   1335       }
   1336     }
   1337   }
   1338   OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n";
   1339 }
   1340 
   1341 /// \brief Emits the first-argument-is-type property for attributes.
   1342 static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) {
   1343   OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n";
   1344   std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
   1345 
   1346   for (const auto *Attr : Attrs) {
   1347     // Determine whether the first argument is a type.
   1348     std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
   1349     if (Args.empty())
   1350       continue;
   1351 
   1352     if (Args[0]->getSuperClasses().back()->getName() != "TypeArgument")
   1353       continue;
   1354 
   1355     // All these spellings take a single type argument.
   1356     std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
   1357     std::set<std::string> Emitted;
   1358     for (const auto &S : Spellings) {
   1359       if (Emitted.insert(S.name()).second)
   1360         OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
   1361     }
   1362   }
   1363   OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n";
   1364 }
   1365 
   1366 /// \brief Emits the parse-arguments-in-unevaluated-context property for
   1367 /// attributes.
   1368 static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) {
   1369   OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n";
   1370   ParsedAttrMap Attrs = getParsedAttrList(Records);
   1371   for (const auto &I : Attrs) {
   1372     const Record &Attr = *I.second;
   1373 
   1374     if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated"))
   1375       continue;
   1376 
   1377     // All these spellings take are parsed unevaluated.
   1378     std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
   1379     std::set<std::string> Emitted;
   1380     for (const auto &S : Spellings) {
   1381       if (Emitted.insert(S.name()).second)
   1382         OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
   1383     }
   1384   }
   1385   OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n";
   1386 }
   1387 
   1388 static bool isIdentifierArgument(Record *Arg) {
   1389   return !Arg->getSuperClasses().empty() &&
   1390     llvm::StringSwitch<bool>(Arg->getSuperClasses().back()->getName())
   1391     .Case("IdentifierArgument", true)
   1392     .Case("EnumArgument", true)
   1393     .Case("VariadicEnumArgument", true)
   1394     .Default(false);
   1395 }
   1396 
   1397 // Emits the first-argument-is-identifier property for attributes.
   1398 static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) {
   1399   OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n";
   1400   std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
   1401 
   1402   for (const auto *Attr : Attrs) {
   1403     // Determine whether the first argument is an identifier.
   1404     std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
   1405     if (Args.empty() || !isIdentifierArgument(Args[0]))
   1406       continue;
   1407 
   1408     // All these spellings take an identifier argument.
   1409     std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
   1410     std::set<std::string> Emitted;
   1411     for (const auto &S : Spellings) {
   1412       if (Emitted.insert(S.name()).second)
   1413         OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
   1414     }
   1415   }
   1416   OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n";
   1417 }
   1418 
   1419 namespace clang {
   1420 
   1421 // Emits the class definitions for attributes.
   1422 void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
   1423   emitSourceFileHeader("Attribute classes' definitions", OS);
   1424 
   1425   OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
   1426   OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
   1427 
   1428   std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
   1429 
   1430   for (const auto *Attr : Attrs) {
   1431     const Record &R = *Attr;
   1432 
   1433     // FIXME: Currently, documentation is generated as-needed due to the fact
   1434     // that there is no way to allow a generated project "reach into" the docs
   1435     // directory (for instance, it may be an out-of-tree build). However, we want
   1436     // to ensure that every attribute has a Documentation field, and produce an
   1437     // error if it has been neglected. Otherwise, the on-demand generation which
   1438     // happens server-side will fail. This code is ensuring that functionality,
   1439     // even though this Emitter doesn't technically need the documentation.
   1440     // When attribute documentation can be generated as part of the build
   1441     // itself, this code can be removed.
   1442     (void)R.getValueAsListOfDefs("Documentation");
   1443 
   1444     if (!R.getValueAsBit("ASTNode"))
   1445       continue;
   1446 
   1447     const std::vector<Record *> Supers = R.getSuperClasses();
   1448     assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
   1449     std::string SuperName;
   1450     for (const auto *Super : llvm::make_range(Supers.rbegin(), Supers.rend())) {
   1451       const Record &R = *Super;
   1452       if (R.getName() != "TargetSpecificAttr" && SuperName.empty())
   1453         SuperName = R.getName();
   1454     }
   1455 
   1456     OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
   1457 
   1458     std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
   1459     std::vector<std::unique_ptr<Argument>> Args;
   1460     Args.reserve(ArgRecords.size());
   1461 
   1462     for (const auto *ArgRecord : ArgRecords) {
   1463       Args.emplace_back(createArgument(*ArgRecord, R.getName()));
   1464       Args.back()->writeDeclarations(OS);
   1465       OS << "\n\n";
   1466     }
   1467 
   1468     OS << "\npublic:\n";
   1469 
   1470     std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
   1471 
   1472     // If there are zero or one spellings, all spelling-related functionality
   1473     // can be elided. If all of the spellings share the same name, the spelling
   1474     // functionality can also be elided.
   1475     bool ElideSpelling = (Spellings.size() <= 1) ||
   1476                          SpellingNamesAreCommon(Spellings);
   1477 
   1478     // This maps spelling index values to semantic Spelling enumerants.
   1479     SemanticSpellingMap SemanticToSyntacticMap;
   1480 
   1481     if (!ElideSpelling)
   1482       OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
   1483 
   1484     OS << "  static " << R.getName() << "Attr *CreateImplicit(";
   1485     OS << "ASTContext &Ctx";
   1486     if (!ElideSpelling)
   1487       OS << ", Spelling S";
   1488     for (auto const &ai : Args) {
   1489       OS << ", ";
   1490       ai->writeCtorParameters(OS);
   1491     }
   1492     OS << ", SourceRange Loc = SourceRange()";
   1493     OS << ") {\n";
   1494     OS << "    " << R.getName() << "Attr *A = new (Ctx) " << R.getName();
   1495     OS << "Attr(Loc, Ctx, ";
   1496     for (auto const &ai : Args) {
   1497       ai->writeImplicitCtorArgs(OS);
   1498       OS << ", ";
   1499     }
   1500     OS << (ElideSpelling ? "0" : "S") << ");\n";
   1501     OS << "    A->setImplicit(true);\n";
   1502     OS << "    return A;\n  }\n\n";
   1503 
   1504     OS << "  " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
   1505 
   1506     bool HasOpt = false;
   1507     for (auto const &ai : Args) {
   1508       OS << "              , ";
   1509       ai->writeCtorParameters(OS);
   1510       OS << "\n";
   1511       if (ai->isOptional())
   1512         HasOpt = true;
   1513     }
   1514 
   1515     OS << "              , ";
   1516     OS << "unsigned SI\n";
   1517 
   1518     OS << "             )\n";
   1519     OS << "    : " << SuperName << "(attr::" << R.getName() << ", R, SI, "
   1520        << R.getValueAsBit("LateParsed") << ", "
   1521        << R.getValueAsBit("DuplicatesAllowedWhileMerging") << ")\n";
   1522 
   1523     for (auto const &ai : Args) {
   1524       OS << "              , ";
   1525       ai->writeCtorInitializers(OS);
   1526       OS << "\n";
   1527     }
   1528 
   1529     OS << "  {\n";
   1530 
   1531     for (auto const &ai : Args) {
   1532       ai->writeCtorBody(OS);
   1533       OS << "\n";
   1534     }
   1535     OS << "  }\n\n";
   1536 
   1537     // If there are optional arguments, write out a constructor that elides the
   1538     // optional arguments as well.
   1539     if (HasOpt) {
   1540       OS << "  " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
   1541       for (auto const &ai : Args) {
   1542         if (!ai->isOptional()) {
   1543           OS << "              , ";
   1544           ai->writeCtorParameters(OS);
   1545           OS << "\n";
   1546         }
   1547       }
   1548 
   1549       OS << "              , ";
   1550       OS << "unsigned SI\n";
   1551 
   1552       OS << "             )\n";
   1553       OS << "    : " << SuperName << "(attr::" << R.getName() << ", R, SI, "
   1554          << R.getValueAsBit("LateParsed") << ", "
   1555          << R.getValueAsBit("DuplicatesAllowedWhileMerging") << ")\n";
   1556 
   1557       for (auto const &ai : Args) {
   1558         OS << "              , ";
   1559         ai->writeCtorDefaultInitializers(OS);
   1560         OS << "\n";
   1561       }
   1562 
   1563       OS << "  {\n";
   1564 
   1565       for (auto const &ai : Args) {
   1566         if (!ai->isOptional()) {
   1567           ai->writeCtorBody(OS);
   1568           OS << "\n";
   1569         }
   1570       }
   1571       OS << "  }\n\n";
   1572     }
   1573 
   1574     OS << "  " << R.getName() << "Attr *clone(ASTContext &C) const;\n";
   1575     OS << "  void printPretty(raw_ostream &OS,\n"
   1576        << "                   const PrintingPolicy &Policy) const;\n";
   1577     OS << "  const char *getSpelling() const;\n";
   1578 
   1579     if (!ElideSpelling) {
   1580       assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list");
   1581       OS << "  Spelling getSemanticSpelling() const {\n";
   1582       WriteSemanticSpellingSwitch("SpellingListIndex", SemanticToSyntacticMap,
   1583                                   OS);
   1584       OS << "  }\n";
   1585     }
   1586 
   1587     writeAttrAccessorDefinition(R, OS);
   1588 
   1589     for (auto const &ai : Args) {
   1590       ai->writeAccessors(OS);
   1591       OS << "\n\n";
   1592 
   1593       if (ai->isEnumArg())
   1594         static_cast<const EnumArgument *>(ai.get())->writeConversion(OS);
   1595       else if (ai->isVariadicEnumArg())
   1596         static_cast<const VariadicEnumArgument *>(ai.get())
   1597             ->writeConversion(OS);
   1598     }
   1599 
   1600     OS << R.getValueAsString("AdditionalMembers");
   1601     OS << "\n\n";
   1602 
   1603     OS << "  static bool classof(const Attr *A) { return A->getKind() == "
   1604        << "attr::" << R.getName() << "; }\n";
   1605 
   1606     OS << "};\n\n";
   1607   }
   1608 
   1609   OS << "#endif\n";
   1610 }
   1611 
   1612 // Emits the class method definitions for attributes.
   1613 void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
   1614   emitSourceFileHeader("Attribute classes' member function definitions", OS);
   1615 
   1616   std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
   1617 
   1618   for (auto *Attr : Attrs) {
   1619     Record &R = *Attr;
   1620 
   1621     if (!R.getValueAsBit("ASTNode"))
   1622       continue;
   1623 
   1624     std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
   1625     std::vector<std::unique_ptr<Argument>> Args;
   1626     for (const auto *Arg : ArgRecords)
   1627       Args.emplace_back(createArgument(*Arg, R.getName()));
   1628 
   1629     for (auto const &ai : Args)
   1630       ai->writeAccessorDefinitions(OS);
   1631 
   1632     OS << R.getName() << "Attr *" << R.getName()
   1633        << "Attr::clone(ASTContext &C) const {\n";
   1634     OS << "  auto *A = new (C) " << R.getName() << "Attr(getLocation(), C";
   1635     for (auto const &ai : Args) {
   1636       OS << ", ";
   1637       ai->writeCloneArgs(OS);
   1638     }
   1639     OS << ", getSpellingListIndex());\n";
   1640     OS << "  A->Inherited = Inherited;\n";
   1641     OS << "  A->IsPackExpansion = IsPackExpansion;\n";
   1642     OS << "  A->Implicit = Implicit;\n";
   1643     OS << "  return A;\n}\n\n";
   1644 
   1645     writePrettyPrintFunction(R, Args, OS);
   1646     writeGetSpellingFunction(R, OS);
   1647   }
   1648 
   1649   // Instead of relying on virtual dispatch we just create a huge dispatch
   1650   // switch. This is both smaller and faster than virtual functions.
   1651   auto EmitFunc = [&](const char *Method) {
   1652     OS << "  switch (getKind()) {\n";
   1653     for (const auto *Attr : Attrs) {
   1654       const Record &R = *Attr;
   1655       if (!R.getValueAsBit("ASTNode"))
   1656         continue;
   1657 
   1658       OS << "  case attr::" << R.getName() << ":\n";
   1659       OS << "    return cast<" << R.getName() << "Attr>(this)->" << Method
   1660          << ";\n";
   1661     }
   1662     OS << "  case attr::NUM_ATTRS:\n";
   1663     OS << "    break;\n";
   1664     OS << "  }\n";
   1665     OS << "  llvm_unreachable(\"Unexpected attribute kind!\");\n";
   1666     OS << "}\n\n";
   1667   };
   1668 
   1669   OS << "const char *Attr::getSpelling() const {\n";
   1670   EmitFunc("getSpelling()");
   1671 
   1672   OS << "Attr *Attr::clone(ASTContext &C) const {\n";
   1673   EmitFunc("clone(C)");
   1674 
   1675   OS << "void Attr::printPretty(raw_ostream &OS, "
   1676         "const PrintingPolicy &Policy) const {\n";
   1677   EmitFunc("printPretty(OS, Policy)");
   1678 }
   1679 
   1680 } // end namespace clang
   1681 
   1682 static void EmitAttrList(raw_ostream &OS, StringRef Class,
   1683                          const std::vector<Record*> &AttrList) {
   1684   std::vector<Record*>::const_iterator i = AttrList.begin(), e = AttrList.end();
   1685 
   1686   if (i != e) {
   1687     // Move the end iterator back to emit the last attribute.
   1688     for(--e; i != e; ++i) {
   1689       if (!(*i)->getValueAsBit("ASTNode"))
   1690         continue;
   1691 
   1692       OS << Class << "(" << (*i)->getName() << ")\n";
   1693     }
   1694 
   1695     OS << "LAST_" << Class << "(" << (*i)->getName() << ")\n\n";
   1696   }
   1697 }
   1698 
   1699 // Determines if an attribute has a Pragma spelling.
   1700 static bool AttrHasPragmaSpelling(const Record *R) {
   1701   std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
   1702   return std::find_if(Spellings.begin(), Spellings.end(),
   1703                       [](const FlattenedSpelling &S) {
   1704            return S.variety() == "Pragma";
   1705          }) != Spellings.end();
   1706 }
   1707 
   1708 namespace clang {
   1709 // Emits the enumeration list for attributes.
   1710 void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
   1711   emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
   1712 
   1713   OS << "#ifndef LAST_ATTR\n";
   1714   OS << "#define LAST_ATTR(NAME) ATTR(NAME)\n";
   1715   OS << "#endif\n\n";
   1716 
   1717   OS << "#ifndef INHERITABLE_ATTR\n";
   1718   OS << "#define INHERITABLE_ATTR(NAME) ATTR(NAME)\n";
   1719   OS << "#endif\n\n";
   1720 
   1721   OS << "#ifndef LAST_INHERITABLE_ATTR\n";
   1722   OS << "#define LAST_INHERITABLE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
   1723   OS << "#endif\n\n";
   1724 
   1725   OS << "#ifndef INHERITABLE_PARAM_ATTR\n";
   1726   OS << "#define INHERITABLE_PARAM_ATTR(NAME) ATTR(NAME)\n";
   1727   OS << "#endif\n\n";
   1728 
   1729   OS << "#ifndef LAST_INHERITABLE_PARAM_ATTR\n";
   1730   OS << "#define LAST_INHERITABLE_PARAM_ATTR(NAME)"
   1731         " INHERITABLE_PARAM_ATTR(NAME)\n";
   1732   OS << "#endif\n\n";
   1733 
   1734   OS << "#ifndef PRAGMA_SPELLING_ATTR\n";
   1735   OS << "#define PRAGMA_SPELLING_ATTR(NAME)\n";
   1736   OS << "#endif\n\n";
   1737 
   1738   OS << "#ifndef LAST_PRAGMA_SPELLING_ATTR\n";
   1739   OS << "#define LAST_PRAGMA_SPELLING_ATTR(NAME) PRAGMA_SPELLING_ATTR(NAME)\n";
   1740   OS << "#endif\n\n";
   1741 
   1742   Record *InhClass = Records.getClass("InheritableAttr");
   1743   Record *InhParamClass = Records.getClass("InheritableParamAttr");
   1744   std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"),
   1745                         NonInhAttrs, InhAttrs, InhParamAttrs, PragmaAttrs;
   1746   for (auto *Attr : Attrs) {
   1747     if (!Attr->getValueAsBit("ASTNode"))
   1748       continue;
   1749 
   1750     if (AttrHasPragmaSpelling(Attr))
   1751       PragmaAttrs.push_back(Attr);
   1752 
   1753     if (Attr->isSubClassOf(InhParamClass))
   1754       InhParamAttrs.push_back(Attr);
   1755     else if (Attr->isSubClassOf(InhClass))
   1756       InhAttrs.push_back(Attr);
   1757     else
   1758       NonInhAttrs.push_back(Attr);
   1759   }
   1760 
   1761   EmitAttrList(OS, "PRAGMA_SPELLING_ATTR", PragmaAttrs);
   1762   EmitAttrList(OS, "INHERITABLE_PARAM_ATTR", InhParamAttrs);
   1763   EmitAttrList(OS, "INHERITABLE_ATTR", InhAttrs);
   1764   EmitAttrList(OS, "ATTR", NonInhAttrs);
   1765 
   1766   OS << "#undef LAST_ATTR\n";
   1767   OS << "#undef INHERITABLE_ATTR\n";
   1768   OS << "#undef LAST_INHERITABLE_ATTR\n";
   1769   OS << "#undef LAST_INHERITABLE_PARAM_ATTR\n";
   1770   OS << "#undef LAST_PRAGMA_ATTR\n";
   1771   OS << "#undef PRAGMA_SPELLING_ATTR\n";
   1772   OS << "#undef ATTR\n";
   1773 }
   1774 
   1775 // Emits the code to read an attribute from a precompiled header.
   1776 void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
   1777   emitSourceFileHeader("Attribute deserialization code", OS);
   1778 
   1779   Record *InhClass = Records.getClass("InheritableAttr");
   1780   std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
   1781                        ArgRecords;
   1782   std::vector<std::unique_ptr<Argument>> Args;
   1783 
   1784   OS << "  switch (Kind) {\n";
   1785   OS << "  default:\n";
   1786   OS << "    llvm_unreachable(\"Unknown attribute!\");\n";
   1787   for (const auto *Attr : Attrs) {
   1788     const Record &R = *Attr;
   1789     if (!R.getValueAsBit("ASTNode"))
   1790       continue;
   1791 
   1792     OS << "  case attr::" << R.getName() << ": {\n";
   1793     if (R.isSubClassOf(InhClass))
   1794       OS << "    bool isInherited = Record[Idx++];\n";
   1795     OS << "    bool isImplicit = Record[Idx++];\n";
   1796     OS << "    unsigned Spelling = Record[Idx++];\n";
   1797     ArgRecords = R.getValueAsListOfDefs("Args");
   1798     Args.clear();
   1799     for (const auto *Arg : ArgRecords) {
   1800       Args.emplace_back(createArgument(*Arg, R.getName()));
   1801       Args.back()->writePCHReadDecls(OS);
   1802     }
   1803     OS << "    New = new (Context) " << R.getName() << "Attr(Range, Context";
   1804     for (auto const &ri : Args) {
   1805       OS << ", ";
   1806       ri->writePCHReadArgs(OS);
   1807     }
   1808     OS << ", Spelling);\n";
   1809     if (R.isSubClassOf(InhClass))
   1810       OS << "    cast<InheritableAttr>(New)->setInherited(isInherited);\n";
   1811     OS << "    New->setImplicit(isImplicit);\n";
   1812     OS << "    break;\n";
   1813     OS << "  }\n";
   1814   }
   1815   OS << "  }\n";
   1816 }
   1817 
   1818 // Emits the code to write an attribute to a precompiled header.
   1819 void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
   1820   emitSourceFileHeader("Attribute serialization code", OS);
   1821 
   1822   Record *InhClass = Records.getClass("InheritableAttr");
   1823   std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
   1824 
   1825   OS << "  switch (A->getKind()) {\n";
   1826   OS << "  default:\n";
   1827   OS << "    llvm_unreachable(\"Unknown attribute kind!\");\n";
   1828   OS << "    break;\n";
   1829   for (const auto *Attr : Attrs) {
   1830     const Record &R = *Attr;
   1831     if (!R.getValueAsBit("ASTNode"))
   1832       continue;
   1833     OS << "  case attr::" << R.getName() << ": {\n";
   1834     Args = R.getValueAsListOfDefs("Args");
   1835     if (R.isSubClassOf(InhClass) || !Args.empty())
   1836       OS << "    const " << R.getName() << "Attr *SA = cast<" << R.getName()
   1837          << "Attr>(A);\n";
   1838     if (R.isSubClassOf(InhClass))
   1839       OS << "    Record.push_back(SA->isInherited());\n";
   1840     OS << "    Record.push_back(A->isImplicit());\n";
   1841     OS << "    Record.push_back(A->getSpellingListIndex());\n";
   1842 
   1843     for (const auto *Arg : Args)
   1844       createArgument(*Arg, R.getName())->writePCHWrite(OS);
   1845     OS << "    break;\n";
   1846     OS << "  }\n";
   1847   }
   1848   OS << "  }\n";
   1849 }
   1850 
   1851 static void GenerateHasAttrSpellingStringSwitch(
   1852     const std::vector<Record *> &Attrs, raw_ostream &OS,
   1853     const std::string &Variety = "", const std::string &Scope = "") {
   1854   for (const auto *Attr : Attrs) {
   1855     // C++11-style attributes have specific version information associated with
   1856     // them. If the attribute has no scope, the version information must not
   1857     // have the default value (1), as that's incorrect. Instead, the unscoped
   1858     // attribute version information should be taken from the SD-6 standing
   1859     // document, which can be found at:
   1860     // https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
   1861     int Version = 1;
   1862 
   1863     if (Variety == "CXX11") {
   1864         std::vector<Record *> Spellings = Attr->getValueAsListOfDefs("Spellings");
   1865         for (const auto &Spelling : Spellings) {
   1866           if (Spelling->getValueAsString("Variety") == "CXX11") {
   1867             Version = static_cast<int>(Spelling->getValueAsInt("Version"));
   1868             if (Scope.empty() && Version == 1)
   1869               PrintError(Spelling->getLoc(), "C++ standard attributes must "
   1870               "have valid version information.");
   1871             break;
   1872           }
   1873       }
   1874     }
   1875 
   1876     // It is assumed that there will be an llvm::Triple object named T within
   1877     // scope that can be used to determine whether the attribute exists in
   1878     // a given target.
   1879     std::string Test;
   1880     if (Attr->isSubClassOf("TargetSpecificAttr")) {
   1881       const Record *R = Attr->getValueAsDef("Target");
   1882       std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
   1883 
   1884       Test += "(";
   1885       for (auto AI = Arches.begin(), AE = Arches.end(); AI != AE; ++AI) {
   1886         std::string Part = *AI;
   1887         Test += "T.getArch() == llvm::Triple::" + Part;
   1888         if (AI + 1 != AE)
   1889           Test += " || ";
   1890       }
   1891       Test += ")";
   1892 
   1893       std::vector<std::string> OSes;
   1894       if (!R->isValueUnset("OSes")) {
   1895         Test += " && (";
   1896         std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes");
   1897         for (auto AI = OSes.begin(), AE = OSes.end(); AI != AE; ++AI) {
   1898           std::string Part = *AI;
   1899 
   1900           Test += "T.getOS() == llvm::Triple::" + Part;
   1901           if (AI + 1 != AE)
   1902             Test += " || ";
   1903         }
   1904         Test += ")";
   1905       }
   1906 
   1907       // If this is the C++11 variety, also add in the LangOpts test.
   1908       if (Variety == "CXX11")
   1909         Test += " && LangOpts.CPlusPlus11";
   1910     } else if (Variety == "CXX11")
   1911       // C++11 mode should be checked against LangOpts, which is presumed to be
   1912       // present in the caller.
   1913       Test = "LangOpts.CPlusPlus11";
   1914 
   1915     std::string TestStr =
   1916         !Test.empty() ? Test + " ? " + llvm::itostr(Version) + " : 0" : "1";
   1917     std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
   1918     for (const auto &S : Spellings)
   1919       if (Variety.empty() || (Variety == S.variety() &&
   1920                               (Scope.empty() || Scope == S.nameSpace())))
   1921         OS << "    .Case(\"" << S.name() << "\", " << TestStr << ")\n";
   1922   }
   1923   OS << "    .Default(0);\n";
   1924 }
   1925 
   1926 // Emits the list of spellings for attributes.
   1927 void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
   1928   emitSourceFileHeader("Code to implement the __has_attribute logic", OS);
   1929 
   1930   // Separate all of the attributes out into four group: generic, C++11, GNU,
   1931   // and declspecs. Then generate a big switch statement for each of them.
   1932   std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
   1933   std::vector<Record *> Declspec, GNU, Pragma;
   1934   std::map<std::string, std::vector<Record *>> CXX;
   1935 
   1936   // Walk over the list of all attributes, and split them out based on the
   1937   // spelling variety.
   1938   for (auto *R : Attrs) {
   1939     std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
   1940     for (const auto &SI : Spellings) {
   1941       std::string Variety = SI.variety();
   1942       if (Variety == "GNU")
   1943         GNU.push_back(R);
   1944       else if (Variety == "Declspec")
   1945         Declspec.push_back(R);
   1946       else if (Variety == "CXX11")
   1947         CXX[SI.nameSpace()].push_back(R);
   1948       else if (Variety == "Pragma")
   1949         Pragma.push_back(R);
   1950     }
   1951   }
   1952 
   1953   OS << "switch (Syntax) {\n";
   1954   OS << "case AttrSyntax::GNU:\n";
   1955   OS << "  return llvm::StringSwitch<int>(Name)\n";
   1956   GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU");
   1957   OS << "case AttrSyntax::Declspec:\n";
   1958   OS << "  return llvm::StringSwitch<int>(Name)\n";
   1959   GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec");
   1960   OS << "case AttrSyntax::Pragma:\n";
   1961   OS << "  return llvm::StringSwitch<int>(Name)\n";
   1962   GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma");
   1963   OS << "case AttrSyntax::CXX: {\n";
   1964   // C++11-style attributes are further split out based on the Scope.
   1965   for (std::map<std::string, std::vector<Record *>>::iterator I = CXX.begin(),
   1966                                                               E = CXX.end();
   1967        I != E; ++I) {
   1968     if (I != CXX.begin())
   1969       OS << " else ";
   1970     if (I->first.empty())
   1971       OS << "if (!Scope || Scope->getName() == \"\") {\n";
   1972     else
   1973       OS << "if (Scope->getName() == \"" << I->first << "\") {\n";
   1974     OS << "  return llvm::StringSwitch<int>(Name)\n";
   1975     GenerateHasAttrSpellingStringSwitch(I->second, OS, "CXX11", I->first);
   1976     OS << "}";
   1977   }
   1978   OS << "\n}\n";
   1979   OS << "}\n";
   1980 }
   1981 
   1982 void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
   1983   emitSourceFileHeader("Code to translate different attribute spellings "
   1984                        "into internal identifiers", OS);
   1985 
   1986   OS <<
   1987     "  switch (AttrKind) {\n"
   1988     "  default:\n"
   1989     "    llvm_unreachable(\"Unknown attribute kind!\");\n"
   1990     "    break;\n";
   1991 
   1992   ParsedAttrMap Attrs = getParsedAttrList(Records);
   1993   for (const auto &I : Attrs) {
   1994     const Record &R = *I.second;
   1995     std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
   1996     OS << "  case AT_" << I.first << ": {\n";
   1997     for (unsigned I = 0; I < Spellings.size(); ++ I) {
   1998       OS << "    if (Name == \"" << Spellings[I].name() << "\" && "
   1999          << "SyntaxUsed == "
   2000          << StringSwitch<unsigned>(Spellings[I].variety())
   2001                 .Case("GNU", 0)
   2002                 .Case("CXX11", 1)
   2003                 .Case("Declspec", 2)
   2004                 .Case("Keyword", 3)
   2005                 .Case("Pragma", 4)
   2006                 .Default(0)
   2007          << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
   2008          << "        return " << I << ";\n";
   2009     }
   2010 
   2011     OS << "    break;\n";
   2012     OS << "  }\n";
   2013   }
   2014 
   2015   OS << "  }\n";
   2016   OS << "  return 0;\n";
   2017 }
   2018 
   2019 // Emits code used by RecursiveASTVisitor to visit attributes
   2020 void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) {
   2021   emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS);
   2022 
   2023   std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
   2024 
   2025   // Write method declarations for Traverse* methods.
   2026   // We emit this here because we only generate methods for attributes that
   2027   // are declared as ASTNodes.
   2028   OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n";
   2029   for (const auto *Attr : Attrs) {
   2030     const Record &R = *Attr;
   2031     if (!R.getValueAsBit("ASTNode"))
   2032       continue;
   2033     OS << "  bool Traverse"
   2034        << R.getName() << "Attr(" << R.getName() << "Attr *A);\n";
   2035     OS << "  bool Visit"
   2036        << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
   2037        << "    return true; \n"
   2038        << "  };\n";
   2039   }
   2040   OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n";
   2041 
   2042   // Write individual Traverse* methods for each attribute class.
   2043   for (const auto *Attr : Attrs) {
   2044     const Record &R = *Attr;
   2045     if (!R.getValueAsBit("ASTNode"))
   2046       continue;
   2047 
   2048     OS << "template <typename Derived>\n"
   2049        << "bool VISITORCLASS<Derived>::Traverse"
   2050        << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
   2051        << "  if (!getDerived().VisitAttr(A))\n"
   2052        << "    return false;\n"
   2053        << "  if (!getDerived().Visit" << R.getName() << "Attr(A))\n"
   2054        << "    return false;\n";
   2055 
   2056     std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
   2057     for (const auto *Arg : ArgRecords)
   2058       createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS);
   2059 
   2060     OS << "  return true;\n";
   2061     OS << "}\n\n";
   2062   }
   2063 
   2064   // Write generic Traverse routine
   2065   OS << "template <typename Derived>\n"
   2066      << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n"
   2067      << "  if (!A)\n"
   2068      << "    return true;\n"
   2069      << "\n"
   2070      << "  switch (A->getKind()) {\n"
   2071      << "    default:\n"
   2072      << "      return true;\n";
   2073 
   2074   for (const auto *Attr : Attrs) {
   2075     const Record &R = *Attr;
   2076     if (!R.getValueAsBit("ASTNode"))
   2077       continue;
   2078 
   2079     OS << "    case attr::" << R.getName() << ":\n"
   2080        << "      return getDerived().Traverse" << R.getName() << "Attr("
   2081        << "cast<" << R.getName() << "Attr>(A));\n";
   2082   }
   2083   OS << "  }\n";  // end case
   2084   OS << "}\n";  // end function
   2085   OS << "#endif  // ATTR_VISITOR_DECLS_ONLY\n";
   2086 }
   2087 
   2088 // Emits code to instantiate dependent attributes on templates.
   2089 void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
   2090   emitSourceFileHeader("Template instantiation code for attributes", OS);
   2091 
   2092   std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
   2093 
   2094   OS << "namespace clang {\n"
   2095      << "namespace sema {\n\n"
   2096      << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
   2097      << "Sema &S,\n"
   2098      << "        const MultiLevelTemplateArgumentList &TemplateArgs) {\n"
   2099      << "  switch (At->getKind()) {\n"
   2100      << "    default:\n"
   2101      << "      break;\n";
   2102 
   2103   for (const auto *Attr : Attrs) {
   2104     const Record &R = *Attr;
   2105     if (!R.getValueAsBit("ASTNode"))
   2106       continue;
   2107 
   2108     OS << "    case attr::" << R.getName() << ": {\n";
   2109     bool ShouldClone = R.getValueAsBit("Clone");
   2110 
   2111     if (!ShouldClone) {
   2112       OS << "      return NULL;\n";
   2113       OS << "    }\n";
   2114       continue;
   2115     }
   2116 
   2117     OS << "      const " << R.getName() << "Attr *A = cast<"
   2118        << R.getName() << "Attr>(At);\n";
   2119     bool TDependent = R.getValueAsBit("TemplateDependent");
   2120 
   2121     if (!TDependent) {
   2122       OS << "      return A->clone(C);\n";
   2123       OS << "    }\n";
   2124       continue;
   2125     }
   2126 
   2127     std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
   2128     std::vector<std::unique_ptr<Argument>> Args;
   2129     Args.reserve(ArgRecords.size());
   2130 
   2131     for (const auto *ArgRecord : ArgRecords)
   2132       Args.emplace_back(createArgument(*ArgRecord, R.getName()));
   2133 
   2134     for (auto const &ai : Args)
   2135       ai->writeTemplateInstantiation(OS);
   2136 
   2137     OS << "      return new (C) " << R.getName() << "Attr(A->getLocation(), C";
   2138     for (auto const &ai : Args) {
   2139       OS << ", ";
   2140       ai->writeTemplateInstantiationArgs(OS);
   2141     }
   2142     OS << ", A->getSpellingListIndex());\n    }\n";
   2143   }
   2144   OS << "  } // end switch\n"
   2145      << "  llvm_unreachable(\"Unknown attribute!\");\n"
   2146      << "  return 0;\n"
   2147      << "}\n\n"
   2148      << "} // end namespace sema\n"
   2149      << "} // end namespace clang\n";
   2150 }
   2151 
   2152 // Emits the list of parsed attributes.
   2153 void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
   2154   emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
   2155 
   2156   OS << "#ifndef PARSED_ATTR\n";
   2157   OS << "#define PARSED_ATTR(NAME) NAME\n";
   2158   OS << "#endif\n\n";
   2159 
   2160   ParsedAttrMap Names = getParsedAttrList(Records);
   2161   for (const auto &I : Names) {
   2162     OS << "PARSED_ATTR(" << I.first << ")\n";
   2163   }
   2164 }
   2165 
   2166 static bool isArgVariadic(const Record &R, StringRef AttrName) {
   2167   return createArgument(R, AttrName)->isVariadic();
   2168 }
   2169 
   2170 static void emitArgInfo(const Record &R, std::stringstream &OS) {
   2171   // This function will count the number of arguments specified for the
   2172   // attribute and emit the number of required arguments followed by the
   2173   // number of optional arguments.
   2174   std::vector<Record *> Args = R.getValueAsListOfDefs("Args");
   2175   unsigned ArgCount = 0, OptCount = 0;
   2176   bool HasVariadic = false;
   2177   for (const auto *Arg : Args) {
   2178     Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount;
   2179     if (!HasVariadic && isArgVariadic(*Arg, R.getName()))
   2180       HasVariadic = true;
   2181   }
   2182 
   2183   // If there is a variadic argument, we will set the optional argument count
   2184   // to its largest value. Since it's currently a 4-bit number, we set it to 15.
   2185   OS << ArgCount << ", " << (HasVariadic ? 15 : OptCount);
   2186 }
   2187 
   2188 static void GenerateDefaultAppertainsTo(raw_ostream &OS) {
   2189   OS << "static bool defaultAppertainsTo(Sema &, const AttributeList &,";
   2190   OS << "const Decl *) {\n";
   2191   OS << "  return true;\n";
   2192   OS << "}\n\n";
   2193 }
   2194 
   2195 static std::string CalculateDiagnostic(const Record &S) {
   2196   // If the SubjectList object has a custom diagnostic associated with it,
   2197   // return that directly.
   2198   std::string CustomDiag = S.getValueAsString("CustomDiag");
   2199   if (!CustomDiag.empty())
   2200     return CustomDiag;
   2201 
   2202   // Given the list of subjects, determine what diagnostic best fits.
   2203   enum {
   2204     Func = 1U << 0,
   2205     Var = 1U << 1,
   2206     ObjCMethod = 1U << 2,
   2207     Param = 1U << 3,
   2208     Class = 1U << 4,
   2209     GenericRecord = 1U << 5,
   2210     Type = 1U << 6,
   2211     ObjCIVar = 1U << 7,
   2212     ObjCProp = 1U << 8,
   2213     ObjCInterface = 1U << 9,
   2214     Block = 1U << 10,
   2215     Namespace = 1U << 11,
   2216     Field = 1U << 12,
   2217     CXXMethod = 1U << 13,
   2218     ObjCProtocol = 1U << 14,
   2219     Enum = 1U << 15
   2220   };
   2221   uint32_t SubMask = 0;
   2222 
   2223   std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects");
   2224   for (const auto *Subject : Subjects) {
   2225     const Record &R = *Subject;
   2226     std::string Name;
   2227 
   2228     if (R.isSubClassOf("SubsetSubject")) {
   2229       PrintError(R.getLoc(), "SubsetSubjects should use a custom diagnostic");
   2230       // As a fallback, look through the SubsetSubject to see what its base
   2231       // type is, and use that. This needs to be updated if SubsetSubjects
   2232       // are allowed within other SubsetSubjects.
   2233       Name = R.getValueAsDef("Base")->getName();
   2234     } else
   2235       Name = R.getName();
   2236 
   2237     uint32_t V = StringSwitch<uint32_t>(Name)
   2238                    .Case("Function", Func)
   2239                    .Case("Var", Var)
   2240                    .Case("ObjCMethod", ObjCMethod)
   2241                    .Case("ParmVar", Param)
   2242                    .Case("TypedefName", Type)
   2243                    .Case("ObjCIvar", ObjCIVar)
   2244                    .Case("ObjCProperty", ObjCProp)
   2245                    .Case("Record", GenericRecord)
   2246                    .Case("ObjCInterface", ObjCInterface)
   2247                    .Case("ObjCProtocol", ObjCProtocol)
   2248                    .Case("Block", Block)
   2249                    .Case("CXXRecord", Class)
   2250                    .Case("Namespace", Namespace)
   2251                    .Case("Field", Field)
   2252                    .Case("CXXMethod", CXXMethod)
   2253                    .Case("Enum", Enum)
   2254                    .Default(0);
   2255     if (!V) {
   2256       // Something wasn't in our mapping, so be helpful and let the developer
   2257       // know about it.
   2258       PrintFatalError(R.getLoc(), "Unknown subject type: " + R.getName());
   2259       return "";
   2260     }
   2261 
   2262     SubMask |= V;
   2263   }
   2264 
   2265   switch (SubMask) {
   2266     // For the simple cases where there's only a single entry in the mask, we
   2267     // don't have to resort to bit fiddling.
   2268     case Func:  return "ExpectedFunction";
   2269     case Var:   return "ExpectedVariable";
   2270     case Param: return "ExpectedParameter";
   2271     case Class: return "ExpectedClass";
   2272     case Enum:  return "ExpectedEnum";
   2273     case CXXMethod:
   2274       // FIXME: Currently, this maps to ExpectedMethod based on existing code,
   2275       // but should map to something a bit more accurate at some point.
   2276     case ObjCMethod:  return "ExpectedMethod";
   2277     case Type:  return "ExpectedType";
   2278     case ObjCInterface: return "ExpectedObjectiveCInterface";
   2279     case ObjCProtocol: return "ExpectedObjectiveCProtocol";
   2280 
   2281     // "GenericRecord" means struct, union or class; check the language options
   2282     // and if not compiling for C++, strip off the class part. Note that this
   2283     // relies on the fact that the context for this declares "Sema &S".
   2284     case GenericRecord:
   2285       return "(S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass : "
   2286                                            "ExpectedStructOrUnion)";
   2287     case Func | ObjCMethod | Block: return "ExpectedFunctionMethodOrBlock";
   2288     case Func | ObjCMethod | Class: return "ExpectedFunctionMethodOrClass";
   2289     case Func | Param:
   2290     case Func | ObjCMethod | Param: return "ExpectedFunctionMethodOrParameter";
   2291     case Func | ObjCMethod: return "ExpectedFunctionOrMethod";
   2292     case Func | Var: return "ExpectedVariableOrFunction";
   2293 
   2294     // If not compiling for C++, the class portion does not apply.
   2295     case Func | Var | Class:
   2296       return "(S.getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass : "
   2297                                            "ExpectedVariableOrFunction)";
   2298 
   2299     case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty";
   2300     case ObjCProtocol | ObjCInterface:
   2301       return "ExpectedObjectiveCInterfaceOrProtocol";
   2302     case Field | Var: return "ExpectedFieldOrGlobalVar";
   2303   }
   2304 
   2305   PrintFatalError(S.getLoc(),
   2306                   "Could not deduce diagnostic argument for Attr subjects");
   2307 
   2308   return "";
   2309 }
   2310 
   2311 static std::string GetSubjectWithSuffix(const Record *R) {
   2312   std::string B = R->getName();
   2313   if (B == "DeclBase")
   2314     return "Decl";
   2315   return B + "Decl";
   2316 }
   2317 static std::string GenerateCustomAppertainsTo(const Record &Subject,
   2318                                               raw_ostream &OS) {
   2319   std::string FnName = "is" + Subject.getName();
   2320 
   2321   // If this code has already been generated, simply return the previous
   2322   // instance of it.
   2323   static std::set<std::string> CustomSubjectSet;
   2324   std::set<std::string>::iterator I = CustomSubjectSet.find(FnName);
   2325   if (I != CustomSubjectSet.end())
   2326     return *I;
   2327 
   2328   Record *Base = Subject.getValueAsDef("Base");
   2329 
   2330   // Not currently support custom subjects within custom subjects.
   2331   if (Base->isSubClassOf("SubsetSubject")) {
   2332     PrintFatalError(Subject.getLoc(),
   2333                     "SubsetSubjects within SubsetSubjects is not supported");
   2334     return "";
   2335   }
   2336 
   2337   OS << "static bool " << FnName << "(const Decl *D) {\n";
   2338   OS << "  if (const " << GetSubjectWithSuffix(Base) << " *S = dyn_cast<";
   2339   OS << GetSubjectWithSuffix(Base);
   2340   OS << ">(D))\n";
   2341   OS << "    return " << Subject.getValueAsString("CheckCode") << ";\n";
   2342   OS << "  return false;\n";
   2343   OS << "}\n\n";
   2344 
   2345   CustomSubjectSet.insert(FnName);
   2346   return FnName;
   2347 }
   2348 
   2349 static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
   2350   // If the attribute does not contain a Subjects definition, then use the
   2351   // default appertainsTo logic.
   2352   if (Attr.isValueUnset("Subjects"))
   2353     return "defaultAppertainsTo";
   2354 
   2355   const Record *SubjectObj = Attr.getValueAsDef("Subjects");
   2356   std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
   2357 
   2358   // If the list of subjects is empty, it is assumed that the attribute
   2359   // appertains to everything.
   2360   if (Subjects.empty())
   2361     return "defaultAppertainsTo";
   2362 
   2363   bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn");
   2364 
   2365   // Otherwise, generate an appertainsTo check specific to this attribute which
   2366   // checks all of the given subjects against the Decl passed in. Return the
   2367   // name of that check to the caller.
   2368   std::string FnName = "check" + Attr.getName() + "AppertainsTo";
   2369   std::stringstream SS;
   2370   SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, ";
   2371   SS << "const Decl *D) {\n";
   2372   SS << "  if (";
   2373   for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
   2374     // If the subject has custom code associated with it, generate a function
   2375     // for it. The function cannot be inlined into this check (yet) because it
   2376     // requires the subject to be of a specific type, and were that information
   2377     // inlined here, it would not support an attribute with multiple custom
   2378     // subjects.
   2379     if ((*I)->isSubClassOf("SubsetSubject")) {
   2380       SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)";
   2381     } else {
   2382       SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)";
   2383     }
   2384 
   2385     if (I + 1 != E)
   2386       SS << " && ";
   2387   }
   2388   SS << ") {\n";
   2389   SS << "    S.Diag(Attr.getLoc(), diag::";
   2390   SS << (Warn ? "warn_attribute_wrong_decl_type" :
   2391                "err_attribute_wrong_decl_type");
   2392   SS << ")\n";
   2393   SS << "      << Attr.getName() << ";
   2394   SS << CalculateDiagnostic(*SubjectObj) << ";\n";
   2395   SS << "    return false;\n";
   2396   SS << "  }\n";
   2397   SS << "  return true;\n";
   2398   SS << "}\n\n";
   2399 
   2400   OS << SS.str();
   2401   return FnName;
   2402 }
   2403 
   2404 static void GenerateDefaultLangOptRequirements(raw_ostream &OS) {
   2405   OS << "static bool defaultDiagnoseLangOpts(Sema &, ";
   2406   OS << "const AttributeList &) {\n";
   2407   OS << "  return true;\n";
   2408   OS << "}\n\n";
   2409 }
   2410 
   2411 static std::string GenerateLangOptRequirements(const Record &R,
   2412                                                raw_ostream &OS) {
   2413   // If the attribute has an empty or unset list of language requirements,
   2414   // return the default handler.
   2415   std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts");
   2416   if (LangOpts.empty())
   2417     return "defaultDiagnoseLangOpts";
   2418 
   2419   // Generate the test condition, as well as a unique function name for the
   2420   // diagnostic test. The list of options should usually be short (one or two
   2421   // options), and the uniqueness isn't strictly necessary (it is just for
   2422   // codegen efficiency).
   2423   std::string FnName = "check", Test;
   2424   for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) {
   2425     std::string Part = (*I)->getValueAsString("Name");
   2426     if ((*I)->getValueAsBit("Negated"))
   2427       Test += "!";
   2428     Test += "S.LangOpts." + Part;
   2429     if (I + 1 != E)
   2430       Test += " || ";
   2431     FnName += Part;
   2432   }
   2433   FnName += "LangOpts";
   2434 
   2435   // If this code has already been generated, simply return the previous
   2436   // instance of it.
   2437   static std::set<std::string> CustomLangOptsSet;
   2438   std::set<std::string>::iterator I = CustomLangOptsSet.find(FnName);
   2439   if (I != CustomLangOptsSet.end())
   2440     return *I;
   2441 
   2442   OS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr) {\n";
   2443   OS << "  if (" << Test << ")\n";
   2444   OS << "    return true;\n\n";
   2445   OS << "  S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) ";
   2446   OS << "<< Attr.getName();\n";
   2447   OS << "  return false;\n";
   2448   OS << "}\n\n";
   2449 
   2450   CustomLangOptsSet.insert(FnName);
   2451   return FnName;
   2452 }
   2453 
   2454 static void GenerateDefaultTargetRequirements(raw_ostream &OS) {
   2455   OS << "static bool defaultTargetRequirements(const llvm::Triple &) {\n";
   2456   OS << "  return true;\n";
   2457   OS << "}\n\n";
   2458 }
   2459 
   2460 static std::string GenerateTargetRequirements(const Record &Attr,
   2461                                               const ParsedAttrMap &Dupes,
   2462                                               raw_ostream &OS) {
   2463   // If the attribute is not a target specific attribute, return the default
   2464   // target handler.
   2465   if (!Attr.isSubClassOf("TargetSpecificAttr"))
   2466     return "defaultTargetRequirements";
   2467 
   2468   // Get the list of architectures to be tested for.
   2469   const Record *R = Attr.getValueAsDef("Target");
   2470   std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
   2471   if (Arches.empty()) {
   2472     PrintError(Attr.getLoc(), "Empty list of target architectures for a "
   2473                               "target-specific attr");
   2474     return "defaultTargetRequirements";
   2475   }
   2476 
   2477   // If there are other attributes which share the same parsed attribute kind,
   2478   // such as target-specific attributes with a shared spelling, collapse the
   2479   // duplicate architectures. This is required because a shared target-specific
   2480   // attribute has only one AttributeList::Kind enumeration value, but it
   2481   // applies to multiple target architectures. In order for the attribute to be
   2482   // considered valid, all of its architectures need to be included.
   2483   if (!Attr.isValueUnset("ParseKind")) {
   2484     std::string APK = Attr.getValueAsString("ParseKind");
   2485     for (const auto &I : Dupes) {
   2486       if (I.first == APK) {
   2487         std::vector<std::string> DA = I.second->getValueAsDef("Target")
   2488                                           ->getValueAsListOfStrings("Arches");
   2489         std::copy(DA.begin(), DA.end(), std::back_inserter(Arches));
   2490       }
   2491     }
   2492   }
   2493 
   2494   std::string FnName = "isTarget", Test = "(";
   2495   for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) {
   2496     std::string Part = *I;
   2497     Test += "Arch == llvm::Triple::" + Part;
   2498     if (I + 1 != E)
   2499       Test += " || ";
   2500     FnName += Part;
   2501   }
   2502   Test += ")";
   2503 
   2504   // If the target also requires OS testing, generate those tests as well.
   2505   bool UsesOS = false;
   2506   if (!R->isValueUnset("OSes")) {
   2507     UsesOS = true;
   2508 
   2509     // We know that there was at least one arch test, so we need to and in the
   2510     // OS tests.
   2511     Test += " && (";
   2512     std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes");
   2513     for (auto I = OSes.begin(), E = OSes.end(); I != E; ++I) {
   2514       std::string Part = *I;
   2515 
   2516       Test += "OS == llvm::Triple::" + Part;
   2517       if (I + 1 != E)
   2518         Test += " || ";
   2519       FnName += Part;
   2520     }
   2521     Test += ")";
   2522   }
   2523 
   2524   // If this code has already been generated, simply return the previous
   2525   // instance of it.
   2526   static std::set<std::string> CustomTargetSet;
   2527   std::set<std::string>::iterator I = CustomTargetSet.find(FnName);
   2528   if (I != CustomTargetSet.end())
   2529     return *I;
   2530 
   2531   OS << "static bool " << FnName << "(const llvm::Triple &T) {\n";
   2532   OS << "  llvm::Triple::ArchType Arch = T.getArch();\n";
   2533   if (UsesOS)
   2534     OS << "  llvm::Triple::OSType OS = T.getOS();\n";
   2535   OS << "  return " << Test << ";\n";
   2536   OS << "}\n\n";
   2537 
   2538   CustomTargetSet.insert(FnName);
   2539   return FnName;
   2540 }
   2541 
   2542 static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) {
   2543   OS << "static unsigned defaultSpellingIndexToSemanticSpelling("
   2544      << "const AttributeList &Attr) {\n";
   2545   OS << "  return UINT_MAX;\n";
   2546   OS << "}\n\n";
   2547 }
   2548 
   2549 static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr,
   2550                                                            raw_ostream &OS) {
   2551   // If the attribute does not have a semantic form, we can bail out early.
   2552   if (!Attr.getValueAsBit("ASTNode"))
   2553     return "defaultSpellingIndexToSemanticSpelling";
   2554 
   2555   std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
   2556 
   2557   // If there are zero or one spellings, or all of the spellings share the same
   2558   // name, we can also bail out early.
   2559   if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings))
   2560     return "defaultSpellingIndexToSemanticSpelling";
   2561 
   2562   // Generate the enumeration we will use for the mapping.
   2563   SemanticSpellingMap SemanticToSyntacticMap;
   2564   std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
   2565   std::string Name = Attr.getName() + "AttrSpellingMap";
   2566 
   2567   OS << "static unsigned " << Name << "(const AttributeList &Attr) {\n";
   2568   OS << Enum;
   2569   OS << "  unsigned Idx = Attr.getAttributeSpellingListIndex();\n";
   2570   WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS);
   2571   OS << "}\n\n";
   2572 
   2573   return Name;
   2574 }
   2575 
   2576 static bool IsKnownToGCC(const Record &Attr) {
   2577   // Look at the spellings for this subject; if there are any spellings which
   2578   // claim to be known to GCC, the attribute is known to GCC.
   2579   std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
   2580   for (const auto &I : Spellings) {
   2581     if (I.knownToGCC())
   2582       return true;
   2583   }
   2584   return false;
   2585 }
   2586 
   2587 /// Emits the parsed attribute helpers
   2588 void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
   2589   emitSourceFileHeader("Parsed attribute helpers", OS);
   2590 
   2591   // Get the list of parsed attributes, and accept the optional list of
   2592   // duplicates due to the ParseKind.
   2593   ParsedAttrMap Dupes;
   2594   ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes);
   2595 
   2596   // Generate the default appertainsTo, target and language option diagnostic,
   2597   // and spelling list index mapping methods.
   2598   GenerateDefaultAppertainsTo(OS);
   2599   GenerateDefaultLangOptRequirements(OS);
   2600   GenerateDefaultTargetRequirements(OS);
   2601   GenerateDefaultSpellingIndexToSemanticSpelling(OS);
   2602 
   2603   // Generate the appertainsTo diagnostic methods and write their names into
   2604   // another mapping. At the same time, generate the AttrInfoMap object
   2605   // contents. Due to the reliance on generated code, use separate streams so
   2606   // that code will not be interleaved.
   2607   std::stringstream SS;
   2608   for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
   2609     // TODO: If the attribute's kind appears in the list of duplicates, that is
   2610     // because it is a target-specific attribute that appears multiple times.
   2611     // It would be beneficial to test whether the duplicates are "similar
   2612     // enough" to each other to not cause problems. For instance, check that
   2613     // the spellings are identical, and custom parsing rules match, etc.
   2614 
   2615     // We need to generate struct instances based off ParsedAttrInfo from
   2616     // AttributeList.cpp.
   2617     SS << "  { ";
   2618     emitArgInfo(*I->second, SS);
   2619     SS << ", " << I->second->getValueAsBit("HasCustomParsing");
   2620     SS << ", " << I->second->isSubClassOf("TargetSpecificAttr");
   2621     SS << ", " << I->second->isSubClassOf("TypeAttr");
   2622     SS << ", " << IsKnownToGCC(*I->second);
   2623     SS << ", " << GenerateAppertainsTo(*I->second, OS);
   2624     SS << ", " << GenerateLangOptRequirements(*I->second, OS);
   2625     SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS);
   2626     SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS);
   2627     SS << " }";
   2628 
   2629     if (I + 1 != E)
   2630       SS << ",";
   2631 
   2632     SS << "  // AT_" << I->first << "\n";
   2633   }
   2634 
   2635   OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n";
   2636   OS << SS.str();
   2637   OS << "};\n\n";
   2638 }
   2639 
   2640 // Emits the kind list of parsed attributes
   2641 void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
   2642   emitSourceFileHeader("Attribute name matcher", OS);
   2643 
   2644   std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
   2645   std::vector<StringMatcher::StringPair> GNU, Declspec, CXX11, Keywords, Pragma;
   2646   std::set<std::string> Seen;
   2647   for (const auto *A : Attrs) {
   2648     const Record &Attr = *A;
   2649 
   2650     bool SemaHandler = Attr.getValueAsBit("SemaHandler");
   2651     bool Ignored = Attr.getValueAsBit("Ignored");
   2652     if (SemaHandler || Ignored) {
   2653       // Attribute spellings can be shared between target-specific attributes,
   2654       // and can be shared between syntaxes for the same attribute. For
   2655       // instance, an attribute can be spelled GNU<"interrupt"> for an ARM-
   2656       // specific attribute, or MSP430-specific attribute. Additionally, an
   2657       // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport">
   2658       // for the same semantic attribute. Ultimately, we need to map each of
   2659       // these to a single AttributeList::Kind value, but the StringMatcher
   2660       // class cannot handle duplicate match strings. So we generate a list of
   2661       // string to match based on the syntax, and emit multiple string matchers
   2662       // depending on the syntax used.
   2663       std::string AttrName;
   2664       if (Attr.isSubClassOf("TargetSpecificAttr") &&
   2665           !Attr.isValueUnset("ParseKind")) {
   2666         AttrName = Attr.getValueAsString("ParseKind");
   2667         if (Seen.find(AttrName) != Seen.end())
   2668           continue;
   2669         Seen.insert(AttrName);
   2670       } else
   2671         AttrName = NormalizeAttrName(StringRef(Attr.getName())).str();
   2672 
   2673       std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
   2674       for (const auto &S : Spellings) {
   2675         std::string RawSpelling = S.name();
   2676         std::vector<StringMatcher::StringPair> *Matches = nullptr;
   2677         std::string Spelling, Variety = S.variety();
   2678         if (Variety == "CXX11") {
   2679           Matches = &CXX11;
   2680           Spelling += S.nameSpace();
   2681           Spelling += "::";
   2682         } else if (Variety == "GNU")
   2683           Matches = &GNU;
   2684         else if (Variety == "Declspec")
   2685           Matches = &Declspec;
   2686         else if (Variety == "Keyword")
   2687           Matches = &Keywords;
   2688         else if (Variety == "Pragma")
   2689           Matches = &Pragma;
   2690 
   2691         assert(Matches && "Unsupported spelling variety found");
   2692 
   2693         Spelling += NormalizeAttrSpelling(RawSpelling);
   2694         if (SemaHandler)
   2695           Matches->push_back(StringMatcher::StringPair(Spelling,
   2696                               "return AttributeList::AT_" + AttrName + ";"));
   2697         else
   2698           Matches->push_back(StringMatcher::StringPair(Spelling,
   2699                               "return AttributeList::IgnoredAttribute;"));
   2700       }
   2701     }
   2702   }
   2703 
   2704   OS << "static AttributeList::Kind getAttrKind(StringRef Name, ";
   2705   OS << "AttributeList::Syntax Syntax) {\n";
   2706   OS << "  if (AttributeList::AS_GNU == Syntax) {\n";
   2707   StringMatcher("Name", GNU, OS).Emit();
   2708   OS << "  } else if (AttributeList::AS_Declspec == Syntax) {\n";
   2709   StringMatcher("Name", Declspec, OS).Emit();
   2710   OS << "  } else if (AttributeList::AS_CXX11 == Syntax) {\n";
   2711   StringMatcher("Name", CXX11, OS).Emit();
   2712   OS << "  } else if (AttributeList::AS_Keyword == Syntax) {\n";
   2713   StringMatcher("Name", Keywords, OS).Emit();
   2714   OS << "  } else if (AttributeList::AS_Pragma == Syntax) {\n";
   2715   StringMatcher("Name", Pragma, OS).Emit();
   2716   OS << "  }\n";
   2717   OS << "  return AttributeList::UnknownAttribute;\n"
   2718      << "}\n";
   2719 }
   2720 
   2721 // Emits the code to dump an attribute.
   2722 void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
   2723   emitSourceFileHeader("Attribute dumper", OS);
   2724 
   2725   OS <<
   2726     "  switch (A->getKind()) {\n"
   2727     "  default:\n"
   2728     "    llvm_unreachable(\"Unknown attribute kind!\");\n"
   2729     "    break;\n";
   2730   std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
   2731   for (const auto *Attr : Attrs) {
   2732     const Record &R = *Attr;
   2733     if (!R.getValueAsBit("ASTNode"))
   2734       continue;
   2735     OS << "  case attr::" << R.getName() << ": {\n";
   2736 
   2737     // If the attribute has a semantically-meaningful name (which is determined
   2738     // by whether there is a Spelling enumeration for it), then write out the
   2739     // spelling used for the attribute.
   2740     std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
   2741     if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings))
   2742       OS << "    OS << \" \" << A->getSpelling();\n";
   2743 
   2744     Args = R.getValueAsListOfDefs("Args");
   2745     if (!Args.empty()) {
   2746       OS << "    const " << R.getName() << "Attr *SA = cast<" << R.getName()
   2747          << "Attr>(A);\n";
   2748       for (const auto *Arg : Args)
   2749         createArgument(*Arg, R.getName())->writeDump(OS);
   2750 
   2751       for (auto AI = Args.begin(), AE = Args.end(); AI != AE; ++AI)
   2752         createArgument(**AI, R.getName())->writeDumpChildren(OS);
   2753     }
   2754     OS <<
   2755       "    break;\n"
   2756       "  }\n";
   2757   }
   2758   OS << "  }\n";
   2759 }
   2760 
   2761 void EmitClangAttrParserStringSwitches(RecordKeeper &Records,
   2762                                        raw_ostream &OS) {
   2763   emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS);
   2764   emitClangAttrArgContextList(Records, OS);
   2765   emitClangAttrIdentifierArgList(Records, OS);
   2766   emitClangAttrTypeArgList(Records, OS);
   2767   emitClangAttrLateParsedList(Records, OS);
   2768 }
   2769 
   2770 class DocumentationData {
   2771 public:
   2772   const Record *Documentation;
   2773   const Record *Attribute;
   2774 
   2775   DocumentationData(const Record &Documentation, const Record &Attribute)
   2776       : Documentation(&Documentation), Attribute(&Attribute) {}
   2777 };
   2778 
   2779 static void WriteCategoryHeader(const Record *DocCategory,
   2780                                 raw_ostream &OS) {
   2781   const std::string &Name = DocCategory->getValueAsString("Name");
   2782   OS << Name << "\n" << std::string(Name.length(), '=') << "\n";
   2783 
   2784   // If there is content, print that as well.
   2785   std::string ContentStr = DocCategory->getValueAsString("Content");
   2786   // Trim leading and trailing newlines and spaces.
   2787   OS << StringRef(ContentStr).trim();
   2788 
   2789   OS << "\n\n";
   2790 }
   2791 
   2792 enum SpellingKind {
   2793   GNU = 1 << 0,
   2794   CXX11 = 1 << 1,
   2795   Declspec = 1 << 2,
   2796   Keyword = 1 << 3,
   2797   Pragma = 1 << 4
   2798 };
   2799 
   2800 static void WriteDocumentation(const DocumentationData &Doc,
   2801                                raw_ostream &OS) {
   2802   // FIXME: there is no way to have a per-spelling category for the attribute
   2803   // documentation. This may not be a limiting factor since the spellings
   2804   // should generally be consistently applied across the category.
   2805 
   2806   std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Doc.Attribute);
   2807 
   2808   // Determine the heading to be used for this attribute.
   2809   std::string Heading = Doc.Documentation->getValueAsString("Heading");
   2810   bool CustomHeading = !Heading.empty();
   2811   if (Heading.empty()) {
   2812     // If there's only one spelling, we can simply use that.
   2813     if (Spellings.size() == 1)
   2814       Heading = Spellings.begin()->name();
   2815     else {
   2816       std::set<std::string> Uniques;
   2817       for (auto I = Spellings.begin(), E = Spellings.end();
   2818            I != E && Uniques.size() <= 1; ++I) {
   2819         std::string Spelling = NormalizeNameForSpellingComparison(I->name());
   2820         Uniques.insert(Spelling);
   2821       }
   2822       // If the semantic map has only one spelling, that is sufficient for our
   2823       // needs.
   2824       if (Uniques.size() == 1)
   2825         Heading = *Uniques.begin();
   2826     }
   2827   }
   2828 
   2829   // If the heading is still empty, it is an error.
   2830   if (Heading.empty())
   2831     PrintFatalError(Doc.Attribute->getLoc(),
   2832                     "This attribute requires a heading to be specified");
   2833 
   2834   // Gather a list of unique spellings; this is not the same as the semantic
   2835   // spelling for the attribute. Variations in underscores and other non-
   2836   // semantic characters are still acceptable.
   2837   std::vector<std::string> Names;
   2838 
   2839   unsigned SupportedSpellings = 0;
   2840   for (const auto &I : Spellings) {
   2841     SpellingKind Kind = StringSwitch<SpellingKind>(I.variety())
   2842                             .Case("GNU", GNU)
   2843                             .Case("CXX11", CXX11)
   2844                             .Case("Declspec", Declspec)
   2845                             .Case("Keyword", Keyword)
   2846                             .Case("Pragma", Pragma);
   2847 
   2848     // Mask in the supported spelling.
   2849     SupportedSpellings |= Kind;
   2850 
   2851     std::string Name;
   2852     if (Kind == CXX11 && !I.nameSpace().empty())
   2853       Name = I.nameSpace() + "::";
   2854     Name += I.name();
   2855 
   2856     // If this name is the same as the heading, do not add it.
   2857     if (Name != Heading)
   2858       Names.push_back(Name);
   2859   }
   2860 
   2861   // Print out the heading for the attribute. If there are alternate spellings,
   2862   // then display those after the heading.
   2863   if (!CustomHeading && !Names.empty()) {
   2864     Heading += " (";
   2865     for (auto I = Names.begin(), E = Names.end(); I != E; ++I) {
   2866       if (I != Names.begin())
   2867         Heading += ", ";
   2868       Heading += *I;
   2869     }
   2870     Heading += ")";
   2871   }
   2872   OS << Heading << "\n" << std::string(Heading.length(), '-') << "\n";
   2873 
   2874   if (!SupportedSpellings)
   2875     PrintFatalError(Doc.Attribute->getLoc(),
   2876                     "Attribute has no supported spellings; cannot be "
   2877                     "documented");
   2878 
   2879   // List what spelling syntaxes the attribute supports.
   2880   OS << ".. csv-table:: Supported Syntaxes\n";
   2881   OS << "   :header: \"GNU\", \"C++11\", \"__declspec\", \"Keyword\",";
   2882   OS << " \"Pragma\"\n\n";
   2883   OS << "   \"";
   2884   if (SupportedSpellings & GNU) OS << "X";
   2885   OS << "\",\"";
   2886   if (SupportedSpellings & CXX11) OS << "X";
   2887   OS << "\",\"";
   2888   if (SupportedSpellings & Declspec) OS << "X";
   2889   OS << "\",\"";
   2890   if (SupportedSpellings & Keyword) OS << "X";
   2891   OS << "\", \"";
   2892   if (SupportedSpellings & Pragma) OS << "X";
   2893   OS << "\"\n\n";
   2894 
   2895   // If the attribute is deprecated, print a message about it, and possibly
   2896   // provide a replacement attribute.
   2897   if (!Doc.Documentation->isValueUnset("Deprecated")) {
   2898     OS << "This attribute has been deprecated, and may be removed in a future "
   2899        << "version of Clang.";
   2900     const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated");
   2901     std::string Replacement = Deprecated.getValueAsString("Replacement");
   2902     if (!Replacement.empty())
   2903       OS << "  This attribute has been superseded by ``"
   2904          << Replacement << "``.";
   2905     OS << "\n\n";
   2906   }
   2907 
   2908   std::string ContentStr = Doc.Documentation->getValueAsString("Content");
   2909   // Trim leading and trailing newlines and spaces.
   2910   OS << StringRef(ContentStr).trim();
   2911 
   2912   OS << "\n\n\n";
   2913 }
   2914 
   2915 void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) {
   2916   // Get the documentation introduction paragraph.
   2917   const Record *Documentation = Records.getDef("GlobalDocumentation");
   2918   if (!Documentation) {
   2919     PrintFatalError("The Documentation top-level definition is missing, "
   2920                     "no documentation will be generated.");
   2921     return;
   2922   }
   2923 
   2924   OS << Documentation->getValueAsString("Intro") << "\n";
   2925 
   2926   // Gather the Documentation lists from each of the attributes, based on the
   2927   // category provided.
   2928   std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
   2929   std::map<const Record *, std::vector<DocumentationData>> SplitDocs;
   2930   for (const auto *A : Attrs) {
   2931     const Record &Attr = *A;
   2932     std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation");
   2933     for (const auto *D : Docs) {
   2934       const Record &Doc = *D;
   2935       const Record *Category = Doc.getValueAsDef("Category");
   2936       // If the category is "undocumented", then there cannot be any other
   2937       // documentation categories (otherwise, the attribute would become
   2938       // documented).
   2939       std::string Cat = Category->getValueAsString("Name");
   2940       bool Undocumented = Cat == "Undocumented";
   2941       if (Undocumented && Docs.size() > 1)
   2942         PrintFatalError(Doc.getLoc(),
   2943                         "Attribute is \"Undocumented\", but has multiple "
   2944                         "documentation categories");
   2945 
   2946       if (!Undocumented)
   2947         SplitDocs[Category].push_back(DocumentationData(Doc, Attr));
   2948     }
   2949   }
   2950 
   2951   // Having split the attributes out based on what documentation goes where,
   2952   // we can begin to generate sections of documentation.
   2953   for (const auto &I : SplitDocs) {
   2954     WriteCategoryHeader(I.first, OS);
   2955 
   2956     // Walk over each of the attributes in the category and write out their
   2957     // documentation.
   2958     for (const auto &Doc : I.second)
   2959       WriteDocumentation(Doc, OS);
   2960   }
   2961 }
   2962 
   2963 } // end namespace clang
   2964