Files
swift-mirror/lib/SILAnalysis/ClassHierarchyAnalysis.cpp

58 lines
1.9 KiB
C++

//===- ClassHierarchyAnalysis.cpp - Analysis of class hierarchy -*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/SILAnalysis/ClassHierarchyAnalysis.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Module.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILValue.h"
#include "swift/SIL/SILModule.h"
using namespace swift;
void ClassHierarchyAnalysis::init() {
// For each class declaration in our V-table list:
for (auto &VT : M->getVTableList()) {
ClassDecl *C = VT.getClass();
// Ignore classes that are at the top of the class hierarchy:
if (!C->hasSuperclass())
continue;
// Add the superclass to the list of inherited classes.
ClassDecl *Super = C->getSuperclass()->getClassOrBoundGenericClass();
InheritedClasses.insert(Super);
}
}
void ClassHierarchyAnalysis::collectSubClasses(ClassDecl *C,
std::vector<ClassDecl*> &Sub) {
// TODO: Cache this search.
assert(Sub.empty() && "Incoming list is not empty");
SmallVector<ClassDecl*, 4> Path;
for (auto &VT : M->getVTableList()) {
ClassDecl *CD = VT.getClass();
while (CD->hasSuperclass()) {
Path.push_back(CD);
// Add the superclass to the list of inherited classes.
ClassDecl *Super = CD->getSuperclass()->getClassOrBoundGenericClass();
if (Super == C) {
Sub.insert(Sub.end(), Path.begin(), Path.end());
break;
}
CD = Super;
}
Path.clear();
}
}
ClassHierarchyAnalysis::~ClassHierarchyAnalysis() {}