tools/icheck/parser/src/libs/cplusplus/LookupContext.h
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef CPLUSPLUS_LOOKUPCONTEXT_H
       
    43 #define CPLUSPLUS_LOOKUPCONTEXT_H
       
    44 
       
    45 #include "CppDocument.h"
       
    46 #include <FullySpecifiedType.h>
       
    47 
       
    48 namespace CPlusPlus {
       
    49 
       
    50 class CPLUSPLUS_EXPORT LookupItem
       
    51 {
       
    52 public:
       
    53     LookupItem()
       
    54         : _lastVisibleSymbol(0) {}
       
    55 
       
    56     LookupItem(const FullySpecifiedType &type, Symbol *lastVisibleSymbol)
       
    57         : _type(type), _lastVisibleSymbol(lastVisibleSymbol) {}
       
    58 
       
    59     FullySpecifiedType type() const { return _type; }
       
    60     void setType(const FullySpecifiedType &type) { _type = type; }
       
    61 
       
    62     Symbol *lastVisibleSymbol() const { return _lastVisibleSymbol; }
       
    63     void setLastVisibleSymbol(Symbol *symbol) { _lastVisibleSymbol = symbol; }
       
    64 
       
    65     bool operator == (const LookupItem &other) const
       
    66     {
       
    67         if (_type == other._type)
       
    68             return _lastVisibleSymbol == other._lastVisibleSymbol;
       
    69 
       
    70         return false;
       
    71     }
       
    72 
       
    73     bool operator != (const LookupItem &result) const
       
    74     { return ! operator == (result); }
       
    75 
       
    76 private:
       
    77     FullySpecifiedType _type;
       
    78     Symbol *_lastVisibleSymbol;
       
    79 };
       
    80 
       
    81 class CPLUSPLUS_EXPORT LookupContext
       
    82 {
       
    83 public:
       
    84     LookupContext(Control *control = 0);
       
    85 
       
    86     LookupContext(Symbol *symbol,
       
    87                   Document::Ptr expressionDocument,
       
    88                   Document::Ptr thisDocument,
       
    89                   const Snapshot &snapshot);
       
    90 
       
    91     bool isValid() const;
       
    92 
       
    93     Control *control() const;
       
    94     Symbol *symbol() const;
       
    95     Document::Ptr expressionDocument() const;
       
    96     Document::Ptr thisDocument() const;
       
    97     Document::Ptr document(const QString &fileName) const;
       
    98     Snapshot snapshot() const;
       
    99 
       
   100     static Symbol *canonicalSymbol(const QList<Symbol *> &candidates,
       
   101                                    NamespaceBinding *globalNamespaceBinding);
       
   102 
       
   103     static Symbol *canonicalSymbol(Symbol *symbol,
       
   104                                    NamespaceBinding *globalNamespaceBinding);
       
   105 
       
   106     static Symbol *canonicalSymbol(const QList<LookupItem> &candidates,
       
   107                                    NamespaceBinding *globalNamespaceBinding);
       
   108 
       
   109     QList<Symbol *> resolve(const Name *name) const
       
   110     { return resolve(name, visibleScopes()); }
       
   111 
       
   112     QList<Symbol *> resolveNamespace(const Name *name) const
       
   113     { return resolveNamespace(name, visibleScopes()); }
       
   114 
       
   115     QList<Symbol *> resolveClass(const Name *name) const
       
   116     { return resolveClass(name, visibleScopes()); }
       
   117 
       
   118     QList<Symbol *> resolveClassOrNamespace(const Name *name) const
       
   119     { return resolveClassOrNamespace(name, visibleScopes()); }
       
   120 
       
   121     QList<Symbol *> resolveObjCClass(const Name *name) const
       
   122     { return resolveObjCClass(name, visibleScopes()); }
       
   123 
       
   124     QList<Symbol *> resolveObjCProtocol(const Name *name) const
       
   125     { return resolveObjCProtocol(name, visibleScopes()); }
       
   126 
       
   127     enum ResolveMode {
       
   128         ResolveSymbol           = 0x01,
       
   129         ResolveClass            = 0x02,
       
   130         ResolveNamespace        = 0x04,
       
   131         ResolveClassOrNamespace = ResolveClass  | ResolveNamespace,
       
   132         ResolveObjCClass        = 0x08,
       
   133         ResolveObjCProtocol     = 0x10,
       
   134         ResolveAll              = ResolveSymbol | ResolveClassOrNamespace | ResolveObjCClass | ResolveObjCProtocol
       
   135     };
       
   136 
       
   137     QList<Symbol *> resolve(const Name *name, const QList<Scope *> &visibleScopes,
       
   138                             ResolveMode mode = ResolveAll) const;
       
   139 
       
   140     QList<Symbol *> resolveNamespace(const Name *name, const QList<Scope *> &visibleScopes) const
       
   141     { return resolve(name, visibleScopes, ResolveNamespace); }
       
   142 
       
   143     QList<Symbol *> resolveClass(const Name *name, const QList<Scope *> &visibleScopes) const
       
   144     { return resolve(name, visibleScopes, ResolveClass); }
       
   145 
       
   146     QList<Symbol *> resolveClassOrNamespace(const Name *name, const QList<Scope *> &visibleScopes) const
       
   147     { return resolve(name, visibleScopes, ResolveClassOrNamespace); }
       
   148 
       
   149     QList<Symbol *> resolveObjCClass(const Name *name, const QList<Scope *> &visibleScopes) const
       
   150     { return resolve(name, visibleScopes, ResolveObjCClass); }
       
   151 
       
   152     QList<Symbol *> resolveObjCProtocol(const Name *name, const QList<Scope *> &visibleScopes) const
       
   153     { return resolve(name, visibleScopes, ResolveObjCProtocol); }
       
   154 
       
   155     QList<Scope *> visibleScopes() const
       
   156     { return _visibleScopes; }
       
   157 
       
   158     QList<Scope *> visibleScopes(Symbol *symbol) const;
       
   159     QList<Scope *> visibleScopes(const LookupItem &result) const;
       
   160 
       
   161     QList<Scope *> expand(const QList<Scope *> &scopes) const;
       
   162 
       
   163     void expand(const QList<Scope *> &scopes, QList<Scope *> *expandedScopes) const;
       
   164 
       
   165     void expand(Scope *scope, const QList<Scope *> &visibleScopes,
       
   166                 QList<Scope *> *expandedScopes) const;
       
   167 
       
   168     void expandNamespace(Namespace *namespaceSymbol,
       
   169                          const QList<Scope *> &visibleScopes,
       
   170                          QList<Scope *> *expandedScopes) const;
       
   171 
       
   172     void expandClass(Class *classSymbol,
       
   173                      const QList<Scope *> &visibleScopes,
       
   174                      QList<Scope *> *expandedScopes) const;
       
   175 
       
   176     void expandBlock(Block *blockSymbol,
       
   177                      const QList<Scope *> &visibleScopes,
       
   178                      QList<Scope *> *expandedScopes) const;
       
   179 
       
   180     void expandFunction(Function *functionSymbol,
       
   181                         const QList<Scope *> &visibleScopes,
       
   182                         QList<Scope *> *expandedScopes) const;
       
   183 
       
   184     void expandObjCMethod(ObjCMethod *method,
       
   185                           const QList<Scope *> &visibleScopes,
       
   186                           QList<Scope *> *expandedScopes) const;
       
   187 
       
   188     void expandObjCClass(ObjCClass *klass,
       
   189                          const QList<Scope *> &visibleScopes,
       
   190                          QList<Scope *> *expandedScopes) const;
       
   191 
       
   192     void expandObjCProtocol(ObjCProtocol *protocol,
       
   193                             const QList<Scope *> &visibleScopes,
       
   194                             QList<Scope *> *expandedScopes) const;
       
   195 
       
   196     void expandEnumOrAnonymousSymbol(ScopedSymbol *scopedSymbol,
       
   197                                      QList<Scope *> *expandedScopes) const;
       
   198 
       
   199 private:
       
   200     static Symbol *canonicalSymbol(Symbol *symbol);
       
   201 
       
   202     QList<Symbol *> resolveQualifiedNameId(const QualifiedNameId *q,
       
   203                                            const QList<Scope *> &visibleScopes,
       
   204                                            ResolveMode mode) const;
       
   205 
       
   206     QList<Symbol *> resolveOperatorNameId(const OperatorNameId *opId,
       
   207                                           const QList<Scope *> &visibleScopes,
       
   208                                           ResolveMode mode) const;
       
   209 
       
   210     QList<Scope *> resolveNestedNameSpecifier(const QualifiedNameId *q,
       
   211                                                const QList<Scope *> &visibleScopes) const;
       
   212 
       
   213     const Identifier *identifier(const Name *name) const;
       
   214 
       
   215     QList<Scope *> buildVisibleScopes();
       
   216 
       
   217     void buildVisibleScopes_helper(Document::Ptr doc, QList<Scope *> *scopes,
       
   218                                    QSet<QString> *processed);
       
   219 
       
   220     static bool maybeValidSymbol(Symbol *symbol,
       
   221                                  ResolveMode mode,
       
   222                                  const QList<Symbol *> &candidates);
       
   223 
       
   224 private:
       
   225     Control *_control;
       
   226 
       
   227     // The current symbol.
       
   228     Symbol *_symbol;
       
   229 
       
   230     // The current expression.
       
   231     Document::Ptr _expressionDocument;
       
   232 
       
   233     // The current document.
       
   234     Document::Ptr _thisDocument;
       
   235 
       
   236     // All documents.
       
   237     Snapshot _snapshot;
       
   238 
       
   239     // Visible scopes.
       
   240     QList<Scope *> _visibleScopes;
       
   241 };
       
   242 
       
   243 uint qHash(const CPlusPlus::LookupItem &result);
       
   244 
       
   245 } // end of namespace CPlusPlus
       
   246 
       
   247 #if defined(Q_CC_MSVC) && _MSC_VER <= 1300
       
   248 //this ensures that code outside QmlJS can use the hash function
       
   249 //it also a workaround for some compilers
       
   250 inline uint qHash(const CPlusPlus::LookupItem &item) { return CPlusPlus::qHash(item); }
       
   251 #endif
       
   252 
       
   253 #endif // CPLUSPLUS_LOOKUPCONTEXT_H