From: Aaron Ballman Date: Tue, 14 Jan 2014 01:29:54 +0000 (+0000) Subject: Removing some attribute magic related to the OpenCL keyword attributes. Instead of... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=03e5063fccd8670e7d203c6d66f1b5f9823bbf5b;p=clang Removing some attribute magic related to the OpenCL keyword attributes. Instead of mapping them to their semantics as a custom part of the parser, they instead map declaratively through the rest of the attribute system. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@199175 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/Attr.td b/include/clang/Basic/Attr.td index 9a035994b8..9a403867ee 100644 --- a/include/clang/Basic/Attr.td +++ b/include/clang/Basic/Attr.td @@ -454,10 +454,33 @@ def OpenCLKernel : InheritableAttr { } def OpenCLImageAccess : Attr { - let Spellings = [GNU<"opencl_image_access">]; + let Spellings = [GNU<"opencl_image_access">, + Keyword<"__read_only">, Keyword<"read_only">, + Keyword<"__write_only">, Keyword<"write_only">, + Keyword<"__read_write">, Keyword<"read_write">]; + + // The access argument is used by the GNU syntax when parsing the attribute, + // but is used by the semantic attribute for all syntaxes (the keywords imply + // a specific access value). let Args = [IntArgument<"Access">]; } +def OpenCLPrivateAddressSpace : TypeAttr { + let Spellings = [Keyword<"__private">, Keyword<"private">]; +} + +def OpenCLGlobalAddressSpace : TypeAttr { + let Spellings = [Keyword<"__global">, Keyword<"global">]; +} + +def OpenCLLocalAddressSpace : TypeAttr { + let Spellings = [Keyword<"__local">, Keyword<"local">]; +} + +def OpenCLConstantAddressSpace : TypeAttr { + let Spellings = [Keyword<"__constant">, Keyword<"constant">]; +} + def Deprecated : InheritableAttr { let Spellings = [GNU<"deprecated">, Declspec<"deprecated">, CXX11<"gnu", "deprecated">, CXX11<"","deprecated">]; diff --git a/include/clang/Parse/Parser.h b/include/clang/Parse/Parser.h index ccb4259cd1..9d0d1b6059 100644 --- a/include/clang/Parse/Parser.h +++ b/include/clang/Parse/Parser.h @@ -2042,7 +2042,7 @@ private: void ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs); void ParseBorlandTypeAttributes(ParsedAttributes &attrs); void ParseOpenCLAttributes(ParsedAttributes &attrs); - void ParseOpenCLQualifiers(DeclSpec &DS); + void ParseOpenCLQualifiers(ParsedAttributes &Attrs); VersionTuple ParseVersionTuple(SourceRange &Range); void ParseAvailabilityAttribute(IdentifierInfo &Availability, diff --git a/include/clang/Sema/AttributeList.h b/include/clang/Sema/AttributeList.h index 7311a8fdfb..8b5c47aaef 100644 --- a/include/clang/Sema/AttributeList.h +++ b/include/clang/Sema/AttributeList.h @@ -648,9 +648,6 @@ public: syntax)); } - AttributeList *createIntegerAttribute(ASTContext &C, IdentifierInfo *Name, - SourceLocation TokLoc, int Arg); - AttributeList *createTypeTagForDatatype( IdentifierInfo *attrName, SourceRange attrRange, IdentifierInfo *scopeName, SourceLocation scopeLoc, @@ -863,15 +860,6 @@ public: return attr; } - AttributeList *addNewInteger(ASTContext &C, IdentifierInfo *name, - SourceLocation loc, int arg) { - AttributeList *attr = - pool.createIntegerAttribute(C, name, loc, arg); - add(attr); - return attr; - } - - private: mutable AttributePool pool; AttributeList *list; diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 7af1d93a6f..0f74e13783 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -651,55 +651,34 @@ void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { } } -void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { - // FIXME: The mapping from attribute spelling to semantics should be - // performed in Sema, not here. - SourceLocation Loc = Tok.getLocation(); - switch(Tok.getKind()) { +void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) { + IdentifierInfo *AttrName = Tok.getIdentifierInfo(); + SourceLocation AttrNameLoc = Tok.getLocation(); + ArgsUnion Expr; + switch (Tok.getKind()) { // OpenCL qualifiers: case tok::kw___private: - DS.getAttributes().addNewInteger( - Actions.getASTContext(), - PP.getIdentifierInfo("address_space"), Loc, 0); - break; - case tok::kw___global: - DS.getAttributes().addNewInteger( - Actions.getASTContext(), - PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); - break; - case tok::kw___local: - DS.getAttributes().addNewInteger( - Actions.getASTContext(), - PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); - break; - case tok::kw___constant: - DS.getAttributes().addNewInteger( - Actions.getASTContext(), - PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); + // These are handled automatically below and have no args. break; - case tok::kw___read_only: - DS.getAttributes().addNewInteger( - Actions.getASTContext(), - PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); + Expr = Actions.ActOnIntegerConstant(SourceLocation(), + CLIA_read_only).take(); break; - case tok::kw___write_only: - DS.getAttributes().addNewInteger( - Actions.getASTContext(), - PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); + Expr = Actions.ActOnIntegerConstant(SourceLocation(), + CLIA_write_only).take(); break; - case tok::kw___read_write: - DS.getAttributes().addNewInteger( - Actions.getASTContext(), - PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); + Expr = Actions.ActOnIntegerConstant(SourceLocation(), + CLIA_read_write).take(); break; - default: break; + default: llvm_unreachable("Unknown OpenCL qualifier"); } + Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &Expr, + Expr.isNull() ? 0 : 1, AttributeList::AS_Keyword); } /// \brief Parse a version number. @@ -3235,7 +3214,7 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, case tok::kw___read_only: case tok::kw___write_only: case tok::kw___read_write: - ParseOpenCLQualifiers(DS); + ParseOpenCLQualifiers(DS.getAttributes()); break; case tok::less: @@ -4468,7 +4447,7 @@ void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, case tok::kw___read_only: case tok::kw___write_only: case tok::kw___read_write: - ParseOpenCLQualifiers(DS); + ParseOpenCLQualifiers(DS.getAttributes()); break; case tok::kw___uptr: diff --git a/lib/Sema/AttributeList.cpp b/lib/Sema/AttributeList.cpp index dbb31b6490..834001c666 100644 --- a/lib/Sema/AttributeList.cpp +++ b/lib/Sema/AttributeList.cpp @@ -106,14 +106,6 @@ void AttributePool::takePool(AttributeList *pool) { } while (pool); } -AttributeList * -AttributePool::createIntegerAttribute(ASTContext &C, IdentifierInfo *Name, - SourceLocation TokLoc, int Arg) { - ArgsUnion IArg = IntegerLiteral::Create(C, llvm::APInt(32, (uint64_t) Arg), - C.IntTy, TokLoc); - return create(Name, TokLoc, 0, TokLoc, &IArg, 1, AttributeList::AS_GNU); -} - #include "clang/Sema/AttrParsedAttrKinds.inc" AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name, diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp index 7423a4d0f1..ef62f43863 100644 --- a/lib/Sema/SemaType.cpp +++ b/lib/Sema/SemaType.cpp @@ -3913,44 +3913,60 @@ static void HandleAddressSpaceTypeAttribute(QualType &Type, return; } - // Check the attribute arguments. - if (Attr.getNumArgs() != 1) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) - << Attr.getName() << 1; - Attr.setInvalid(); - return; - } - Expr *ASArgExpr = static_cast(Attr.getArgAsExpr(0)); - llvm::APSInt addrSpace(32); - if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() || - !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { - S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) - << Attr.getName() << AANT_ArgumentIntegerConstant - << ASArgExpr->getSourceRange(); - Attr.setInvalid(); - return; - } - - // Bounds checking. - if (addrSpace.isSigned()) { - if (addrSpace.isNegative()) { - S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative) + unsigned ASIdx; + if (Attr.getKind() == AttributeList::AT_AddressSpace) { + // Check the attribute arguments. + if (Attr.getNumArgs() != 1) { + S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) + << Attr.getName() << 1; + Attr.setInvalid(); + return; + } + Expr *ASArgExpr = static_cast(Attr.getArgAsExpr(0)); + llvm::APSInt addrSpace(32); + if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() || + !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { + S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) + << Attr.getName() << AANT_ArgumentIntegerConstant << ASArgExpr->getSourceRange(); Attr.setInvalid(); return; } - addrSpace.setIsSigned(false); - } - llvm::APSInt max(addrSpace.getBitWidth()); - max = Qualifiers::MaxAddressSpace; - if (addrSpace > max) { - S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high) - << int(Qualifiers::MaxAddressSpace) << ASArgExpr->getSourceRange(); - Attr.setInvalid(); - return; - } - unsigned ASIdx = static_cast(addrSpace.getZExtValue()); + // Bounds checking. + if (addrSpace.isSigned()) { + if (addrSpace.isNegative()) { + S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative) + << ASArgExpr->getSourceRange(); + Attr.setInvalid(); + return; + } + addrSpace.setIsSigned(false); + } + llvm::APSInt max(addrSpace.getBitWidth()); + max = Qualifiers::MaxAddressSpace; + if (addrSpace > max) { + S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high) + << int(Qualifiers::MaxAddressSpace) << ASArgExpr->getSourceRange(); + Attr.setInvalid(); + return; + } + ASIdx = static_cast(addrSpace.getZExtValue()); + } else { + // The keyword-based type attributes imply which address space to use. + switch (Attr.getKind()) { + case AttributeList::AT_OpenCLGlobalAddressSpace: + ASIdx = LangAS::opencl_global; break; + case AttributeList::AT_OpenCLLocalAddressSpace: + ASIdx = LangAS::opencl_local; break; + case AttributeList::AT_OpenCLConstantAddressSpace: + ASIdx = LangAS::opencl_constant; break; + default: + assert(Attr.getKind() == AttributeList::AT_OpenCLPrivateAddressSpace); + ASIdx = 0; break; + } + } + Type = S.Context.getAddrSpaceQualType(Type, ASIdx); } @@ -4893,6 +4909,10 @@ static void processTypeAttrs(TypeProcessingState &state, QualType &type, // it it breaks large amounts of Linux software. attr.setUsedAsTypeAttr(); break; + case AttributeList::AT_OpenCLPrivateAddressSpace: + case AttributeList::AT_OpenCLGlobalAddressSpace: + case AttributeList::AT_OpenCLLocalAddressSpace: + case AttributeList::AT_OpenCLConstantAddressSpace: case AttributeList::AT_AddressSpace: HandleAddressSpaceTypeAttribute(type, attr, state.getSema()); attr.setUsedAsTypeAttr();