xref: /trunk/main/idlc/source/aststruct.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_idlc.hxx"
30 #include <idlc/aststruct.hxx>
31 #ifndef _IDLC_ASTMember_HXX_
32 #include <idlc/astmember.hxx>
33 #endif
34 
35 #include "registry/version.h"
36 #include "registry/writer.hxx"
37 
38 using namespace ::rtl;
39 
40 AstStruct::AstStruct(
41     const OString& name, std::vector< rtl::OString > const & typeParameters,
42     AstStruct* pBaseType, AstScope* pScope)
43 	: AstType(NT_struct, name, pScope)
44 	, AstScope(NT_struct)
45 	, m_pBaseType(pBaseType)
46 {
47     for (std::vector< rtl::OString >::const_iterator i(typeParameters.begin());
48          i != typeParameters.end(); ++i)
49     {
50         m_typeParameters.push_back(
51             new AstDeclaration(NT_type_parameter, *i, 0));
52     }
53 }
54 
55 AstStruct::AstStruct(const NodeType type,
56 		 		  	 const OString& name,
57 			  		 AstStruct* pBaseType,
58 					 AstScope* pScope)
59 	: AstType(type, name, pScope)
60 	, AstScope(type)
61 	, m_pBaseType(pBaseType)
62 {
63 }
64 
65 AstStruct::~AstStruct()
66 {
67     for (DeclList::iterator i(m_typeParameters.begin());
68          i != m_typeParameters.end(); ++i)
69     {
70         delete *i;
71     }
72 }
73 
74 AstDeclaration const * AstStruct::findTypeParameter(rtl::OString const & name)
75     const
76 {
77     for (DeclList::const_iterator i(m_typeParameters.begin());
78          i != m_typeParameters.end(); ++i)
79     {
80         if ((*i)->getLocalName() == name) {
81             return *i;
82         }
83     }
84     return 0;
85 }
86 
87 bool AstStruct::isType() const {
88     return getNodeType() == NT_struct
89         ? getTypeParameterCount() == 0 : AstDeclaration::isType();
90 }
91 
92 sal_Bool AstStruct::dump(RegistryKey& rKey)
93 {
94 	RegistryKey localKey;
95 	if (rKey.createKey( OStringToOUString(getFullName(), RTL_TEXTENCODING_UTF8 ), localKey))
96 	{
97 		fprintf(stderr, "%s: warning, could	not create key '%s' in '%s'\n",
98 			    idlc()->getOptions()->getProgramName().getStr(),
99 			    getFullName().getStr(), OUStringToOString(rKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
100 		return sal_False;
101 	}
102 
103     if (m_typeParameters.size() > SAL_MAX_UINT16) {
104         fprintf(
105             stderr,
106             ("%s: polymorphic struct type template %s has too many type"
107              " parameters\n"),
108             idlc()->getOptions()->getProgramName().getStr(),
109             getScopedName().getStr());
110         return false;
111     }
112 
113 	sal_uInt16 nMember = getNodeCount(NT_member);
114 
115 	RTTypeClass typeClass = RT_TYPE_STRUCT;
116 	if ( getNodeType() == NT_exception )
117 		typeClass = RT_TYPE_EXCEPTION;
118 
119     OUString emptyStr;
120 	typereg::Writer aBlob(
121         (m_typeParameters.empty() && !m_bPublished
122          ? TYPEREG_VERSION_0 : TYPEREG_VERSION_1),
123         getDocumentation(), emptyStr, typeClass, m_bPublished,
124         OStringToOUString(getRelativName(), RTL_TEXTENCODING_UTF8),
125         m_pBaseType == 0 ? 0 : 1, nMember, 0,
126         static_cast< sal_uInt16 >(m_typeParameters.size()));
127     if (m_pBaseType != 0) {
128         aBlob.setSuperTypeName(
129             0,
130             OStringToOUString(
131                 m_pBaseType->getRelativName(), RTL_TEXTENCODING_UTF8));
132     }
133 
134 	if ( nMember > 0 )
135 	{
136 		DeclList::const_iterator iter = getIteratorBegin();
137 		DeclList::const_iterator end = getIteratorEnd();
138 		AstDeclaration* pDecl = NULL;
139 		AstMember* 	pMember = NULL;
140 		OUString 	docu;
141 		sal_uInt16 	index = 0;
142 		while ( iter != end )
143 		{
144 			pDecl = *iter;
145 			if ( pDecl->getNodeType() == NT_member )
146 			{
147 				pMember = (AstMember*)pDecl;
148                 RTFieldAccess flags = RT_ACCESS_READWRITE;
149                 rtl::OString typeName;
150                 if (pMember->getType()->getNodeType() == NT_type_parameter) {
151                     flags |= RT_ACCESS_PARAMETERIZED_TYPE;
152                     typeName = pMember->getType()->getLocalName();
153                 } else {
154                     typeName = pMember->getType()->getRelativName();
155                 }
156 				aBlob.setFieldData(
157                     index++, pMember->getDocumentation(), emptyStr, flags,
158 					OStringToOUString(
159                         pMember->getLocalName(), RTL_TEXTENCODING_UTF8),
160 					OStringToOUString(typeName, RTL_TEXTENCODING_UTF8),
161                     RTConstValue());
162 			}
163 			++iter;
164 		}
165 	}
166 
167     sal_uInt16 index = 0;
168     for (DeclList::iterator i(m_typeParameters.begin());
169          i != m_typeParameters.end(); ++i)
170     {
171         aBlob.setReferenceData(
172             index++, emptyStr, RT_REF_TYPE_PARAMETER, RT_ACCESS_INVALID,
173             OStringToOUString(
174                 (*i)->getLocalName(), RTL_TEXTENCODING_UTF8));
175     }
176 
177     sal_uInt32 aBlobSize;
178     void const * pBlob = aBlob.getBlob(&aBlobSize);
179 
180 	if (localKey.setValue(emptyStr, RG_VALUETYPE_BINARY,
181 					  	  (RegValue)pBlob, aBlobSize))
182 	{
183 		fprintf(stderr, "%s: warning, could	not set value of key \"%s\" in %s\n",
184 			    idlc()->getOptions()->getProgramName().getStr(),
185 				getFullName().getStr(), OUStringToOString(localKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
186 		return sal_False;
187 	}
188 
189 	return sal_True;
190 }
191 
192