18f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall//===-- Internals.h - Implementation Details---------------------*- C++ -*-===//
28f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall//
38f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall//                     The LLVM Compiler Infrastructure
48f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall//
58f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall// This file is distributed under the University of Illinois Open Source
68f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall// License. See LICENSE.TXT for details.
78f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall//
88f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall//===----------------------------------------------------------------------===//
98f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
108f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall#ifndef LLVM_CLANG_LIB_ARCMIGRATE_INTERNALS_H
118f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall#define LLVM_CLANG_LIB_ARCMIGRATE_INTERNALS_H
128f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
138f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall#include "clang/ARCMigrate/ARCMT.h"
148f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall#include "llvm/ADT/ArrayRef.h"
158f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
168f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCallnamespace clang {
178f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  class Sema;
188f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  class Stmt;
198f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
208f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCallnamespace arcmt {
218f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
228f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCallclass CapturedDiagList {
238f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  typedef std::list<StoredDiagnostic> ListTy;
248f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  ListTy List;
258f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
268f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCallpublic:
278f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  void push_back(const StoredDiagnostic &diag) { List.push_back(diag); }
288f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
292d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner  bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
302d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner  bool hasDiagnostic(ArrayRef<unsigned> IDs, SourceRange range) const;
318f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
32d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  void reportDiagnostics(DiagnosticsEngine &diags) const;
33e665d6929e11796620ff799bc0186ebd747bfc76Argyrios Kyrtzidis
34e665d6929e11796620ff799bc0186ebd747bfc76Argyrios Kyrtzidis  bool hasErrors() const;
357ee2049278b98d42709380054eb83f4952af1200Argyrios Kyrtzidis
367ee2049278b98d42709380054eb83f4952af1200Argyrios Kyrtzidis  typedef ListTy::const_iterator iterator;
377ee2049278b98d42709380054eb83f4952af1200Argyrios Kyrtzidis  iterator begin() const { return List.begin(); }
387ee2049278b98d42709380054eb83f4952af1200Argyrios Kyrtzidis  iterator end()   const { return List.end();   }
398f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall};
408f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
417ee2049278b98d42709380054eb83f4952af1200Argyrios Kyrtzidisvoid writeARCDiagsToPlist(const std::string &outPath,
422d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner                          ArrayRef<StoredDiagnostic> diags,
437ee2049278b98d42709380054eb83f4952af1200Argyrios Kyrtzidis                          SourceManager &SM, const LangOptions &LangOpts);
447ee2049278b98d42709380054eb83f4952af1200Argyrios Kyrtzidis
458f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCallclass TransformActions {
46d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &Diags;
478f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  CapturedDiagList &CapturedDiags;
48fd10398c10ffdcbdeb1e3e299c74d70e689f503cArgyrios Kyrtzidis  bool ReportedErrors;
498f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  void *Impl; // TransformActionsImpl.
508f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
518f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCallpublic:
52d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  TransformActions(DiagnosticsEngine &diag, CapturedDiagList &capturedDiags,
538f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall                   ASTContext &ctx, Preprocessor &PP);
548f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  ~TransformActions();
558f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
568f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  void startTransaction();
578f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  bool commitTransaction();
588f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  void abortTransaction();
598f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
60686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void insert(SourceLocation loc, StringRef text);
61686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void insertAfterToken(SourceLocation loc, StringRef text);
628f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  void remove(SourceRange range);
638f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  void removeStmt(Stmt *S);
64686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void replace(SourceRange range, StringRef text);
658f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  void replace(SourceRange range, SourceRange replacementRange);
66686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void replaceStmt(Stmt *S, StringRef text);
67686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void replaceText(SourceLocation loc, StringRef text,
68686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                   StringRef replacementText);
698f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  void increaseIndentation(SourceRange range,
708f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall                           SourceLocation parentIndent);
718f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
722d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner  bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
738f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  bool clearAllDiagnostics(SourceRange range) {
742d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner    return clearDiagnostic(ArrayRef<unsigned>(), range);
758f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  }
768f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  bool clearDiagnostic(unsigned ID1, unsigned ID2, SourceRange range) {
778f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    unsigned IDs[] = { ID1, ID2 };
788f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    return clearDiagnostic(IDs, range);
798f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  }
808f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  bool clearDiagnostic(unsigned ID1, unsigned ID2, unsigned ID3,
818f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall                       SourceRange range) {
828f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    unsigned IDs[] = { ID1, ID2, ID3 };
838f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    return clearDiagnostic(IDs, range);
848f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  }
858f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
868f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  bool hasDiagnostic(unsigned ID, SourceRange range) {
878f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    return CapturedDiags.hasDiagnostic(ID, range);
888f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  }
898f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
908f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  bool hasDiagnostic(unsigned ID1, unsigned ID2, SourceRange range) {
918f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    unsigned IDs[] = { ID1, ID2 };
928f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    return CapturedDiags.hasDiagnostic(IDs, range);
938f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  }
948f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
95686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void reportError(StringRef error, SourceLocation loc,
968f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall                   SourceRange range = SourceRange());
97b5c6babd3d8e0233b8ea5a4eb1e2700e30c0d396Fariborz Jahanian  void reportWarning(StringRef warning, SourceLocation loc,
98b5c6babd3d8e0233b8ea5a4eb1e2700e30c0d396Fariborz Jahanian                   SourceRange range = SourceRange());
99686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void reportNote(StringRef note, SourceLocation loc,
1008f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall                  SourceRange range = SourceRange());
1018f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
102fd10398c10ffdcbdeb1e3e299c74d70e689f503cArgyrios Kyrtzidis  bool hasReportedErrors() const { return ReportedErrors; }
103fd10398c10ffdcbdeb1e3e299c74d70e689f503cArgyrios Kyrtzidis
1048f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  class RewriteReceiver {
1058f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  public:
1068f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    virtual ~RewriteReceiver();
1078f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
108686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    virtual void insert(SourceLocation loc, StringRef text) = 0;
1098f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    virtual void remove(CharSourceRange range) = 0;
1108f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    virtual void increaseIndentation(CharSourceRange range,
1118f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall                                     SourceLocation parentIndent) = 0;
1128f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  };
1138f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
1148f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  void applyRewrites(RewriteReceiver &receiver);
1158f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall};
1168f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
1178f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCallclass Transaction {
1188f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  TransformActions &TA;
1198f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  bool Aborted;
1208f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
1218f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCallpublic:
1228f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  Transaction(TransformActions &TA) : TA(TA), Aborted(false) {
1238f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    TA.startTransaction();
1248f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  }
1258f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
1268f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  ~Transaction() {
1278f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    if (!isAborted())
1288f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall      TA.commitTransaction();
1298f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  }
1308f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
1318f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  void abort() {
1328f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    TA.abortTransaction();
1338f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall    Aborted = true;
1348f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  }
1358f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
1368f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  bool isAborted() const { return Aborted; }
1378f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall};
1388f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
1398f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCallclass MigrationPass {
1408f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCallpublic:
1418f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  ASTContext &Ctx;
142e0ac7454bae910ab3d67a92f6e2e5046d3bb8c1aArgyrios Kyrtzidis  LangOptions::GCMode OrigGCMode;
143b5c6babd3d8e0233b8ea5a4eb1e2700e30c0d396Fariborz Jahanian  MigratorOptions MigOptions;
1448f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  Sema &SemaRef;
1458f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  TransformActions &TA;
1468f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  std::vector<SourceLocation> &ARCMTMacroLocs;
1478f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
148e0ac7454bae910ab3d67a92f6e2e5046d3bb8c1aArgyrios Kyrtzidis  MigrationPass(ASTContext &Ctx, LangOptions::GCMode OrigGCMode,
149e0ac7454bae910ab3d67a92f6e2e5046d3bb8c1aArgyrios Kyrtzidis                Sema &sema, TransformActions &TA,
1508f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall                std::vector<SourceLocation> &ARCMTMacroLocs)
151b5c6babd3d8e0233b8ea5a4eb1e2700e30c0d396Fariborz Jahanian    : Ctx(Ctx), OrigGCMode(OrigGCMode), MigOptions(),
152b5c6babd3d8e0233b8ea5a4eb1e2700e30c0d396Fariborz Jahanian      SemaRef(sema), TA(TA),
153e0ac7454bae910ab3d67a92f6e2e5046d3bb8c1aArgyrios Kyrtzidis      ARCMTMacroLocs(ARCMTMacroLocs) { }
154e0ac7454bae910ab3d67a92f6e2e5046d3bb8c1aArgyrios Kyrtzidis
155e0ac7454bae910ab3d67a92f6e2e5046d3bb8c1aArgyrios Kyrtzidis  bool isGCMigration() const { return OrigGCMode != LangOptions::NonGC; }
156b5c6babd3d8e0233b8ea5a4eb1e2700e30c0d396Fariborz Jahanian  bool noNSAllocReallocError() const { return MigOptions.NoNSAllocReallocError; }
157b5c6babd3d8e0233b8ea5a4eb1e2700e30c0d396Fariborz Jahanian  void setNSAllocReallocError(bool val) { MigOptions.NoNSAllocReallocError = val; }
15826f0e4e7ab534fb42485c930f20a424ecc8c9830Fariborz Jahanian  bool noFinalizeRemoval() const { return MigOptions.NoFinalizeRemoval; }
15926f0e4e7ab534fb42485c930f20a424ecc8c9830Fariborz Jahanian  void setNoFinalizeRemoval(bool val) {MigOptions.NoFinalizeRemoval = val; }
1608f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall};
1618f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
162686775deca8b8685eb90801495880e3abdd844c2Chris Lattnerstatic inline StringRef getARCMTMacroName() {
1638f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall  return "__IMPL_ARCMT_REMOVED_EXPR__";
1648f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall}
1658f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
1668f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall} // end namespace arcmt
1678f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
1688f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall} // end namespace clang
1698f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall
1708f0e8d22960d56f8390f4971e2c0f2f0a0884602John McCall#endif
171