[cfe-commits] [Patch review request] Binary type traits

Douglas Gregor dgregor at apple.com
Mon Sep 20 10:30:05 PDT 2010


On Sep 17, 2010, at 4:00 PM, Steven Watanabe wrote:

> AMDG
> 
> The attached patch implements __is_base_of and __is_convertible_to.
> These are the last intrinsics required to compile the <type_traits> header
> that ships with MSVC 10.0.

Index: tools/clang/lib/Sema/SemaExprCXX.cpp
===================================================================
--- tools/clang/lib/Sema/SemaExprCXX.cpp	(revision 114230)
+++ tools/clang/lib/Sema/SemaExprCXX.cpp	(working copy)
@@ -1996,6 +1996,15 @@
   return false;
 }
 
+static bool CheckTypeTraitArgument(Sema& S, QualType T, SourceLocation KWLoc) {
+  QualType E = T;
+  if (T->isIncompleteArrayType())
+    E = S.getASTContext().getAsArrayType(T)->getElementType();

I know this is just a refactor, but this would be more efficient if implemented as

	if (const IncompleteArrayType *Array = S.getASTContext().getAsIncompleteArrayType(T))
		E = Array->getElementType();


+  return !T->isVoidType() &&
+      S.RequireCompleteType(KWLoc, E,
+                            diag::err_incomplete_type_used_in_type_trait_expr);
+}
+

Index: tools/clang/include/clang/AST/Expr.h
===================================================================
--- tools/clang/include/clang/AST/Expr.h	(revision 114230)
+++ tools/clang/include/clang/AST/Expr.h	(working copy)
@@ -3500,6 +3500,28 @@
   virtual child_iterator child_end();
 };
 
+class DummyExpr : public Expr {
+
+  Classification::Kinds Kind;
+
+public:
+  DummyExpr(QualType T, Classification::Kinds kind)
+    : Expr(DummyExprClass, T, false, false), Kind(kind) {}
+
+  virtual SourceRange getSourceRange() const { return SourceRange(); }
+
+  Classification::Kinds getKind() const { return Kind; }
+
+  static bool classof(const Stmt *T) {
+    return T->getStmtClass() == DummyExprClass;
+  }
+  static bool classof(const DummyExpr *) { return true; }
+
+  // Iterators
+  virtual child_iterator child_begin();
+  virtual child_iterator child_end();
+};
+
 }  // end namespace clang
 
 #endif

I'd really rather not add a new expression type just for the purpose of type-checking __is_convertible_to. How about we synthesize appropriate expressions instead, e.g., a DeclRefExpr to a synthesized variable, or a CallExpr to a synthesized function with an appropriate return type?

Everything else looks great.

	- Doug



More information about the cfe-commits mailing list