]> granicus.if.org Git - clang/commitdiff
[CONCEPTS] Parsing of concept keyword
authorHubert Tong <hubert.reinterpretcast@gmail.com>
Tue, 30 Jun 2015 12:14:52 +0000 (12:14 +0000)
committerHubert Tong <hubert.reinterpretcast@gmail.com>
Tue, 30 Jun 2015 12:14:52 +0000 (12:14 +0000)
Summary: This change adds parsing for the concept keyword in a
declaration and tracks the location. Diagnostic testing added for
invalid use of concept keyword.

Reviewers: faisalv, fraggamuffin, rsmith, hubert.reinterpretcast

Reviewed By: rsmith, hubert.reinterpretcast

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D10528

Patch by Nathan Wilson!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@241060 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Sema/DeclSpec.h
lib/Parse/ParseDecl.cpp
lib/Parse/ParseTentative.cpp
lib/Sema/DeclSpec.cpp
test/Parser/cxx-concept-declaration.cpp [new file with mode: 0644]

index 2ec3286ad799ccff9741492a7ccb685443a2c3ff..d375ec303785d3a4b651d88d969b79696415498d 100644 (file)
@@ -358,6 +358,9 @@ private:
   // constexpr-specifier
   unsigned Constexpr_specified : 1;
 
+  // concept-specifier
+  unsigned Concept_specified : 1;
+
   union {
     UnionParsedType TypeRep;
     Decl *DeclRep;
@@ -393,7 +396,7 @@ private:
   SourceLocation TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc;
   SourceLocation FS_inlineLoc, FS_virtualLoc, FS_explicitLoc, FS_noreturnLoc;
   SourceLocation FS_forceinlineLoc;
-  SourceLocation FriendLoc, ModulePrivateLoc, ConstexprLoc;
+  SourceLocation FriendLoc, ModulePrivateLoc, ConstexprLoc, ConceptLoc;
 
   WrittenBuiltinSpecs writtenBS;
   void SaveWrittenBuiltinSpecs();
@@ -437,6 +440,7 @@ public:
       FS_noreturn_specified(false),
       Friend_specified(false),
       Constexpr_specified(false),
+      Concept_specified(false),
       Attrs(attrFactory),
       ProtocolQualifiers(nullptr),
       NumProtocolQualifiers(0),
@@ -688,6 +692,8 @@ public:
                             unsigned &DiagID);
   bool SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
                         unsigned &DiagID);
+  bool SetConceptSpec(SourceLocation Loc, const char *&PrevSpec,
+                      unsigned &DiagID);
 
   bool isFriendSpecified() const { return Friend_specified; }
   SourceLocation getFriendSpecLoc() const { return FriendLoc; }
@@ -698,11 +704,19 @@ public:
   bool isConstexprSpecified() const { return Constexpr_specified; }
   SourceLocation getConstexprSpecLoc() const { return ConstexprLoc; }
 
+  bool isConceptSpecified() const { return Concept_specified; }
+  SourceLocation getConceptSpecLoc() const { return ConceptLoc; }
+
   void ClearConstexprSpec() {
     Constexpr_specified = false;
     ConstexprLoc = SourceLocation();
   }
 
+  void ClearConceptSpec() {
+    Concept_specified = false;
+    ConceptLoc = SourceLocation();
+  }
+
   AttributePool &getAttributePool() const {
     return Attrs.getPool();
   }
index f497c59cec1a5964cec1a9d2c0098be490137260..d843e801b6342f43e3682522f289e370e03a46b8 100644 (file)
@@ -3192,6 +3192,11 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
       isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
       break;
 
+    // concept
+    case tok::kw_concept:
+      isInvalid = DS.SetConceptSpec(Loc, PrevSpec, DiagID);
+      break;
+
     // type-specifier
     case tok::kw_short:
       isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
@@ -4475,6 +4480,9 @@ bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
   case tok::annot_decltype:
   case tok::kw_constexpr:
 
+    // C++ Concepts TS - concept
+  case tok::kw_concept:
+
     // C11 _Atomic
   case tok::kw__Atomic:
     return true;
index 536e98c68312dbc88fb7b8476acc3ae103db8720..368cb934bbaffd538d84b749a0e5e3411c19067a 100644 (file)
@@ -1213,9 +1213,11 @@ Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
     //   'friend'
     //   'typedef'
     //   'constexpr'
+    //   'concept'
   case tok::kw_friend:
   case tok::kw_typedef:
   case tok::kw_constexpr:
+  case tok::kw_concept:
     // storage-class-specifier
   case tok::kw_register:
   case tok::kw_static:
index 1e7fc750a9cf2bad803c5c4f72929a4b1a168272..d5c8871cac87b4c97acdb321654bf2983bab4df6 100644 (file)
@@ -893,6 +893,18 @@ bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
   return false;
 }
 
+bool DeclSpec::SetConceptSpec(SourceLocation Loc, const char *&PrevSpec,
+                              unsigned &DiagID) {
+  if (Concept_specified) {
+    DiagID = diag::ext_duplicate_declspec;
+    PrevSpec = "concept";
+    return true;
+  }
+  Concept_specified = true;
+  ConceptLoc = Loc;
+  return false;
+}
+
 void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
                                      unsigned NP,
                                      SourceLocation *ProtoLocs,
diff --git a/test/Parser/cxx-concept-declaration.cpp b/test/Parser/cxx-concept-declaration.cpp
new file mode 100644 (file)
index 0000000..591629c
--- /dev/null
@@ -0,0 +1,30 @@
+
+// Support parsing of function concepts and variable concepts
+
+// RUN:  %clang_cc1 -std=c++14 -fconcepts-ts -x c++ -verify %s
+
+template<typename T> concept bool C1 = true;
+
+template<typename T> concept bool C2() { return true; }
+
+template<typename T>
+struct A { typedef bool Boolean; };
+
+template<int N>
+A<void>::Boolean concept C3(!0);
+
+template<typename T, int = 0>
+concept auto C4(void) -> bool { return true; }
+
+constexpr int One = 1;
+
+template <typename>
+static concept decltype(!0) C5 { bool(One) };
+
+template<typename T> concept concept bool C6 = true; // expected-warning {{duplicate 'concept' declaration specifier}}
+
+template<typename T> concept concept bool C7() { return true; } // expected-warning {{duplicate 'concept' declaration specifier}}
+
+concept D1 = true; // expected-error {{C++ requires a type specifier for all declarations}}
+
+template<concept T> concept bool D2 = true; // expected-error {{unknown type name 'T'}}