summaryrefslogtreecommitdiffstats
path: root/languages/cpp/simpletypefunction.h
blob: d2169e720a018613fa5c417e2fee9c9360c52ade (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
/***************************************************************************
   copyright            : (C) 2006 by David Nolden
   email                : david.nolden.kdevelop@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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef SIMPLETYPEFUNCTION_H
#define SIMPLETYPEFUNCTION_H

#include "simpletype.h"
#include "simpletypecachebinder.h"
#include "simpletypecatalog.h"

HashedStringSet getIncludeFiles( const ItemDom& item );


///Interface that functions should implement
class SimpleTypeFunctionInterface {
  public:
  
    SimpleTypeFunctionInterface() {
    }
  
    SimpleTypeFunctionInterface( SimpleTypeFunctionInterface* rhs ) {
      m_nextFunction = rhs->m_nextFunction;
    }
  
    void clearNextFunctions() {
      m_nextFunction = SimpleType();
    }
  
    void appendNextFunction( SimpleType func );
  
    SimpleType nextFunction() {
      return m_nextFunction;
    }
  
    ///Returns the totally unresolved return-type
    virtual TypeDesc getReturnType() = 0;
  
    virtual TQValueList<TypeDesc> getArgumentTypes() = 0;
  
    virtual TQStringList getArgumentDefaults() = 0;
  
    virtual TQStringList getArgumentNames() = 0;
  
    virtual bool isConst() = 0;
  
    virtual TQString signature();
  
///TODO: This function should locate the correct overloaded method in the chain, fitting the parameters
    ///should also moved into another class then
    SimpleTypeImpl* match( const TQValueList<LocateResult>& /*params*/ ) {
        //      TQValueList<TypeDesc> args = getArgumentTypes();
      return dynamic_cast<SimpleTypeImpl*>( this );
    }

  private:
        ///Since functions can be overloaded, many functions with the same name can exist. Other functions that belong to this one
    ///should be appended to this.
    SimpleType m_nextFunction;
   
  
  protected:
  
    bool containsUndefinedTemplateParam( TypeDesc& desc, SimpleTypeImpl::TemplateParamInfo& paramInfo );
  
    ///Tries to match the types, filling implicit template-params into paramInfo
    void resolveImplicitTypes( TypeDesc& argType, TypeDesc& gottenArgType, SimpleTypeImpl::TemplateParamInfo& paramInfo );
  
    ///Tries to match the types, filling implicit template-params into paramInfo
    void resolveImplicitTypes( TypeDesc::TemplateParams& argTypes, TypeDesc::TemplateParams& gottenArgTypes, SimpleTypeImpl::TemplateParamInfo& paramInfo );
  
    ///Tries to match the types, filling implicit template-params into paramInfo
    void resolveImplicitTypes( TQValueList<TypeDesc>& argTypes, TQValueList<TypeDesc>& gottenArgTypes, SimpleTypeImpl::TemplateParamInfo& paramInfo );
};

template <class Base=SimpleTypeImpl>
class SimpleTypeFunction : public Base, public SimpleTypeFunctionInterface {
public:
  SimpleTypeFunction() : Base() {
  }
    
  SimpleTypeFunction( SimpleTypeFunction<Base>* rhs ) : Base( rhs ), SimpleTypeFunctionInterface( rhs ) {
  }
  
  template <class Type>
    SimpleTypeFunction( Type t ) : Base( t ) {
    }
  
  virtual ~SimpleTypeFunction() {
  };
  
  virtual SimpleTypeImpl::TypePointer clone() = 0;
  
  virtual SimpleTypeImpl::MemberInfo findMember( TypeDesc name, SimpleTypeImpl::MemberInfo::MemberType type ){
    SimpleTypeImpl::MemberInfo ret;
    if( type & SimpleTypeImpl::MemberInfo::Template ) {
      TypeDesc s = Base::findTemplateParam( name.name() );
      if( s ) {
        ret.memberType = SimpleTypeImpl::MemberInfo::Template;
        ret.type = s;
      }
    }
    
    return ret;
  }
  
