[cfe-commits] r54852 - in /cfe/trunk/lib/Sema: Sema.h SemaExpr.cpp

Argiris Kirtzidis akyrtzi at gmail.com
Sat Aug 16 13:27:36 PDT 2008


Author: akirtzidis
Date: Sat Aug 16 15:27:34 2008
New Revision: 54852

URL: http://llvm.org/viewvc/llvm-project?rev=54852&view=rev
Log:
Move the type checking that Sema::ActOnCastExpr does into a new Sema::CheckCastTypes function so that it can be reused.

Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=54852&r1=54851&r2=54852&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Sat Aug 16 15:27:34 2008
@@ -869,6 +869,9 @@
   
   StringLiteral *IsStringLiteralInit(Expr *Init, QualType DeclType);
   bool CheckStringLiteralInit(StringLiteral *strLiteral, QualType &DeclT);
+
+  /// CheckCastTypes - Check type constraints for casting between types.
+  bool CheckCastTypes(SourceRange TyRange, QualType CastTy, Expr *CastExpr);
   
   // CheckVectorCast - check type constraints for vectors. 
   // Since vectors are an extension, there are no C standard reference for this.

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=54852&r1=54851&r2=54852&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sat Aug 16 15:27:34 2008
@@ -1088,6 +1088,42 @@
   return E;
 }
 
+/// CheckCastTypes - Check type constraints for casting between types.
+bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *castExpr) {
+  UsualUnaryConversions(castExpr);
+
+  // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
+  // type needs to be scalar.
+  if (castType->isVoidType()) {
+    // Cast to void allows any expr type.
+  } else if (!castType->isScalarType() && !castType->isVectorType()) {
+    // GCC struct/union extension: allow cast to self.
+    if (Context.getCanonicalType(castType) !=
+        Context.getCanonicalType(castExpr->getType()) ||
+        (!castType->isStructureType() && !castType->isUnionType())) {
+      // Reject any other conversions to non-scalar types.
+      return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar, 
+                  castType.getAsString(), castExpr->getSourceRange());
+    }
+      
+    // accept this, but emit an ext-warn.
+    Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar, 
+         castType.getAsString(), castExpr->getSourceRange());
+  } else if (!castExpr->getType()->isScalarType() && 
+             !castExpr->getType()->isVectorType()) {
+    return Diag(castExpr->getLocStart(), 
+                diag::err_typecheck_expect_scalar_operand, 
+                castExpr->getType().getAsString(),castExpr->getSourceRange());
+  } else if (castExpr->getType()->isVectorType()) {
+    if (CheckVectorCast(TyR, castExpr->getType(), castType))
+      return true;
+  } else if (castType->isVectorType()) {
+    if (CheckVectorCast(TyR, castType, castExpr->getType()))
+      return true;
+  }
+  return false;
+}
+
 bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
   assert(VectorTy->isVectorType() && "Not a vector type!");
   
@@ -1116,39 +1152,8 @@
   Expr *castExpr = static_cast<Expr*>(Op);
   QualType castType = QualType::getFromOpaquePtr(Ty);
 
-  UsualUnaryConversions(castExpr);
-
-  // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
-  // type needs to be scalar.
-  if (castType->isVoidType()) {
-    // Cast to void allows any expr type.
-  } else if (!castType->isScalarType() && !castType->isVectorType()) {
-    // GCC struct/union extension: allow cast to self.
-    if (Context.getCanonicalType(castType) !=
-        Context.getCanonicalType(castExpr->getType()) ||
-        (!castType->isStructureType() && !castType->isUnionType())) {
-      // Reject any other conversions to non-scalar types.
-      return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar, 
-                  castType.getAsString(), castExpr->getSourceRange());
-    }
-      
-    // accept this, but emit an ext-warn.
-    Diag(LParenLoc, diag::ext_typecheck_cast_nonscalar, 
-         castType.getAsString(), castExpr->getSourceRange());
-  } else if (!castExpr->getType()->isScalarType() && 
-             !castExpr->getType()->isVectorType()) {
-    return Diag(castExpr->getLocStart(), 
-                diag::err_typecheck_expect_scalar_operand, 
-                castExpr->getType().getAsString(),castExpr->getSourceRange());
-  } else if (castExpr->getType()->isVectorType()) {
-    if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc), 
-                        castExpr->getType(), castType))
-      return true;
-  } else if (castType->isVectorType()) {
-    if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc), 
-                        castType, castExpr->getType()))
-      return true;
-  }
+  if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr))
+    return true;
   return new CastExpr(castType, castExpr, LParenLoc);
 }
 





More information about the cfe-commits mailing list