-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathComParameter.cs
160 lines (140 loc) · 6.34 KB
/
ComParameter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.Serialization;
using Rubberduck.Parsing.Grammar;
using Rubberduck.VBEditor.Utility;
using ELEMDESC = System.Runtime.InteropServices.ComTypes.ELEMDESC;
using PARAMFLAG = System.Runtime.InteropServices.ComTypes.PARAMFLAG;
using TYPEATTR = System.Runtime.InteropServices.ComTypes.TYPEATTR;
using TYPEDESC = System.Runtime.InteropServices.ComTypes.TYPEDESC;
using TYPEKIND = System.Runtime.InteropServices.ComTypes.TYPEKIND;
namespace Rubberduck.Parsing.ComReflection
{
[DataContract]
[KnownType(typeof(ComTypeName))]
[DebuggerDisplay("{" + nameof(DeclarationName) + "}")]
public class ComParameter
{
public static ComParameter Void = new ComParameter { _typeName = new ComTypeName(null, string.Empty) };
[DataMember(IsRequired = true)]
public string Name { get; private set; }
public string DeclarationName => $"{(IsOptional ? "Optional " : string.Empty)}{(IsByRef ? "ByRef" : "ByVal")} {Name} As {TypeName}{(IsOptional && DefaultValue != null ? " = " : string.Empty)}{(IsOptional && DefaultValue != null ? _typeName.IsEnumMember ? DefaultAsEnum : DefaultValue : string.Empty)}";
[DataMember(IsRequired = true)]
public bool IsArray { get; private set; }
[DataMember(IsRequired = true)]
public bool IsByRef { get; private set; }
[DataMember(IsRequired = true)]
public bool IsOptional { get; private set; }
[DataMember(IsRequired = true)]
public bool IsReturnValue { get; private set; }
[DataMember(IsRequired = true)]
public bool IsParamArray { get; set; }
[DataMember(IsRequired = true)]
public object DefaultValue { get; private set; }
public bool HasDefaultValue => DefaultValue != null;
public string DefaultAsEnum
{
get
{
if (!_typeName.IsEnumMember || !HasDefaultValue || !ComProject.KnownEnumerations.TryGetValue(_typeName.EnumGuid, out ComEnumeration enumType))
{
return string.Empty;
}
var member = enumType.Members.FirstOrDefault(m => m.Value == (int)DefaultValue);
return member != null ? member.Name : string.Empty;
}
}
[DataMember(IsRequired = true)]
private ComTypeName _typeName;
public string TypeName => _typeName.Name;
[DataMember(IsRequired = true)]
ComMember Parent { get; set; }
public ComProject Project => Parent?.Project;
private ComParameter() { }
public ComParameter(ComMember parent, ELEMDESC elemDesc, ITypeInfo info, string name)
{
Debug.Assert(name != null, "Parameter name is null");
Parent = parent;
Name = name;
var paramDesc = elemDesc.desc.paramdesc;
GetParameterType(elemDesc.tdesc, info);
IsOptional = paramDesc.wParamFlags.HasFlag(PARAMFLAG.PARAMFLAG_FOPT);
IsReturnValue = paramDesc.wParamFlags.HasFlag(PARAMFLAG.PARAMFLAG_FRETVAL);
if (!paramDesc.wParamFlags.HasFlag(PARAMFLAG.PARAMFLAG_FHASDEFAULT) || string.IsNullOrEmpty(name))
{
return;
}
//lpVarValue points to a PARAMDESCEX structure, but we don't care about the cBytes here at all.
//Offset and dereference the VARIANTARG directly.
var defValue = new ComVariant(paramDesc.lpVarValue + Marshal.SizeOf(typeof(ulong)));
DefaultValue = defValue.Value;
}
//This overload should only be used for retrieving the TypeName from a random TYPEATTR. TODO: This really belongs somewhere else.
public ComParameter(TYPEATTR attributes, ITypeInfo info)
{
GetParameterType(attributes.tdescAlias, info);
}
private void GetParameterType(TYPEDESC desc, ITypeInfo info)
{
var vt = (VarEnum)desc.vt;
TYPEDESC tdesc;
if (vt == VarEnum.VT_PTR)
{
tdesc = Marshal.PtrToStructure<TYPEDESC>(desc.lpValue);
GetParameterType(tdesc, info);
IsByRef = true;
}
else if (vt == VarEnum.VT_USERDEFINED)
{
int href;
unchecked
{
href = (int)(desc.lpValue.ToInt64() & 0xFFFFFFFF);
}
try
{
info.GetRefTypeInfo(href, out ITypeInfo refTypeInfo);
refTypeInfo.GetTypeAttr(out IntPtr attribPtr);
using (DisposalActionContainer.Create(attribPtr, refTypeInfo.ReleaseTypeAttr))
{
var attribs = Marshal.PtrToStructure<TYPEATTR>(attribPtr);
var type = new ComDocumentation(refTypeInfo, ComDocumentation.LibraryIndex).Name;
if (attribs.typekind == TYPEKIND.TKIND_ENUM)
{
_typeName = new ComTypeName(Project, type, attribs.guid, Guid.Empty);
}
else if (attribs.typekind == TYPEKIND.TKIND_ALIAS)
{
_typeName = new ComTypeName(Project, type, Guid.Empty, attribs.guid);
}
else
{
_typeName = new ComTypeName(Project, type);
}
}
}
catch (COMException)
{
_typeName = new ComTypeName(Project, Tokens.Object);
}
}
else if (vt == VarEnum.VT_SAFEARRAY || vt == VarEnum.VT_CARRAY || vt.HasFlag(VarEnum.VT_ARRAY))
{
tdesc = Marshal.PtrToStructure<TYPEDESC>(desc.lpValue);
GetParameterType(tdesc, info);
IsArray = true;
}
else if (vt == VarEnum.VT_HRESULT)
{
_typeName = new ComTypeName(Project, Tokens.Long);
}
else
{
_typeName = new ComTypeName(Project, (ComVariant.TypeNames.TryGetValue(vt, out string result)) ? result : Tokens.Object);
}
}
}
}