  virtual LocateResult applyOperator( typename Base::Operator op , TQValueList<LocateResult> params ) {
    Debug d("#apply#");
    if( !d )
      return LocateResult();
    
    if( op == SimpleTypeImpl::ParenOp ) {
            ///First, try to find an overloaded function matching the parameter-types.
      SimpleTypeImpl* f = match( params );
      if( f && f->asFunction() ) {
        ifVerbose( dbg() << "applying Operator " << this->operatorToString( op ) << " to \"" << f->desc().fullNameChain() << "\"" <<  endl );
        
        TypeDesc rt = f->asFunction()->getReturnType();
        SimpleTypeImpl::TemplateParamInfo paramInfo = f->getTemplateParamInfo();
        if( containsUndefinedTemplateParam( rt, paramInfo ) ) {
                    /** This is the place where implicit template-function-instatiation takes place.
                     *  Match the given param-types with the argument-types to resolve new template-params.
                    */
          TQValueList<TypeDesc> args = getArgumentTypes();
          TQValueList<TypeDesc> paramDescs;
          for( TQValueList<LocateResult>::iterator it = params.begin(); it != params.end(); ++it )
            paramDescs << (TypeDesc)(*it);
          resolveImplicitTypes( args, paramDescs, paramInfo );
                    ///paramInfo now contains the information for all implicit types
        }
        
        return this->parent()->locateDecType( f->replaceTemplateParams( rt, paramInfo ) );
      } else {
        ifVerbose( dbg() << "failed to find a fitting overloaded method" << endl );
      }
    }
    return Base::applyOperator( op, params );
  }
  
};

/**
The SimpleTypeCodeModel and SimpleTypeCatalog can represent namespaces too,
but they only represent a REAL(physical) namespace and only within either the catalog
or the code-model. This is a proxy-class that handles namespace-aliases, imports, and splits requests to both, the catalog, and the code-model.
There can be multiple namespaces aliased to the same one
*/


class SimpleTypeCodeModel : public SimpleTypeImpl {
public:
  
  SimpleTypeCodeModel( SimpleTypeCodeModel* rhs ) : SimpleTypeImpl( rhs ), m_item( rhs->m_item ) {
  }
  
  SimpleTypeCodeModel( SimpleTypeImpl* rhs  ) : SimpleTypeImpl( rhs ) {
    init();
  }
  
  SimpleTypeCodeModel( const TQStringList& scope ) : SimpleTypeImpl( scope ) {
    init();
  }
  
  virtual bool hasNode() const {
    return (bool)m_item;
  };
  
  virtual TQString comment() const {
    if( m_item ) {
      return m_item->comment();
    } else {
      return "";
    }
  };
  
  virtual DeclarationInfo getDeclarationInfo();
  
  SimpleTypeCodeModel( ItemDom& item );
  
  virtual TypePointer clone() {
    return new SimpleTypeCachedCodeModel( this );
  }
  
  virtual Repository rep() {
    return RepoCodeModel;
  }
  
  virtual bool isNamespace() const {
    if( m_item ) {
      return m_item->isNamespace();
    } else {
      return false;
    }
  };
  
  inline ItemDom& item() {
    return m_item;
  }
  
  virtual TemplateParamInfo getTemplateParamInfo();
  
  virtual const LocateResult findTemplateParam( const TQString& name );
  
    /** In case of a class, returns all base-types */
  virtual TQStringList getBaseStrings();
  
  ItemDom locateModelContainer( class CodeModel* m, TypeDesc t, ClassDom cnt = ClassDom() );

  virtual TQString specialization() const;

  virtual void addAliasesTo( SimpleTypeNamespace* ns );
  
private:
  ItemDom m_item;
  
  bool findItem();
  
  void init();

