blob: 17c70869ea6ad947353c2a473f769e708b70423c [file] [log] [blame]
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00001//===--- Plugin.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Plugin support for llvmc2.
11//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +000014#include "llvm/CompilerDriver/Plugin.h"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000015
Mikhail Glushenkov35fde152008-11-17 17:30:25 +000016#include <algorithm>
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000017#include <vector>
18
19namespace {
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000020
21 // Registry::Add<> does not do lifetime management (probably issues
22 // with static constructor/destructor ordering), so we have to
23 // implement it here.
24 //
25 // All this static registration/life-before-main model seems
26 // unnecessary convoluted to me.
27
28 static bool pluginListInitialized = false;
29 typedef std::vector<const llvmc::BasePlugin*> PluginList;
30 static PluginList Plugins;
Mikhail Glushenkov35fde152008-11-17 17:30:25 +000031
32 struct ByPriority {
33 bool operator()(const llvmc::BasePlugin* lhs,
34 const llvmc::BasePlugin* rhs) {
35 return lhs->Priority() < rhs->Priority();
36 }
37 };
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000038}
39
40namespace llvmc {
41
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000042 PluginLoader::PluginLoader() {
43 if (!pluginListInitialized) {
44 for (PluginRegistry::iterator B = PluginRegistry::begin(),
45 E = PluginRegistry::end(); B != E; ++B)
46 Plugins.push_back(B->instantiate());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +000047 std::sort(Plugins.begin(), Plugins.end(), ByPriority());
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000048 }
49 pluginListInitialized = true;
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000050 }
51
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000052 PluginLoader::~PluginLoader() {
53 if (pluginListInitialized) {
54 for (PluginList::iterator B = Plugins.begin(), E = Plugins.end();
55 B != E; ++B)
56 delete (*B);
57 }
58 pluginListInitialized = false;
59 }
60
61 void PluginLoader::PopulateLanguageMap(LanguageMap& langMap) {
62 for (PluginList::iterator B = Plugins.begin(), E = Plugins.end();
63 B != E; ++B)
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000064 (*B)->PopulateLanguageMap(langMap);
65 }
66
Mikhail Glushenkov14ef0592008-09-22 20:51:19 +000067 void PluginLoader::PopulateCompilationGraph(CompilationGraph& graph) {
68 for (PluginList::iterator B = Plugins.begin(), E = Plugins.end();
69 B != E; ++B)
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +000070 (*B)->PopulateCompilationGraph(graph);
71 }
72
73}