void QSResolveNode::check( QSCheckData *c ) { if ( !c->directLookupEnabled() ) return; QSClass * cl = c->currentScope(); QSClass *cont = cl; int uplvl = 0; int blocks = 0; QSMember member; while ( cont ) { QSMember mem; if ( cont->member( 0, ident, &mem ) ) { if ( mem.type()==QSMember::Variable && !mem.isStatic() ) { member = mem; break; } } uplvl++; cont = cont->enclosingClass(); } if( member.isDefined() ) { /* If the containing class has an undefined base, the indexes will be moved when the we call QSAbstractBaseClass::replace() and the lookup info will be crap. Therefore, we don't create it. */ QSClass *tmp = cont->base(); while (tmp) { if (tmp->name() == QString::fromLatin1("AbstractBase")) return; tmp = tmp->base(); } // Due to special handling of block scopes in the global object... if( cont==c->env()->globalClass() ) uplvl+=blocks; info = new QSLookupInfo( uplvl, member ); } }
void QSClassDefNode::check( QSCheckData *c ) { // qDebug( "Class noticed: " + c->globalName( ident ) ); // forward declaration ? if ( !body ) { c->addWarning( this, QString::fromLatin1("Forward declarations not supported, yet") ); return; } if ( attrs ) attrs->check( c ); else c->setLastAttributes( AttributeNone ); int as = c->lastAttributes(); // base class QSClass *b = c->env()->objectClass(); if ( type ) { type->check( c ); if ( c->hasError() ) return; Q_ASSERT( c->lastType() ); b = c->lastType(); // try to cast if ( b->isFinal() ) { c->addError( this, QSErrClassBaseFinal, QString::fromLatin1("Base class has been declared final") ); return; } } // create new class description QSClass *scope = c->currentScope(); if ( scope->name()==QString::fromLatin1("Class") ) { if ( !(as&AttributeStatic) ) { c->addError( this, QSErrNestedClass, QString::fromLatin1( "Nested class '%1' in class '%2' must be " "declared static" ) .arg( ident ).arg( scope->identifier() ) ); return; } } else if ( as&AttributeStatic ) { c->addError( this, QSErrAttributeConflict, QString::fromLatin1( "Static class '%1' cannot be declared outside " "a class" ).arg( ident ) ); return; } else if ( scope->name()==QString::fromLatin1("FunctionScope") ) { c->addError( this, QSErrNestedClass, QString::fromLatin1( "Class '%1' cannot be declared inside function '%2'" ) .arg( ident ).arg( scope->identifier() ) ); return; } QSClass *absBase = c->env()->classByIdentifier(ident); bool usesAbstractBase = absBase && absBase->name() == QString::fromLatin1("AbstractBase"); QSClassClass *cl = new QSClassClass( b, as, ident ); ref(); // Compensate for setClassDefNode below. Dereffed in QSClassClass destructor. cl->setClassDefNode( this ); cl->setEnclosingClass( scope ); QSMember member; QSObject tobj = c->env()->typeClass()->createType(cl); if( scope->member( 0, cl->identifier(), &member ) ) { scope->replaceMember( cl->identifier(), &member, tobj ); } else { scope->addStaticVariableMember( cl->identifier(), tobj, AttributeExecutable ); } c->enterClass( cl ); body->check( c ); c->leaveClass(); // if there is a function with the same name it becomes the constructor QSMember ctor; if ( cl->member( 0, ident, &ctor ) ) { if ( ctor.isExecutable() ) { cl->setHasDefaultConstructor( true ); } else { qWarning( "Constructor is no function" ); // pedantic } } cl->setClassBodyNode( body ); QSFunctionScopeClass *fs = new QSFunctionScopeClass( c->env()->objectClass() ); fs->setEnclosingClass( cl ); body->setScopeDefinition( fs ); fs->setFunctionBodyNode(body); // will be used in execute() this->cldef = cl; // If the classname exists in env and is a previously undefined type, we have to // replace it with this one. if (usesAbstractBase) { ((QSAbstractBaseClass*)absBase)->replace(cl); } }
void QSFuncDeclNode::check( QSCheckData *c ) { // qDebug( "Function noticed: " + c->globalName(ident) ); if ( attrs ) attrs->check( c ); else c->setLastAttributes( AttributeNone ); int as = c->lastAttributes(); QSClass * cl = c->currentScope(); Q_ASSERT( cl ); if ( (as&AttributeStatic) && cl->name() != QString::fromLatin1("Class") ) { c->addError( this, QSErrAttrStaticContext, QString::fromLatin1( "Function '%1' cannot be declared static " "outside a class" ).arg( ident ) ); return; } // A bit of magic fail early when trying to overwrite a context. if (c->inGlobal()) { QSObject object = c->env()->globalObject().get(ident); if (object.isValid()) { if (object.objectType()->name() == QString::fromLatin1("QObject")) { c->addError(this, QString("Cannot declare function '%1', already a global object " "present with same name").arg(ident)); return; } } } QSMember m; m.setPrivate( as&AttributePrivate ); if ( cl->member( 0, ident, &m ) ) { QSMember mem( body, as ); cl->replaceMember( ident, &mem ); } else { cl->addFunctionMember( ident, body, as ); } int tmpVarBlockCount = c->varBlockCount(); c->setVarBlockCount( 0 ); QSFunctionScopeClass * fscope = new QSFunctionScopeClass( c->env()->objectClass(), this ); fscope->setEnclosingClass( cl ); body->setScopeDefinition( fscope ); fscope->setFunctionBodyNode(body); c->enterFunction( fscope ); if( param ) param->check( c ); body->check( c ); c->leaveFunction(); if( c->varBlockCount()>fscope->numVariables() ) fscope->setNumVariables( c->varBlockCount() ); c->setVarBlockCount( tmpVarBlockCount ); // Calculate the number of arguments int count = 0; QSParameterNode * node = param; while( node ) { count++; node = node->nextParam(); } fscope->setNumArguments( count ); // unset attributes c->setLastAttributes( AttributeNone ); }