Implement global variable support
[oota-llvm.git] / lib / VMCore / Module.cpp
1 //===-- Module.cpp - Implement the Module class ------------------*- C++ -*--=//
2 //
3 // This file implements the Module class for the VMCore library.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Module.h"
8 #include "llvm/Method.h"
9 #include "llvm/GlobalVariable.h"
10 #include "llvm/BasicBlock.h"
11 #include "llvm/InstrTypes.h"
12 #include "llvm/ValueHolderImpl.h"
13 #include "llvm/Support/STLExtras.h"
14 #include "llvm/Type.h"
15
16 // Instantiate Templates - This ugliness is the price we have to pay
17 // for having a DefHolderImpl.h file seperate from DefHolder.h!  :(
18 //
19 template class ValueHolder<GlobalVariable, Module, Module>;
20 template class ValueHolder<Method, Module, Module>;
21
22 Module::Module()
23   : Value(Type::VoidTy, Value::ModuleVal, ""), SymTabValue(this),
24     GlobalList(this, this), MethodList(this, this) {
25 }
26
27 Module::~Module() {
28   dropAllReferences();
29   GlobalList.delete_all();
30   GlobalList.setParent(0);
31   MethodList.delete_all();
32   MethodList.setParent(0);
33 }
34
35
36 // dropAllReferences() - This function causes all the subinstructions to "let
37 // go" of all references that they are maintaining.  This allows one to
38 // 'delete' a whole class at a time, even though there may be circular
39 // references... first all references are dropped, and all use counts go to
40 // zero.  Then everything is delete'd for real.  Note that no operations are
41 // valid on an object that has "dropped all references", except operator 
42 // delete.
43 //
44 void Module::dropAllReferences() {
45   MethodListType::iterator MI = MethodList.begin();
46   for (; MI != MethodList.end(); ++MI)
47     (*MI)->dropAllReferences();
48 }
49
50 // reduceApply - Apply the specified function to all of the methods in this 
51 // module.  The result values are or'd together and the result is returned.
52 //
53 bool Module::reduceApply(bool (*Func)(GlobalVariable*)) {
54   return reduce_apply_bool(gbegin(), gend(), Func);
55 }
56 bool Module::reduceApply(bool (*Func)(const GlobalVariable*)) const {
57   return reduce_apply_bool(gbegin(), gend(), Func);
58 }
59 bool Module::reduceApply(bool (*Func)(Method*)) {
60   return reduce_apply_bool(begin(), end(), Func);
61 }
62 bool Module::reduceApply(bool (*Func)(const Method*)) const {
63   return reduce_apply_bool(begin(), end(), Func);
64 }
65