Implement support for the const and pure attributes.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@57142 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/Attr.h b/include/clang/AST/Attr.h
index 3115a45..c6fb1d1 100644
--- a/include/clang/AST/Attr.h
+++ b/include/clang/AST/Attr.h
@@ -47,7 +47,9 @@
     Unused,    
     Visibility,
     Weak,
-    Blocks
+    Blocks,
+    Const,
+    Pure
   };
     
 private:
@@ -228,7 +230,27 @@
   static bool classof(const Attr *A) { return A->getKind() == NoThrow; }
   static bool classof(const NoThrowAttr *A) { return true; }
 };
-  
+
+class ConstAttr : public Attr {
+public:
+  ConstAttr() : Attr(Const) {}
+
+  // Implement isa/cast/dyncast/etc.
+
+  static bool classof(const Attr *A) { return A->getKind() == Const; }
+  static bool classof(const ConstAttr *A) { return true; }
+};
+
+class PureAttr : public Attr {
+public:
+  PureAttr() : Attr(Pure) {}
+
+  // Implement isa/cast/dyncast/etc.
+
+  static bool classof(const Attr *A) { return A->getKind() == Pure; }
+  static bool classof(const PureAttr *A) { return true; }
+};
+
 class NonNullAttr : public Attr {
   unsigned* ArgNums;
   unsigned Size;
diff --git a/include/clang/Parse/AttributeList.h b/include/clang/Parse/AttributeList.h
index 8ca78a3..d41faa3 100644
--- a/include/clang/Parse/AttributeList.h
+++ b/include/clang/Parse/AttributeList.h
@@ -73,6 +73,7 @@
     AT_objc_gc,
     AT_blocks,
     AT_sentinel,
+    AT_const,
     UnknownAttribute
   };
   
diff --git a/lib/CodeGen/CGCall.cpp b/lib/CodeGen/CGCall.cpp
index a1c3c33..a246ade 100644
--- a/lib/CodeGen/CGCall.cpp
+++ b/lib/CodeGen/CGCall.cpp
@@ -520,6 +520,10 @@
       FuncAttrs |= llvm::Attribute::NoUnwind;
     if (TargetDecl->getAttr<NoReturnAttr>())
       FuncAttrs |= llvm::Attribute::NoReturn;
+    if (TargetDecl->getAttr<PureAttr>())
+      FuncAttrs |= llvm::Attribute::ReadOnly;
+    if (TargetDecl->getAttr<ConstAttr>())
+      FuncAttrs |= llvm::Attribute::ReadNone;
   }
 
   QualType RetTy = *begin;
diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp
index fd79a10..3ab0c91 100644
--- a/lib/Sema/SemaDeclAttr.cpp
+++ b/lib/Sema/SemaDeclAttr.cpp
@@ -717,6 +717,28 @@
   d->addAttr(new NoThrowAttr());
 }
 
+static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
+  // check the attribute arguments.
+  if (Attr.getNumArgs() != 0) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
+           std::string("0"));
+    return;
+  }
+  
+  d->addAttr(new ConstAttr());
+}
+
+static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
+  // check the attribute arguments.
+  if (Attr.getNumArgs() != 0) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
+           std::string("0"));
+    return;
+  }
+  
+  d->addAttr(new PureAttr());
+}
+
 /// Handle __attribute__((format(type,idx,firstarg))) attributes
 /// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
 static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
@@ -1118,6 +1140,8 @@
   case AttributeList::AT_objc_gc:     HandleObjCGCAttr    (D, Attr, S); break;
   case AttributeList::AT_blocks:      HandleBlocksAttr    (D, Attr, S); break;
   case AttributeList::AT_sentinel:    HandleSentinelAttr  (D, Attr, S); break;
+  case AttributeList::AT_const:       HandleConstAttr     (D, Attr, S); break;
+  case AttributeList::AT_pure:        HandlePureAttr      (D, Attr, S); break;
   default:
 #if 0
     // TODO: when we have the full set of attributes, warn about unknown ones.