Skip to content
Sema.cpp 8.96 KiB
Newer Older
//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the actions class which performs semantic analysis and
// builds an AST out of a parse stream.
//
//===----------------------------------------------------------------------===//

#include "clang/AST/ASTContext.h"
#include "clang/Basic/Diagnostic.h"
Steve Naroff's avatar
 
Steve Naroff committed
#include "clang/Parse/Scope.h"
using namespace clang;

bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
Steve Naroff's avatar
 
Steve Naroff committed
  const char *typeName = TD->getIdentifier()->getName();
  return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
         strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
Steve Naroff's avatar
 
Steve Naroff committed
}

bool Sema::isObjCObjectPointerType(QualType type) const {
  if (!type->isPointerType() && !type->isObjCQualifiedIdType())
  if (type == Context.getObjCIdType() || type == Context.getObjCClassType() ||
      type->isObjCQualifiedIdType())
    PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr());
    type = pointerType->getPointeeType();
  }
  return (type->isObjCInterfaceType() || type->isObjCQualifiedIdType());
Steve Naroff's avatar
 
Steve Naroff committed
void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
  TUScope = S;
  CurContext = Context.getTranslationUnitDecl();
  if (!PP.getLangOptions().ObjC1) return;
  
  TypedefType *t;
  
  // Add the built-in ObjC types.
  t = cast<TypedefType>(Context.getObjCIdType().getTypePtr());
  t = cast<TypedefType>(Context.getObjCClassType().getTypePtr());
  ObjCInterfaceType *it = cast<ObjCInterfaceType>(Context.getObjCProtoType());
  ObjCInterfaceDecl *IDecl = it->getDecl();
  
  // Synthesize "typedef struct objc_selector *SEL;"
Argyrios Kyrtzidis's avatar
Argyrios Kyrtzidis committed
  RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
                                          &Context.Idents.get("objc_selector"),
  
  QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
  TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
                                                SourceLocation(),
  Context.setObjCSelType(SelTypedef);
Steve Naroff's avatar
 
Steve Naroff committed

Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
  : PP(pp), Context(ctxt), Consumer(consumer), 
    CurFunctionDecl(0), CurMethodDecl(0), CurContext(0) {
  
  // Get IdentifierInfo objects for known functions for which we
  // do extra checking.  
  IdentifierTable &IT = PP.getIdentifierTable();  
  KnownFunctionIDs[id_printf]    = &IT.get("printf");
  KnownFunctionIDs[id_fprintf]   = &IT.get("fprintf");
  KnownFunctionIDs[id_sprintf]   = &IT.get("sprintf");
  KnownFunctionIDs[id_snprintf]  = &IT.get("snprintf");
  KnownFunctionIDs[id_asprintf]  = &IT.get("asprintf");
  KnownFunctionIDs[id_NSLog]     = &IT.get("NSLog");
  KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
  KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
  KnownFunctionIDs[id_vfprintf]  = &IT.get("vfprintf");
  KnownFunctionIDs[id_vsprintf]  = &IT.get("vsprintf");
  KnownFunctionIDs[id_vprintf]   = &IT.get("vprintf");
Steve Naroff's avatar
 
Steve Naroff committed

  // FIXME: Move this initialization up to Sema::ActOnTranslationUnitScope()
  // and make sure the decls get inserted into TUScope!
Eli Friedman's avatar
Eli Friedman committed
  // FIXME: And make sure they don't leak!
Steve Naroff's avatar
 
Steve Naroff committed
  if (PP.getLangOptions().ObjC1) {
    TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl();

Steve Naroff's avatar
 
Steve Naroff committed
    // Synthesize "typedef struct objc_class *Class;"
Argyrios Kyrtzidis's avatar
Argyrios Kyrtzidis committed
    RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
Steve Naroff's avatar
 
Steve Naroff committed
    QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
      TypedefDecl::Create(Context, TUDecl, SourceLocation(),
                          &Context.Idents.get("Class"), ClassT, 0);
    Context.setObjCClassType(ClassTypedef);
Steve Naroff's avatar
 
Steve Naroff committed
    
    // Synthesize "@class Protocol;
    ObjCInterfaceDecl *ProtocolDecl =
      ObjCInterfaceDecl::Create(Context, SourceLocation(), 0, 
                                &Context.Idents.get("Protocol"), 
                                SourceLocation(), true);
    Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Steve Naroff's avatar
 
Steve Naroff committed
    // Synthesize "typedef struct objc_object { Class isa; } *id;"
Argyrios Kyrtzidis's avatar
Argyrios Kyrtzidis committed
      RecordDecl::Create(Context, TagDecl::TK_struct, TUDecl,
    FieldDecl *IsaDecl = FieldDecl::Create(Context, SourceLocation(),
                                           &Context.Idents.get("isa"),
                                           Context.getObjCClassType());
Steve Naroff's avatar
 
Steve Naroff committed
    ObjectTag->defineBody(&IsaDecl, 1);
    QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
    TypedefDecl *IdTypedef = TypedefDecl::Create(Context, TUDecl,
    Context.setObjCIdType(IdTypedef);
Steve Naroff's avatar
 
Steve Naroff committed
  }
Steve Naroff's avatar
 
Steve Naroff committed
  TUScope = 0;
/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. 
/// If there is already an implicit cast, merge into the existing one.
void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
  if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
  
  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
    ImpCast->setType(Type);
  else 
    Expr = new ImplicitCastExpr(Type, Expr);
}



void Sema::DeleteExpr(ExprTy *E) {
  delete static_cast<Expr*>(E);
}
void Sema::DeleteStmt(StmtTy *S) {
  delete static_cast<Stmt*>(S);
}

//===----------------------------------------------------------------------===//
// Helper functions.
//===----------------------------------------------------------------------===//

bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Steve Naroff's avatar
Steve Naroff committed
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  &Msg, 1);
  return true;
}

bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
                const std::string &Msg2) {
  std::string MsgArr[] = { Msg1, Msg2 };
  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  MsgArr, 2);
Steve Naroff's avatar
Steve Naroff committed
  return true;
Steve Naroff's avatar
Steve Naroff committed
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Steve Naroff's avatar
Steve Naroff committed
  return true;
}

Steve Naroff's avatar
Steve Naroff committed
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
                SourceRange Range) {
  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Steve Naroff's avatar
Steve Naroff committed
  return true;
}

bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
                const std::string &Msg2, SourceRange Range) {
  std::string MsgArr[] = { Msg1, Msg2 };
  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Steve Naroff's avatar
Steve Naroff committed
  return true;
}

bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, 
                const std::string &Msg2, const std::string &Msg3, 
                SourceRange R1) {
  std::string MsgArr[] = { Msg1, Msg2, Msg3 };
  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
  return true;
}

Steve Naroff's avatar
Steve Naroff committed
bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
                SourceRange R1, SourceRange R2) {
  SourceRange RangeArr[] = { R1, R2 };
  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Steve Naroff's avatar
Steve Naroff committed
  return true;
}

bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
                SourceRange R1, SourceRange R2) {
  SourceRange RangeArr[] = { R1, R2 };
  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  &Msg, 1, RangeArr, 2);
Steve Naroff's avatar
Steve Naroff committed
  return true;
}

bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
                const std::string &Msg2, SourceRange R1, SourceRange R2) {
  std::string MsgArr[] = { Msg1, Msg2 };
  SourceRange RangeArr[] = { R1, R2 };
  PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Steve Naroff's avatar
Steve Naroff committed
  return true;
Steve Naroff's avatar
Steve Naroff committed
}

const LangOptions &Sema::getLangOptions() const {