[serialization] Add support for prefix and postfix operators.

This includes adding an operator lookup table to ModuleFile. Operators are
keyed by a (name, fixity) pair.

Swift SVN r6078
This commit is contained in:
Jordan Rose
2013-07-08 23:41:06 +00:00
parent b31e79e571
commit 929d86bb16
8 changed files with 194 additions and 15 deletions

View File

@@ -811,6 +811,32 @@ Decl *ModuleFile::getDecl(DeclID DID, Optional<DeclContext *> ForcedContext) {
break;
}
case decls_block::PREFIX_OPERATOR_DECL: {
IdentifierID nameID;
DeclID contextID;
decls_block::PrefixOperatorLayout::readRecord(scratch, nameID, contextID);
declOrOffset = new (ctx) PrefixOperatorDecl(getDeclContext(contextID),
SourceLoc(), SourceLoc(),
getIdentifier(nameID),
SourceLoc(), SourceLoc(),
SourceLoc());
break;
}
case decls_block::POSTFIX_OPERATOR_DECL: {
IdentifierID nameID;
DeclID contextID;
decls_block::PostfixOperatorLayout::readRecord(scratch, nameID, contextID);
declOrOffset = new (ctx) PostfixOperatorDecl(getDeclContext(contextID),
SourceLoc(), SourceLoc(),
getIdentifier(nameID),
SourceLoc(), SourceLoc(),
SourceLoc());
break;
}
case decls_block::XREF: {
uint8_t kind;
TypeID expectedTypeID;
@@ -1468,6 +1494,10 @@ ModuleFile::ModuleFile(llvm::OwningPtr<llvm::MemoryBuffer> &&input)
assert(blobData.empty());
RawTopLevelIDs.assign(scratch.begin(), scratch.end());
break;
case index_block::OPERATORS:
assert(blobData.empty());
RawOperatorIDs.assign(scratch.begin(), scratch.end());
break;
default:
// Unknown index kind, which this version of the compiler won't use.
break;
@@ -1544,3 +1574,30 @@ void ModuleFile::lookupValue(Identifier name,
if (DeclID ID = TopLevelIDs.lookup(name))
results.push_back(cast<ValueDecl>(getDecl(ID)));
}
OperatorKind getOperatorKind(DeclKind kind) {
switch (kind) {
case DeclKind::PrefixOperator:
return Prefix;
case DeclKind::PostfixOperator:
return Postfix;
case DeclKind::InfixOperator:
return Infix;
default:
llvm_unreachable("unknown operator fixity");
}
}
OperatorDecl *ModuleFile::lookupOperator(Identifier name, DeclKind fixity) {
if (!RawOperatorIDs.empty()) {
for (DeclID ID : RawOperatorIDs) {
auto op = cast<OperatorDecl>(getDecl(ID));
OperatorKey key(op->getName(), getOperatorKind(op->getKind()));
Operators[key] = op;
}
RawOperatorIDs = {};
}
return Operators.lookup(OperatorKey(name, getOperatorKind(fixity)));
}