    //ClassDom pickMostRelated( ClassList lst, TQString fn );
  
protected:
  SimpleTypeCodeModel() : SimpleTypeImpl() {
  };
  
  
  struct CodeModelBuildInfo : public TypeBuildInfo {
    ItemDom m_item;
    TypeDesc m_desc;
    TypePointer m_parent;
    
    CodeModelBuildInfo( ItemDom item, const TypeDesc& desc, TypePointer parent ) : m_item( item ), m_desc( desc ), m_parent( parent ) {
    }
    
    virtual TypePointer build();
  };
  
  virtual MemberInfo findMember( TypeDesc name , MemberInfo::MemberType type = MemberInfo::AllTypes) ;

  virtual TQValueList<TypePointer> getMemberClasses( const TypeDesc& name ) ;
};


class SimpleTypeCodeModelFunction : public SimpleTypeFunction<SimpleTypeCodeModel> {
public:
  SimpleTypeCodeModelFunction() : SimpleTypeFunction<SimpleTypeCodeModel>() {
  }
    
  SimpleTypeCodeModelFunction( SimpleTypeCodeModelFunction* rhs ) : SimpleTypeFunction<SimpleTypeCodeModel> ( rhs ) {
  }
  
  virtual SimpleTypeImpl::TypePointer clone() {
    return new SimpleTypeCodeModelFunction( this );
  }
  
  SimpleTypeCodeModelFunction( ItemDom item ) : SimpleTypeFunction<SimpleTypeCodeModel>( item ) {
  }
  
  virtual TypeDesc getReturnType();
  
  virtual bool isConst();
  
  virtual TQValueList<TypeDesc> getArgumentTypes();
  
  virtual TQStringList getArgumentNames();
  
  virtual TQStringList getArgumentDefaults();
  
  struct CodeModelFunctionBuildInfo : public TypeBuildInfo {
    FunctionList m_items;
    TypeDesc m_desc;
    TypePointer m_parent;
    
    CodeModelFunctionBuildInfo( FunctionList items, TypeDesc& desc, TypePointer parent ) : m_items( items ), m_desc( desc ), m_parent( parent ) {
    }
    CodeModelFunctionBuildInfo( FunctionDefinitionList items, TypeDesc& desc, TypePointer parent );
    
    virtual TypePointer build();
  };

private:
  FunctionModel* asFunctionModel() {
    if( ! &(*item() ) ) return 0;
    return dynamic_cast<FunctionModel*>( &(*item() ) );
  }
  
};


class SimpleTypeCatalogFunction : public SimpleTypeFunction<SimpleTypeCatalog> {
public:
  SimpleTypeCatalogFunction() : SimpleTypeFunction<SimpleTypeCatalog>() {
  }
    
  SimpleTypeCatalogFunction( Tag tag ) : SimpleTypeFunction<SimpleTypeCatalog>( tag ) {
  }
    
  SimpleTypeCatalogFunction( SimpleTypeCatalogFunction* rhs ) : SimpleTypeFunction<SimpleTypeCatalog>( rhs )  {
  }
  
  virtual SimpleTypeImpl::TypePointer clone() {
    return new SimpleTypeCatalogFunction( this );
  }
  
  struct CatalogFunctionBuildInfo : public TypeBuildInfo {
    TQValueList<Tag> m_tags;
    TypeDesc m_desc;
    TypePointer m_parent;
    
    CatalogFunctionBuildInfo( TQValueList<Tag> tags, TypeDesc& desc, TypePointer parent ) : m_tags( tags ), m_desc( desc ), m_parent( parent ) {
    }
    
    virtual TypePointer build();
  };
  
  virtual TypeDesc getReturnType();
  
  virtual bool isConst ();
  
  virtual TQStringList getArgumentDefaults() {
    return TQStringList();
  }
  
  virtual TQStringList getArgumentNames();
  
  virtual TQValueList<TypeDesc> getArgumentTypes();
};


#endif