Update ooo320-m1
[ooovba.git] / autodoc / source / ary / cpp / usedtype.cxx
blob9f3a5bd7960ed37e0631ab71c51d4c1661b44f6c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: usedtype.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <precomp.h>
32 #include <ary/cpp/usedtype.hxx>
35 // NOT FULLY DEFINED SERVICES
36 #include <cosv/tpl/tpltools.hxx>
37 #include <ary/symtreenode.hxx>
38 #include <ary/cpp/c_ce.hxx>
39 #include <ary/cpp/c_class.hxx>
40 #include <ary/cpp/c_namesp.hxx>
41 #include <ary/cpp/c_slntry.hxx>
42 #include <ary/cpp/c_tydef.hxx>
43 #include <ary/cpp/c_traits.hxx>
44 #include <ary/cpp/c_types4cpp.hxx>
45 #include <ary/cpp/c_gate.hxx>
46 #include <ary/cpp/cp_ce.hxx>
47 #include <ary/cpp/cp_type.hxx>
48 #include <ary/doc/d_oldcppdocu.hxx>
49 #include <ary/getncast.hxx>
50 #include "tplparam.hxx"
54 namespace
57 using namespace ::ary::cpp;
58 typedef std::vector< ary::cpp::E_ConVol > PtrLevelVector;
61 inline bool
62 result2bool( intt i_nResult )
63 { return i_nResult < 0; }
66 intt compare_PtrLevelVector(
67 const PtrLevelVector &
68 i_r1,
69 const PtrLevelVector &
70 i_r2 );
71 inline intt
72 compare_ConVol( E_ConVol i_e1,
73 E_ConVol i_e2 )
74 { return intt(i_e1) - intt(i_e2); }
76 inline intt
77 compare_bool( bool i_b1,
78 bool i_b2 )
79 { return i_b1 == i_b2
80 ? 0
81 : i_b1
82 ? -1
83 : +1; }
84 inline intt
85 compare_Specialisation( E_TypeSpecialisation i_e1,
86 E_TypeSpecialisation i_e2 )
87 { return intt(i_e1) - intt(i_e2); }
89 inline bool
90 is_const( E_ConVol i_eCV )
91 { return ( intt(i_eCV) & intt(CONVOL_const) ) != 0; }
93 inline bool
94 is_volatile( E_ConVol i_eCV )
95 { return ( intt(i_eCV) & intt(CONVOL_volatile) ) != 0; }
98 intt
99 compare_PtrLevelVector( const PtrLevelVector & i_r1,
100 const PtrLevelVector & i_r2 )
102 intt nResult = i_r1.size() - i_r2.size();
103 if ( nResult != 0 )
104 return nResult;
106 PtrLevelVector::const_iterator it1 = i_r1.begin();
107 PtrLevelVector::const_iterator it1End = i_r1.end();
108 PtrLevelVector::const_iterator it2 = i_r2.begin();
110 for ( ; it1 != it1End; ++it1, ++it2 )
112 nResult = compare_ConVol(*it1, *it2);
113 if ( nResult != 0 )
114 return nResult;
117 return 0;
121 } // anonymous namespace
126 namespace ary
128 namespace cpp
131 typedef symtree::Node<CeNode_Traits> CeNode;
132 typedef ut::NameChain::const_iterator nc_iter;
134 Ce_id CheckForRelatedCe_inNode(
135 const CeNode & i_node,
136 const StringVector& i_qualification,
137 const String & i_name );
140 UsedType::UsedType(Ce_id i_scope )
141 : aPath(),
142 aPtrLevels(),
143 eConVol_Type(CONVOL_none),
144 bIsReference(false),
145 bIsAbsolute(false),
146 bRefers2BuiltInType(false),
147 eTypeSpecialisation(TYSP_none),
148 nRelatedCe(0),
149 nScope(i_scope)
153 UsedType::~UsedType()
158 bool
159 UsedType::operator<( const UsedType & i_rType ) const
161 intt nResult = compare_bool( bIsAbsolute, i_rType.bIsAbsolute );
162 if ( nResult != 0 )
163 return result2bool(nResult);
165 nResult = static_cast<intt>(nScope.Value())
167 static_cast<intt>(i_rType.nScope.Value());
168 if ( nResult != 0 )
169 return result2bool(nResult);
171 nResult = aPath.Compare( i_rType.aPath );
172 if ( nResult != 0 )
173 return result2bool(nResult);
175 nResult = compare_ConVol( eConVol_Type, i_rType.eConVol_Type );
176 if ( nResult != 0 )
177 return result2bool(nResult);
179 nResult = compare_PtrLevelVector( aPtrLevels, i_rType.aPtrLevels );
180 if ( nResult != 0 )
181 return result2bool(nResult);
183 nResult = compare_bool( bIsReference, i_rType.bIsReference );
184 if ( nResult != 0 )
185 return result2bool(nResult);
187 nResult = compare_Specialisation( eTypeSpecialisation, i_rType.eTypeSpecialisation );
188 if ( nResult != 0 )
189 return result2bool(nResult);
191 return false;
194 void
195 UsedType::Set_Absolute()
197 bIsAbsolute = true;
200 void
201 UsedType::Add_NameSegment( const char * i_sSeg )
203 aPath.Add_Segment(i_sSeg);
206 ut::List_TplParameter &
207 UsedType::Enter_Template()
209 return aPath.Templatize_LastSegment();
212 void
213 UsedType::Set_Unsigned()
215 eTypeSpecialisation = TYSP_unsigned;
218 void
219 UsedType::Set_Signed()
221 eTypeSpecialisation = TYSP_signed;
224 void
225 UsedType::Set_BuiltIn( const char * i_sType )
227 aPath.Add_Segment(i_sType);
228 bRefers2BuiltInType = true;
231 void
232 UsedType::Set_Const()
234 if (PtrLevel() == 0)
235 eConVol_Type = E_ConVol(eConVol_Type | CONVOL_const);
236 else
237 aPtrLevels.back() = E_ConVol(aPtrLevels.back() | CONVOL_const);
240 void
241 UsedType::Set_Volatile()
243 if (PtrLevel() == 0)
244 eConVol_Type = E_ConVol(eConVol_Type | CONVOL_volatile);
245 else
246 aPtrLevels.back() = E_ConVol(aPtrLevels.back() | CONVOL_volatile);
249 void
250 UsedType::Add_PtrLevel()
252 aPtrLevels.push_back(CONVOL_none);
255 void
256 UsedType::Set_Reference()
258 bIsReference = true;
261 inline bool
262 IsInternal(const ary::cpp::CodeEntity & i_ce)
264 const ary::doc::OldCppDocu *
265 docu = dynamic_cast< const ary::doc::OldCppDocu* >(i_ce.Docu().Data());
266 if (docu != 0)
267 return docu->IsInternal();
268 return false;
272 void
273 UsedType::Connect2Ce( const CePilot & i_ces)
275 StringVector
276 qualification;
277 String
278 name;
279 Get_NameParts(qualification, name);
281 for ( const CeNode * scope_node = CeNode_Traits::NodeOf_(
282 i_ces.Find_Ce(nScope));
283 scope_node != 0;
284 scope_node = scope_node->Parent() )
286 nRelatedCe = CheckForRelatedCe_inNode(*scope_node, qualification, name);
287 if ( nRelatedCe.IsValid() )
289 if ( IsInternal(i_ces.Find_Ce(nRelatedCe)) )
290 nRelatedCe = Ce_id(0);
291 return;
293 } // end for
296 void
297 UsedType::Connect2CeOnlyKnownViaBaseClass(const Gate & i_gate)
299 csv_assert(nScope.IsValid());
300 CesResultList
301 instances = i_gate.Ces().Search_TypeName( LocalName() );
303 // If there are no matches, or only one match that was already
304 // accepted, all work is done.
305 if ( (nRelatedCe.IsValid() AND instances.size() == 1)
306 OR instances.size() == 0 )
307 return;
309 StringVector
310 qualification;
311 String
312 name;
313 Get_NameParts(qualification, name);
315 const CodeEntity &
316 scopece = i_gate.Ces().Find_Ce(nScope);
318 // Else search for declaration in own class and then in base classes.
319 // These would be of higher priority than those in parent namespaces.
320 Ce_id
321 foundce = RecursiveSearchCe_InBaseClassesOf(
322 scopece, qualification, name, i_gate);
323 if (foundce.IsValid())
324 nRelatedCe = foundce;
326 if ( nRelatedCe.IsValid() AND IsInternal(i_gate.Ces().Find_Ce(nRelatedCe)) )
328 nRelatedCe = Ce_id(0);
332 bool
333 UsedType::IsBuiltInType() const
335 return bRefers2BuiltInType
336 AND aPtrLevels.size() == 0
337 AND NOT bIsReference
338 AND eConVol_Type == ary::cpp::CONVOL_none;
341 const String &
342 UsedType::LocalName() const
344 return aPath.LastSegment();
347 E_TypeSpecialisation
348 UsedType::TypeSpecialisation() const
350 return eTypeSpecialisation;
353 void
354 UsedType::do_Accept(csv::ProcessorIfc & io_processor) const
356 csv::CheckedCall(io_processor,*this);
359 ary::ClassId
360 UsedType::get_AryClass() const
362 return class_id;
366 UsedType::inq_RelatedCe() const
368 return nRelatedCe.Value();
371 bool
372 UsedType::inq_IsConst() const
374 if ( is_const(eConVol_Type) )
375 return true;
376 for ( PtrLevelVector::const_iterator it = aPtrLevels.begin();
377 it != aPtrLevels.end();
378 ++it )
380 if ( is_const(*it) )
381 return true;
384 return false;
387 void
388 UsedType::inq_Get_Text( StreamStr & o_rPreName,
389 StreamStr & o_rName,
390 StreamStr & o_rPostName,
391 const Gate & i_rGate ) const
393 if ( is_const(eConVol_Type) )
394 o_rPreName << "const ";
395 if ( is_volatile(eConVol_Type) )
396 o_rPreName << "volatile ";
397 if ( bIsAbsolute )
398 o_rPreName << "::";
400 aPath.Get_Text( o_rPreName, o_rName, o_rPostName, i_rGate );
402 for ( PtrLevelVector::const_iterator it = aPtrLevels.begin();
403 it != aPtrLevels.end();
404 ++it )
406 o_rPostName << " *";
407 if ( is_const(*it) )
408 o_rPostName << " const";
409 if ( is_volatile(*it) )
410 o_rPostName << " volatile";
412 if ( bIsReference )
413 o_rPostName << " &";
416 Ce_id
417 UsedType::RecursiveSearchCe_InBaseClassesOf( const CodeEntity & i_mayBeClass,
418 const StringVector & i_myQualification,
419 const String & i_myName,
420 const Gate & i_gate ) const
422 // Find in this class?
423 const CeNode *
424 basenode = CeNode_Traits::NodeOf_(i_mayBeClass);
425 if (basenode == 0)
426 return Ce_id(0);
427 Ce_id
428 found = CheckForRelatedCe_inNode(*basenode, i_myQualification, i_myName);
429 if (found.IsValid())
430 return found;
433 const Class *
434 cl = ary_cast<Class>(&i_mayBeClass);
435 if (cl == 0)
436 return Ce_id(0);
438 for ( List_Bases::const_iterator it = cl->BaseClasses().begin();
439 it != cl->BaseClasses().end();
440 ++it )
442 csv_assert((*it).nId.IsValid());
443 Ce_id
444 base = i_gate.Types().Find_Type((*it).nId).RelatedCe();
445 while (base.IsValid() AND is_type<Typedef>(i_gate.Ces().Find_Ce(base)) )
447 base = i_gate.Types().Find_Type(
448 ary_cast<Typedef>(i_gate.Ces().Find_Ce(base))
449 .DescribingType() )
450 .RelatedCe();
453 if (base.IsValid())
455 const CodeEntity &
456 basece = i_gate.Ces().Find_Ce(base);
457 found = RecursiveSearchCe_InBaseClassesOf(
458 basece, i_myQualification, i_myName, i_gate);
459 if (found.IsValid())
460 return found;
462 } // end for
464 return Ce_id(0);
468 void
469 UsedType::Get_NameParts( StringVector & o_qualification,
470 String & o_name )
472 nc_iter nit = aPath.begin();
473 nc_iter nit_end = aPath.end();
474 csv_assert(nit != nit_end); // Each UsedType has to have a local name.
476 --nit_end;
477 o_name = (*nit_end).Name();
478 for ( ;
479 nit != nit_end;
480 ++nit )
482 o_qualification.push_back( (*nit).Name() );
486 Ce_id
487 CheckForRelatedCe_inNode( const CeNode & i_node,
488 const StringVector & i_qualification,
489 const String & i_name )
491 if (i_qualification.size() > 0)
493 Ce_id
494 ret(0);
495 i_node.SearchBelow( ret,
496 i_qualification.begin(),
497 i_qualification.end(),
498 i_name );
499 return ret;
501 else
503 return i_node.Search(i_name);
508 namespace ut
511 List_TplParameter::List_TplParameter()
512 : aTplParameters()
516 List_TplParameter::~List_TplParameter()
518 csv::erase_container_of_heap_ptrs(aTplParameters);
521 void
522 List_TplParameter::AddParam_Type( Type_id i_nType )
524 aTplParameters.push_back( new TplParameter_Type(i_nType) );
527 void
528 List_TplParameter::Get_Text( StreamStr & o_rOut,
529 const ary::cpp::Gate & i_rGate ) const
531 Vector_TplArgument::const_iterator it = aTplParameters.begin();
532 Vector_TplArgument::const_iterator itEnd = aTplParameters.end();
534 if ( it == itEnd )
536 o_rOut << "<>";
537 return;
540 o_rOut << "< ";
542 (*it)->Get_Text( o_rOut, i_rGate );
544 for ( ++it; it != itEnd; ++it )
546 o_rOut << ", ";
547 (*it)->Get_Text( o_rOut, i_rGate );
550 o_rOut << " >";
553 intt
554 List_TplParameter::Compare( const List_TplParameter & i_rOther ) const
556 intt nResult = intt(aTplParameters.size()) - intt(i_rOther.aTplParameters.size());
558 if (nResult != 0)
559 return nResult;
561 Vector_TplArgument::const_iterator it1 = aTplParameters.begin();
562 Vector_TplArgument::const_iterator it1End = aTplParameters.end();
563 Vector_TplArgument::const_iterator it2 = i_rOther.aTplParameters.begin();
565 for ( ; it1 != it1End; ++it1, ++it2 )
567 nResult = (*it1)->Compare( *(*it2) );
568 if (nResult != 0)
569 return nResult;
572 return 0;
576 } // namespace ut
577 } // namespace cpp
578 } // namespace ary