summaryrefslogtreecommitdiffstats
path: root/languages/cpp/simpletypecatalog.cpp
blob: edf4dfbb3748eeb050b1fb376f16fa2e0b2471d0 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/***************************************************************************
   copyright            : (C) 2006 by David Nolden
   email                : david.nolden.tdevelop@art-master.de
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include "simpletypecatalog.h"
#include "simpletypefunction.h"
#include "safetycounter.h"

extern SafetyCounter safetyCounter;
extern CppCodeCompletion* cppCompletionInstance;

//SimpleTypeCatalog implementation

TypePointer SimpleTypeCatalog::clone() {
 return new SimpleTypeCachedCatalog( this );
}

TQString SimpleTypeCatalog::specialization() const {
 return m_tag.getSpecializationDeclaration();
}

void SimpleTypeCatalog::addAliasesTo( SimpleTypeNamespace* ns ) {
 if ( m_tag.kind() != Tag::Kind_Namespace ) return;
 TQValueList<Catalog::QueryArgument> args;

///Insert all namespace-imports
 args << Catalog::QueryArgument( "scope", specializedScope() );
 args << Catalog::QueryArgument( "kind", Tag::Kind_UsingDirective );

 TQValueList<Tag> tags( cppCompletionInstance->m_repository->query( args ) );

 for ( TQValueList<Tag>::iterator it = tags.begin(); it != tags.end(); ++it ) {
     TypeDesc d( (*it).name() );
     d.setIncludeFiles( HashedString((*it).fileName()) ); ///@todo implement the include-file-logic
     ns->addAliasMap( TypeDesc(), d, HashedString((*it).fileName()), true, false, bigContainer() );
 }
///Insert all namespace-aliases
 args.clear();
 args << Catalog::QueryArgument( "scope", specializedScope() );
 args << Catalog::QueryArgument( "kind", Tag::Kind_NamespaceAlias );

 tags = cppCompletionInstance->m_repository->query( args );

 for ( TQValueList<Tag>::iterator it = tags.begin(); it != tags.end(); ++it ) {
  TQVariant v = (*it).attribute( "alias" );
  if ( v.type() == TQVariant::String ) {
      TypeDesc d( v.asString() );
      d.setIncludeFiles( HashedString((*it).fileName()) );
      ns->addAliasMap( (*it).name(), d, HashedString((*it).fileName()), true, false, bigContainer() );
  } else
   kdDebug( 9007 ) << "namespace-alias has no alias-text" << endl;
 }
}

TQValueList<TypePointer> SimpleTypeCatalog::getMemberClasses( const TypeDesc& name ) {
 TQValueList<TypePointer> ret;

 TQValueList<Catalog::QueryArgument> args;

 args << Catalog::QueryArgument( "scope", specializedScope() );
 args << Catalog::QueryArgument( "name", name.name() );

 TQValueList<Tag> tags( cppCompletionInstance->m_repository->query( args ) );

 for ( TQValueList<Tag>::iterator it = tags.begin(); it != tags.end(); ++it ) {
  if ( (*it).kind() == Tag::Kind_Class ) {
   ///It would be better to return all matched class-names from within findMember and use them from there so all this will be cached too.
   CatalogBuildInfo b( *it, name, TypePointer( this ) );
   TypePointer t = b.buildCached();
   if ( t ) ret << t;
  }
 }

 return ret;
}

SimpleTypeImpl::MemberInfo SimpleTypeCatalog::findMember( TypeDesc name, SimpleTypeImpl::MemberInfo::MemberType type ) {
 MemberInfo ret;
 ret.name = name.name();
 ret.memberType = MemberInfo::NotFound;
 if ( !name ) return ret;

 if (  ( type & MemberInfo::Template) ) {
  LocateResult s = findTemplateParam( name.name() );
  if ( s ) {
   ret.memberType = MemberInfo::Template;
   ret.type = s;
   ret.decl.name = name.name();
   ret.decl.file = m_tag.fileName();
   m_tag.getStartPosition( &ret.decl.startLine, &ret.decl.startCol );
   m_tag.getEndPosition( &ret.decl.endLine, &ret.decl.endCol );
  }
 }

 TQValueList<Catalog::QueryArgument> args;

 args << Catalog::QueryArgument( "scope", specializedScope() );
 args << Catalog::QueryArgument( "name", name.name() );

 TQValueList<Tag> tags( cppCompletionInstance->m_repository->query( args ) );

 if ( tags.isEmpty() )  return ret;

///skip all using-directives
 TQValueList<Tag>::iterator it = tags.begin();
 while ( ( (*it).kind() == Tag::Kind_UsingDirective || (*it).kind() == Tag::Kind_NamespaceAlias ) && it != tags.end() )
  ++it;

 if ( it == tags.end() ) return ret;

 Tag tag = *it;

 if ( tag.kind() == Tag::Kind_Variable && (type & MemberInfo::Variable) ) {
  ret.memberType = MemberInfo::Variable;
  ret.type = tagType( tag );
  ret.decl.name = tag.name();
  ret.decl.comment = tag.comment();
  tag.getStartPosition( &ret.decl.startLine, &ret.decl.startCol );
  tag.getEndPosition( &ret.decl.endLine, &ret.decl.endCol );
  ret.decl.file = tag.fileName();
 }
 if ( tag.kind() == Tag::Kind_Enumerator && (type & MemberInfo::Variable) ) {
  ret.memberType = MemberInfo::Variable;
  if ( !tag.hasAttribute( "enum" ) ) {
   ret.type = TypeDesc( "const int" );
  } else {
   ret.type = tag.attribute( "enum" ).asString();
   if ( ret.type->name().isEmpty() )
    ret.type = TypeDesc( "const int" );
  }
  ret.decl.name = tag.name();
  ret.decl.comment = tag.comment();
  tag.getStartPosition( &ret.decl.startLine, &ret.decl.startCol );
  tag.getEndPosition( &ret.decl.endLine, &ret.decl.endCol );
  ret.decl.file = tag.fileName();
 } else if ( tag.kind() == Tag::Kind_Class && ( type & MemberInfo::NestedType ) ) {
  //if( tag.hasSpecializationDeclaration() ) {
  //Choose another tag(the main class, not a specialization).
  bool hasSpecializationDeclaration = tag.hasSpecializationDeclaration();
  bool isIncluded = name.includeFiles()[tag.fileName()];
  if ( hasSpecializationDeclaration || !isIncluded ) {
   for ( TQValueList<Tag>::const_iterator it = tags.begin(); it != tags.end(); ++it ) {
    if ( (*it).kind() == Tag::Kind_Class && !(*it).hasSpecializationDeclaration() ) {
     if ( name.includeFiles()[(*it).fileName()] ) {
      tag = *it;
      isIncluded = true;
      hasSpecializationDeclaration = false;
     } else if ( hasSpecializationDeclaration ) {
      tag = *it;
      hasSpecializationDeclaration = false;
      isIncluded = false;
     }

     if ( isIncluded && !hasSpecializationDeclaration ) break;
    }
   }
  }
  //only accept non-specialized classes
  if ( !tag.hasSpecializationDeclaration() ) {
   ret.setBuildInfo( new CatalogBuildInfo( tag, name, TypePointer( this ) ) );
   ret.memberType = MemberInfo::NestedType;
   ret.type = name;
  }
 } else if ( tag.kind() == Tag::Kind_Typedef && ( type & MemberInfo::Typedef ) ) {
  ret.memberType = MemberInfo::Typedef;
  ret.type = tagType( tag );
  ret.decl.name = tag.name();
  ret.decl.comment = tag.comment();
  tag.getStartPosition( &ret.decl.startLine, &ret.decl.startCol );
  tag.getEndPosition( &ret.decl.endLine, &ret.decl.endCol );
  ret.decl.file = tag.fileName();
 } else if ( tag.kind() == Tag::Kind_Enum && ( type & MemberInfo::Typedef ) ) {
  ret.memberType = MemberInfo::Typedef;
  ret.type = TypeDesc( "const int" );
  ret.decl.name = tag.name();
  ret.decl.comment = tag.comment();
  tag.getStartPosition( &ret.decl.startLine, &ret.decl.startCol );
  tag.getEndPosition( &ret.decl.endLine, &ret.decl.endCol );
  ret.decl.file = tag.fileName();
 } else if ( (tag.kind() == Tag::Kind_FunctionDeclaration || tag.kind() == Tag::Kind_Function)  && ( type & MemberInfo::Function ) ) {
  ret.memberType = MemberInfo::Function;
  ret.type = tagType( tag );
  ret.type->increaseFunctionDepth();
  ret.setBuildInfo( new SimpleTypeCatalogFunction::CatalogFunctionBuildInfo( tags, name, TypePointer( this ) ) );
 } else if ( tag.kind() == Tag::Kind_Namespace && ( type & MemberInfo::Namespace ) ) {
  ret.setBuildInfo( new CatalogBuildInfo( tag , name, TypePointer( this ) ) );
  ret.memberType = MemberInfo::Namespace;
  ret.type = name;
 }

///Check if it is a template-name

//if( !ret.type) ret.memberType = MemberInfo::NotFound; //constructor..

    if( ret.memberType == MemberInfo::Function || ret.memberType == MemberInfo::Variable || ret.memberType == MemberInfo::Template || ret.memberType == MemberInfo::Typedef || ret.memberType == MemberInfo::NestedType  ) {
         //For redirected types it is necessary to add the include-files of the context they were searched in.
         //That is not quite correct, but it makes sure that at least the same namespace-aliases will be activated while the search for the type.
       
         ret.type->addIncludeFiles( name.includeFiles() );
    }
    
 chooseSpecialization( ret );
 return ret;
}

Tag SimpleTypeCatalog::findSubTag( const TQString& name ) {
 if ( name.isEmpty() ) return Tag();

 TQValueList<Catalog::QueryArgument> args;
 TQTime t;

 t.start();
 args << Catalog::QueryArgument( "scope", specializedScope() );
 args << Catalog::QueryArgument( "name", name );

 TQValueList<Tag> tags( cppCompletionInstance->m_repository->query( args ) );
 if ( ! tags.isEmpty() ) {
  //ifVerbose( dbg() << "findTag: \"" << str() << "\": tag \"" << name << "\" found " << endl );
  return tags.front();
 } else {
  //ifVerbose( dbg() << "findTag: \"" << str() << "\": tag \"" << name << "\" not found " << endl );
  return Tag();
 }
}

TQValueList<Tag> SimpleTypeCatalog::getBaseClassList( ) {
 if ( scope().isEmpty() )
  return TQValueList<Tag>();
 return cppCompletionInstance->m_repository->getBaseClassList( scope().join("::") + specialization() );
}

void SimpleTypeCatalog::initFromTag() {
 TQStringList l = m_tag.scope();
 l << m_tag.name();
 setScope( l );
}

void SimpleTypeCatalog::init() {
 if ( !scope().isEmpty() ) {
  TQStringList l = scope();
  TQStringList cp = l;
  cp.pop_back();
  setScope( cp );
  m_tag = findSubTag( l.back() );
  setScope( l );
  //initFromTag( ); ///must not be done, because it may initialize to wrong namespaces etc.
 }
}

DeclarationInfo SimpleTypeCatalog::getDeclarationInfo() {
 DeclarationInfo ret;

 ret.name = fullTypeResolved();
 if ( m_tag ) {
  ret.file = m_tag.fileName();
  m_tag.getStartPosition( &ret.startLine, &ret.startCol );
  m_tag.getEndPosition( &ret.endLine, &ret.endCol );
  ret.comment = m_tag.comment();
 }

 return ret;
}

TQStringList SimpleTypeCatalog::getBaseStrings() {
 Debug d( "#getbases#" );
 if ( !d || !safetyCounter ) {
  //ifVerbose( dbg() << "\"" << str() << "\": recursion to deep while getting bases" << endl );
  return TQStringList();
 }

 TQStringList ret;
 TQMap<TQString, bool> bases;
// try with parentsc
 TQTime t;
 t.restart();
 TQValueList<Tag> parents( getBaseClassList() );

 TQValueList<Tag>::Iterator it = parents.begin();
 while ( it != parents.end() ) {
  Tag & tag = *it;
  ++it;

  CppBaseClass<Tag> info( tag );

   bases[ info.baseClass() ] = true;
 }
 return bases.keys();
}

SimpleTypeImpl::TemplateParamInfo SimpleTypeCatalog::getTemplateParamInfo() {
 TemplateParamInfo ret;

 if ( m_tag ) {
  if ( m_tag.hasAttribute( "tpl" ) ) {
   TQStringList l = m_tag.attribute( "tpl" ).asStringList();

   TypeDesc::TemplateParams templateParams = m_desc.templateParams();
   uint pi = 0;
   TQStringList::const_iterator it = l.begin();
   while ( it != l.end() ) {
    TemplateParamInfo::TemplateParam curr;
    curr.name = *it;
    curr.number = pi;
    ++pi;
    ++it;
    if ( it != l.end() ) {
     curr.def = *it;
     ++it;
    }
    if ( pi < templateParams.count() )
     curr.value = *templateParams[pi];
    ret.addParam( curr );
   };
  }
 }

 return ret;
}

const LocateResult SimpleTypeCatalog::findTemplateParam( const TQString& name ) {
 if ( m_tag ) {
  if ( m_tag.hasAttribute( "tpl" ) ) {
   TQStringList l = m_tag.attribute( "tpl" ).asStringList();
   ///we need the index, so count the items through
   uint pi = 0;

   TQStringList::const_iterator it = l.begin();
   while ( it != l.end() && *it != name ) {
    ++pi;
    ++it;
    if ( it != l.end() ) ++it;
   };

   TypeDesc::TemplateParams templateParams = m_desc.templateParams();

   if ( it != l.end() &&  pi < templateParams.count() ) {
    return *templateParams[pi];
   } else {
    if ( it != l.end() && *it == name && !(*it).isEmpty()) {
     ++it;
     if ( it != l.end() && !(*it).isEmpty() ) {
      ifVerbose( dbg() << "using default-template-type " << *it << " for " << name << endl );
      return TypeDesc( *it );     ///return default-parameter
     }
    }
   }
  }
 }
 return LocateResult();
}

//SimpleTypeCatalog::CatalogBuildInfo implementation
TypePointer SimpleTypeCatalog::CatalogBuildInfo::build() {
 if ( !m_tag )
  return TypePointer();
 else {
  TypePointer tp = new SimpleTypeCachedCatalog( m_tag );
  tp->parseParams( m_desc );
  if ( m_parent ) tp->setParent( m_parent->bigContainer() );
  return tp;
 }

}

// kate: indent-mode csands; tab-width 4;