xref: /aoo41x/main/idlc/source/aststruct.cxx (revision 2fe1ca3d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_idlc.hxx"
26 #include <idlc/aststruct.hxx>
27 #ifndef _IDLC_ASTMember_HXX_
28 #include <idlc/astmember.hxx>
29 #endif
30 
31 #include "registry/version.h"
32 #include "registry/writer.hxx"
33 
34 using namespace ::rtl;
35 
AstStruct(const OString & name,std::vector<rtl::OString> const & typeParameters,AstStruct * pBaseType,AstScope * pScope)36 AstStruct::AstStruct(
37     const OString& name, std::vector< rtl::OString > const & typeParameters,
38     AstStruct* pBaseType, AstScope* pScope)
39 	: AstType(NT_struct, name, pScope)
40 	, AstScope(NT_struct)
41 	, m_pBaseType(pBaseType)
42 {
43     for (std::vector< rtl::OString >::const_iterator i(typeParameters.begin());
44          i != typeParameters.end(); ++i)
45     {
46         m_typeParameters.push_back(
47             new AstDeclaration(NT_type_parameter, *i, 0));
48     }
49 }
50 
AstStruct(const NodeType type,const OString & name,AstStruct * pBaseType,AstScope * pScope)51 AstStruct::AstStruct(const NodeType type,
52 		 		  	 const OString& name,
53 			  		 AstStruct* pBaseType,
54 					 AstScope* pScope)
55 	: AstType(type, name, pScope)
56 	, AstScope(type)
57 	, m_pBaseType(pBaseType)
58 {
59 }
60 
~AstStruct()61 AstStruct::~AstStruct()
62 {
63     for (DeclList::iterator i(m_typeParameters.begin());
64          i != m_typeParameters.end(); ++i)
65     {
66         delete *i;
67     }
68 }
69 
findTypeParameter(rtl::OString const & name) const70 AstDeclaration const * AstStruct::findTypeParameter(rtl::OString const & name)
71     const
72 {
73     for (DeclList::const_iterator i(m_typeParameters.begin());
74          i != m_typeParameters.end(); ++i)
75     {
76         if ((*i)->getLocalName() == name) {
77             return *i;
78         }
79     }
80     return 0;
81 }
82 
isType() const83 bool AstStruct::isType() const {
84     return getNodeType() == NT_struct
85         ? getTypeParameterCount() == 0 : AstDeclaration::isType();
86 }
87 
dump(RegistryKey & rKey)88 sal_Bool AstStruct::dump(RegistryKey& rKey)
89 {
90 	RegistryKey localKey;
91 	if (rKey.createKey( OStringToOUString(getFullName(), RTL_TEXTENCODING_UTF8 ), localKey))
92 	{
93 		fprintf(stderr, "%s: warning, could	not create key '%s' in '%s'\n",
94 			    idlc()->getOptions()->getProgramName().getStr(),
95 			    getFullName().getStr(), OUStringToOString(rKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
96 		return sal_False;
97 	}
98 
99     if (m_typeParameters.size() > SAL_MAX_UINT16) {
100         fprintf(
101             stderr,
102             ("%s: polymorphic struct type template %s has too many type"
103              " parameters\n"),
104             idlc()->getOptions()->getProgramName().getStr(),
105             getScopedName().getStr());
106         return false;
107     }
108 
109 	sal_uInt16 nMember = getNodeCount(NT_member);
110 
111 	RTTypeClass typeClass = RT_TYPE_STRUCT;
112 	if ( getNodeType() == NT_exception )
113 		typeClass = RT_TYPE_EXCEPTION;
114 
115     OUString emptyStr;
116 	typereg::Writer aBlob(
117         (m_typeParameters.empty() && !m_bPublished
118          ? TYPEREG_VERSION_0 : TYPEREG_VERSION_1),
119         getDocumentation(), emptyStr, typeClass, m_bPublished,
120         OStringToOUString(getRelativName(), RTL_TEXTENCODING_UTF8),
121         m_pBaseType == 0 ? 0 : 1, nMember, 0,
122         static_cast< sal_uInt16 >(m_typeParameters.size()));
123     if (m_pBaseType != 0) {
124         aBlob.setSuperTypeName(
125             0,
126             OStringToOUString(
127                 m_pBaseType->getRelativName(), RTL_TEXTENCODING_UTF8));
128     }
129 
130 	if ( nMember > 0 )
131 	{
132 		DeclList::const_iterator iter = getIteratorBegin();
133 		DeclList::const_iterator end = getIteratorEnd();
134 		AstDeclaration* pDecl = NULL;
135 		AstMember* 	pMember = NULL;
136 		OUString 	docu;
137 		sal_uInt16 	index = 0;
138 		while ( iter != end )
139 		{
140 			pDecl = *iter;
141 			if ( pDecl->getNodeType() == NT_member )
142 			{
143 				pMember = (AstMember*)pDecl;
144                 RTFieldAccess flags = RT_ACCESS_READWRITE;
145                 rtl::OString typeName;
146                 if (pMember->getType()->getNodeType() == NT_type_parameter) {
147                     flags |= RT_ACCESS_PARAMETERIZED_TYPE;
148                     typeName = pMember->getType()->getLocalName();
149                 } else {
150                     typeName = pMember->getType()->getRelativName();
151                 }
152 				aBlob.setFieldData(
153                     index++, pMember->getDocumentation(), emptyStr, flags,
154 					OStringToOUString(
155                         pMember->getLocalName(), RTL_TEXTENCODING_UTF8),
156 					OStringToOUString(typeName, RTL_TEXTENCODING_UTF8),
157                     RTConstValue());
158 			}
159 			++iter;
160 		}
161 	}
162 
163     sal_uInt16 index = 0;
164     for (DeclList::iterator i(m_typeParameters.begin());
165          i != m_typeParameters.end(); ++i)
166     {
167         aBlob.setReferenceData(
168             index++, emptyStr, RT_REF_TYPE_PARAMETER, RT_ACCESS_INVALID,
169             OStringToOUString(
170                 (*i)->getLocalName(), RTL_TEXTENCODING_UTF8));
171     }
172 
173     sal_uInt32 aBlobSize;
174     void const * pBlob = aBlob.getBlob(&aBlobSize);
175 
176 	if (localKey.setValue(emptyStr, RG_VALUETYPE_BINARY,
177 					  	  (RegValue)pBlob, aBlobSize))
178 	{
179 		fprintf(stderr, "%s: warning, could	not set value of key \"%s\" in %s\n",
180 			    idlc()->getOptions()->getProgramName().getStr(),
181 				getFullName().getStr(), OUStringToOString(localKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
182 		return sal_False;
183 	}
184 
185 	return sal_True;
186 }
187 
188