Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 1 | //===--- 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 | // |
Mikhail Glushenkov | 113ec35 | 2008-11-25 21:38:12 +0000 | [diff] [blame^] | 10 | // Plugin support. |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mikhail Glushenkov | 4a1a77c | 2008-09-22 20:50:40 +0000 | [diff] [blame] | 14 | #include "llvm/CompilerDriver/Plugin.h" |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 15 | |
Mikhail Glushenkov | 35fde15 | 2008-11-17 17:30:25 +0000 | [diff] [blame] | 16 | #include <algorithm> |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
| 19 | namespace { |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 20 | |
| 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 Glushenkov | 35fde15 | 2008-11-17 17:30:25 +0000 | [diff] [blame] | 31 | |
| 32 | struct ByPriority { |
| 33 | bool operator()(const llvmc::BasePlugin* lhs, |
| 34 | const llvmc::BasePlugin* rhs) { |
| 35 | return lhs->Priority() < rhs->Priority(); |
| 36 | } |
| 37 | }; |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | namespace llvmc { |
| 41 | |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 42 | 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 Glushenkov | 35fde15 | 2008-11-17 17:30:25 +0000 | [diff] [blame] | 47 | std::sort(Plugins.begin(), Plugins.end(), ByPriority()); |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 48 | } |
| 49 | pluginListInitialized = true; |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 52 | 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 Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 64 | (*B)->PopulateLanguageMap(langMap); |
| 65 | } |
| 66 | |
Mikhail Glushenkov | 14ef059 | 2008-09-22 20:51:19 +0000 | [diff] [blame] | 67 | void PluginLoader::PopulateCompilationGraph(CompilationGraph& graph) { |
| 68 | for (PluginList::iterator B = Plugins.begin(), E = Plugins.end(); |
| 69 | B != E; ++B) |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame] | 70 | (*B)->PopulateCompilationGraph(graph); |
| 71 | } |
| 72 | |
| 73 | } |