1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5//
6
7#ifndef __CustAttr__h__
8#define __CustAttr__h__
9
10//#include "stdafx.h"
11#include "corhdr.h"
12#include "cahlprinternal.h"
13#include "sarray.h"
14#include "factory.h"
15
16//*****************************************************************************
17// Argument parsing. The custom attributes may have ctor arguments, and may
18// have named arguments. The arguments are defined by the following tables.
19//
20// These tables also include a member to contain the value of the argument,
21// which is used at runtime. When parsing a given custom attribute, a copy
22// of the argument descriptors is filled in with the values for the instance
23// of the custom attribute.
24//
25// For each ctor arg, there is a CaArg struct, with the type. At runtime,
26// a value is filled in for each ctor argument.
27//
28// For each named arg, there is a CaNamedArg struct, with the name of the
29// argument, the expected type of the argument, if the type is an enum,
30// the name of the enum. Also, at runtime, a value is filled in for
31// each named argument found.
32//
33// Note that arrays and variants are not supported.
34//
35// At runtime, after the args have been parsed, the tag field of CaValue
36// can be used to determine if a particular arg was given.
37//*****************************************************************************
38struct CaArg
39{
40 void InitEnum(CorSerializationType _enumType, INT64 _val = 0)
41 {
42 CaTypeCtor caType(SERIALIZATION_TYPE_ENUM, SERIALIZATION_TYPE_UNDEFINED, _enumType, NULL, 0);
43 Init(caType, _val);
44 }
45 void Init(CorSerializationType _type, INT64 _val = 0)
46 {
47 _ASSERTE(_type != SERIALIZATION_TYPE_ENUM);
48 _ASSERTE(_type != SERIALIZATION_TYPE_SZARRAY);
49 CaTypeCtor caType(_type);
50 Init(caType, _val);
51 }
52 void Init(CaType _type, INT64 _val = 0)
53 {
54 type = _type;
55 memset(&val, 0, sizeof(CaValue));
56 val.i8 = _val;
57 }
58
59 CaType type;
60 CaValue val;
61};
62
63struct CaNamedArg
64{
65 void InitI4FieldEnum(LPCSTR _szName, LPCSTR _szEnumName, INT64 _val = 0)
66 {
67 CaTypeCtor caType(SERIALIZATION_TYPE_ENUM, SERIALIZATION_TYPE_UNDEFINED, SERIALIZATION_TYPE_I4, _szEnumName, (ULONG)strlen(_szEnumName));
68 Init(_szName, SERIALIZATION_TYPE_FIELD, caType, _val);
69 }
70
71 void InitBoolField(LPCSTR _szName, INT64 _val = 0)
72 {
73 CaTypeCtor caType(SERIALIZATION_TYPE_BOOLEAN);
74 Init(_szName, SERIALIZATION_TYPE_FIELD, caType, _val);
75 }
76
77 void Init(LPCSTR _szName, CorSerializationType _propertyOrField, CaType _type, INT64 _val = 0)
78 {
79 szName = _szName;
80 cName = _szName ? (ULONG)strlen(_szName) : 0;
81 propertyOrField = _propertyOrField;
82 type = _type;
83
84 memset(&val, 0, sizeof(CaValue));
85 val.i8 = _val;
86 }
87
88 LPCSTR szName;
89 ULONG cName;
90 CorSerializationType propertyOrField;
91 CaType type;
92 CaValue val;
93};
94
95struct CaNamedArgCtor : public CaNamedArg
96{
97 CaNamedArgCtor()
98 {
99 memset(this, 0, sizeof(CaNamedArg));
100 }
101};
102
103HRESULT ParseEncodedType(
104 CustomAttributeParser &ca,
105 CaType* pCaType);
106
107HRESULT ParseKnownCaArgs(
108 CustomAttributeParser &ca, // The Custom Attribute blob.
109 CaArg *pArgs, // Array of argument descriptors.
110 ULONG cArgs); // Count of argument descriptors.
111
112HRESULT ParseKnownCaNamedArgs(
113 CustomAttributeParser &ca, // The Custom Attribute blob.
114 CaNamedArg *pNamedArgs, // Array of argument descriptors.
115 ULONG cNamedArgs); // Count of argument descriptors.
116
117#endif
118