~ubuntu-branches/ubuntu/utopic/kdevelop-php/utopic

« back to all changes in this revision

Viewing changes to duchain/typebuilder.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2010-01-17 17:10:22 UTC
  • Revision ID: james.westby@ubuntu.com-20100117171022-nx770ylotvqxwlmc
Tags: 1.0.0~beta2-1
Initial release (Closes: #565680)

Show diffs side-by-side

added added

removed removed

Lines of Context:
81
81
        iType = IntegralType::TypeNull;
82
82
    } else if (lType == "void") {
83
83
        iType = IntegralType::TypeVoid;
 
84
    } else if (lType == "self" || lType == "this") {
 
85
        DUChainReadLocker lock(DUChain::lock());
 
86
        if ( currentContext()->type() == DUContext::Class && currentContext()->owner() ) {
 
87
            return currentContext()->owner()->abstractType();
 
88
        }
84
89
    } else {
85
90
        //don't use openTypeFromName as it uses cursor for findDeclarations
86
91
        Declaration* decl = findDeclarationImport(ClassDeclarationType, QualifiedIdentifier(type.toLower()), node);
119
124
    return ret;
120
125
}
121
126
 
 
127
/**
 
128
 * Find all (or only one - see @p docCommentName) values for a given needle
 
129
 * in a doc-comment. Needle has to start a line in the doccomment,
 
130
 * i.e.:
 
131
 *
 
132
 *  * @docCommentName value
 
133
 *
 
134
 * or
 
135
 *
 
136
 *  /// @docCommentName value
 
137
 */
 
138
QStringList findInDocComment(const QString &docComment, const QString &docCommentName, const bool onlyOne)
 
139
{
 
140
    QStringList matches;
 
141
    // optimization that does not require potentially slow regexps
 
142
    // old code was something like this:
 
143
    /*
 
144
    if (!docComment.isEmpty()) {
 
145
        QRegExp rx("\\*\\s+@param\\s([^\\s]*)");
 
146
        int pos = 0;
 
147
        while ((pos = rx.indexIn(docComment, pos)) != -1) {
 
148
            ret << parseType(rx.cap(1), node);
 
149
            pos += rx.matchedLength();
 
150
        }
 
151
    }
 
152
    */
 
153
 
 
154
    for ( int i = 0, size = docComment.size(); i < size; ++i ) {
 
155
        if ( docComment[i].isSpace() || docComment[i] == '*' || docComment[i] == '/' ) {
 
156
            // skip whitespace and comment-marker at beginning of line
 
157
            continue;
 
158
        } else if ( docComment[i] == '@' && docComment.midRef(i + 1, docCommentName.size()) == docCommentName ) {
 
159
            // find @return or similar
 
160
            i += docCommentName.size() + 1;
 
161
            // skip whitespace (at least one is required)
 
162
            if ( i >= size || !docComment[i].isSpace() ) {
 
163
                // skip to next line
 
164
                i = docComment.indexOf('\n', i);
 
165
                if ( i == -1 ) {
 
166
                    break;
 
167
                }
 
168
                continue;
 
169
            } else if ( docComment[i] == '\n' ) {
 
170
                continue;
 
171
            }
 
172
            ++i; // at least one whitespace
 
173
            while ( i < size && docComment[i].isSpace() ) {
 
174
                ++i;
 
175
            }
 
176
            // finally get the typename
 
177
            int pos = i;
 
178
            while ( pos < size && !docComment[pos].isSpace() ) {
 
179
                ++pos;
 
180
            }
 
181
            if ( pos > i ) {
 
182
                matches << docComment.mid(i, pos - i);
 
183
                if ( onlyOne ) {
 
184
                    break;
 
185
                } else {
 
186
                    i = pos;
 
187
                }
 
188
            }
 
189
        }
 
190
        // skip to next line
 
191
        i = docComment.indexOf('\n', i);
 
192
        if ( i == -1 ) {
 
193
            break;
 
194
        }
 
195
    }
 
196
 
 
197
    return matches;
 
198
}
 
199
 
122
200
AbstractType::Ptr TypeBuilder::parseDocComment(AstNode* node, const QString& docCommentName)
123
201
{
124
202
    m_gotTypeFromDocComment = false;
125
 
    QString docComment = editor()->parseSession()->docComment(node->startToken);
126
 
    if (!docComment.isEmpty()) {
127
 
        QRegExp rx("(?:\\*|///)\\s+@" + QRegExp::escape(docCommentName) + "\\s+([^\\s]*)");
128
 
        if (rx.indexIn(docComment) != -1) {
 
203
    const QString& docComment = editor()->parseSession()->docComment(node->startToken);
 
204
 
 
205
    if ( !docComment.isEmpty() ) {
 
206
        const QStringList& matches = findInDocComment(docComment, docCommentName, true);
 
207
        if ( !matches.isEmpty() ) {
129
208
            AbstractType::Ptr type;
130
 
            if (rx.cap(1) == "$this") {
 
209
            if (matches.first() == "$this") {
131
210
                DUChainReadLocker lock(DUChain::lock());
132
211
                if (currentContext()->owner()) {
133
212
                    type = currentContext()->owner()->abstractType();
134
213
                }
135
214
            } else {
136
 
                type = injectParseType(rx.cap(1), node);
 
215
                type = injectParseType(matches.first(), node);
137
216
            }
138
217
            if (type) {
139
218
                m_gotTypeFromDocComment = true;
144
223
    return AbstractType::Ptr();
145
224
}
146
225
 
147
 
 
148
226
QList<AbstractType::Ptr> TypeBuilder::parseDocCommentParams(AstNode* node)
149
227
{
150
228
    QList<AbstractType::Ptr> ret;
151
229
    QString docComment = editor()->parseSession()->docComment(node->startToken);
152
 
    if (!docComment.isEmpty()) {
153
 
        QRegExp rx("\\*\\s+@param\\s([^\\s]*)");
154
 
        int pos = 0;
155
 
        while ((pos = rx.indexIn(docComment, pos)) != -1) {
156
 
            ret << parseType(rx.cap(1), node);
157
 
            pos += rx.matchedLength();
 
230
    if ( !docComment.isEmpty() ) {
 
231
        const QStringList& matches = findInDocComment(docComment, "param", false);
 
232
        if ( !matches.isEmpty() ) {
 
233
            foreach ( const QString& type, matches ) {
 
234
                ret << parseType(type, node);
 
235
            }
158
236
        }
159
237
    }
160
238
    return ret;