1fce4895dSRene Gollent /*
2fce4895dSRene Gollent * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3fce4895dSRene Gollent * Distributed under the terms of the MIT License.
4fce4895dSRene Gollent */
5fce4895dSRene Gollent
6fce4895dSRene Gollent #include "FunctionInstance.h"
7fce4895dSRene Gollent
8fce4895dSRene Gollent #include <new>
9fce4895dSRene Gollent
10fce4895dSRene Gollent #include "DisassembledCode.h"
11fce4895dSRene Gollent #include "Function.h"
12fce4895dSRene Gollent #include "FunctionID.h"
13fce4895dSRene Gollent #include "ImageDebugInfo.h"
14fce4895dSRene Gollent #include "LocatableFile.h"
15fce4895dSRene Gollent
16fce4895dSRene Gollent
FunctionInstance(ImageDebugInfo * imageDebugInfo,FunctionDebugInfo * functionDebugInfo)17fce4895dSRene Gollent FunctionInstance::FunctionInstance(ImageDebugInfo* imageDebugInfo,
18fce4895dSRene Gollent FunctionDebugInfo* functionDebugInfo)
19fce4895dSRene Gollent :
20fce4895dSRene Gollent fImageDebugInfo(imageDebugInfo),
21fce4895dSRene Gollent fFunction(NULL),
22fce4895dSRene Gollent fFunctionDebugInfo(functionDebugInfo),
23fce4895dSRene Gollent fSourceCode(NULL),
24fce4895dSRene Gollent fSourceCodeState(FUNCTION_SOURCE_NOT_LOADED)
25fce4895dSRene Gollent {
26fce4895dSRene Gollent fFunctionDebugInfo->AcquireReference();
27fce4895dSRene Gollent // TODO: What about fImageDebugInfo? We must be careful regarding cyclic
28fce4895dSRene Gollent // references.
29fce4895dSRene Gollent }
30fce4895dSRene Gollent
31fce4895dSRene Gollent
~FunctionInstance()32fce4895dSRene Gollent FunctionInstance::~FunctionInstance()
33fce4895dSRene Gollent {
34fce4895dSRene Gollent SetFunction(NULL);
35*3d9b5693SRene Gollent SetSourceCode(NULL, FUNCTION_SOURCE_NOT_LOADED);
36fce4895dSRene Gollent fFunctionDebugInfo->ReleaseReference();
37fce4895dSRene Gollent }
38fce4895dSRene Gollent
39fce4895dSRene Gollent
40fce4895dSRene Gollent FunctionID*
GetFunctionID() const41fce4895dSRene Gollent FunctionInstance::GetFunctionID() const
42fce4895dSRene Gollent {
43fce4895dSRene Gollent if (LocatableFile* file = SourceFile()) {
44fce4895dSRene Gollent BString path;
45fce4895dSRene Gollent file->GetPath(path);
46fce4895dSRene Gollent return new(std::nothrow) SourceFunctionID(path, Name());
47fce4895dSRene Gollent }
48fce4895dSRene Gollent
49fce4895dSRene Gollent return new(std::nothrow) ImageFunctionID(
50fce4895dSRene Gollent GetImageDebugInfo()->GetImageInfo().Name(), Name());
51fce4895dSRene Gollent }
52fce4895dSRene Gollent
53fce4895dSRene Gollent
54fce4895dSRene Gollent void
SetFunction(Function * function)55fce4895dSRene Gollent FunctionInstance::SetFunction(Function* function)
56fce4895dSRene Gollent {
57fce4895dSRene Gollent if (fFunction != NULL)
58fce4895dSRene Gollent fFunction->ReleaseReference();
59fce4895dSRene Gollent
60fce4895dSRene Gollent fFunction = function;
61fce4895dSRene Gollent
62fce4895dSRene Gollent if (fFunction != NULL)
63fce4895dSRene Gollent fFunction->AcquireReference();
64fce4895dSRene Gollent }
65fce4895dSRene Gollent
66fce4895dSRene Gollent
67fce4895dSRene Gollent void
SetSourceCode(DisassembledCode * source,function_source_state state)68fce4895dSRene Gollent FunctionInstance::SetSourceCode(DisassembledCode* source,
69fce4895dSRene Gollent function_source_state state)
70fce4895dSRene Gollent {
71fce4895dSRene Gollent if (source == fSourceCode && state == fSourceCodeState)
72fce4895dSRene Gollent return;
73fce4895dSRene Gollent
74fce4895dSRene Gollent if (fSourceCode != NULL)
75fce4895dSRene Gollent fSourceCode->ReleaseReference();
76fce4895dSRene Gollent
77fce4895dSRene Gollent fSourceCode = source;
78fce4895dSRene Gollent fSourceCodeState = state;
79fce4895dSRene Gollent
80fce4895dSRene Gollent if (fSourceCode != NULL)
81fce4895dSRene Gollent fSourceCode->AcquireReference();
82fce4895dSRene Gollent
83fce4895dSRene Gollent if (fFunction != NULL)
84fce4895dSRene Gollent fFunction->NotifySourceCodeChanged();
85fce4895dSRene Gollent }
86