Exemple #1
0
static void resolveTypePrototypes( void ) {
/*****************************************/

/* adds required protoypes to the protoype section */

    statement   *finger;
    statement   *rc;
    long        len;
    char        *name;
    char        *libname;

    if( !SRU.type_sec ) {
        return;
    }

    finger = SRU.forward_prots;

    /* loop through the chain of prototypes and add them if necessary */
    while( finger ) {
        name = finger->data.sp.name;
        len = strlen( name );

        /* check in hash table for function name */
        if( !FindHashEntry(SRU.type_prots, HashString(name, len), name, len) ) {

            /* add to prototype section */
            rc = insertTypePrototype( finger, SRU.type_sec );
            rc->link = SRU.cpp_prots;
            SRU.cpp_prots = rc;
            rc->keep = TRUE;
            InsertHashValue( SRU.type_prots, rc->data.sp.name,
                             strlen( rc->data.sp.name ), rc );
        }
        finger = finger->link;
    }

    /* generate constructors and destructor prototypes if necessary */
    libname = GetLibName();
    len = max( sizeof( DES_DECL_TEMPLATE ) + strlen( SRU.des_name ),
               sizeof( CONS_DECL_TEMPLATE ) + strlen( SRU.con_name ) );
    name = alloca( len + strlen( libname ) + 1 );
    if( !( SRU.flags & DESTRUCTOR_DEFINED ) ) {
        sprintf( name, DES_DECL_TEMPLATE, SRU.des_name, libname );
        insertStatement( SRU.type_sec, name );
    }
    if( !( SRU.flags & CONSTRUCTOR_DEFINED ) ) {
        sprintf( name, CONS_DECL_TEMPLATE, SRU.con_name, libname );
        insertStatement( SRU.type_sec, name );
    }
}
Exemple #2
0
bool Repository::create(StorageObject sObj, int &uid)
{
    if(!insertStatement(sObj))
        return false;
    uid = db->lastUid();
    return true;
}
void LocalStorageDatabase::updateDatabaseWithChangedItems(const HashMap<String, String>& changedItems)
{
    if (!m_database.isOpen())
        openDatabase(CreateIfNonExistent);
    if (!m_database.isOpen())
        return;

    if (m_shouldClearItems) {
        m_shouldClearItems = false;

        SQLiteStatement clearStatement(m_database, "DELETE FROM ItemTable");
        if (clearStatement.prepare() != SQLITE_OK) {
            LOG_ERROR("Failed to prepare clear statement - cannot write to local storage database");
            return;
        }

        int result = clearStatement.step();
        if (result != SQLITE_DONE) {
            LOG_ERROR("Failed to clear all items in the local storage database - %i", result);
            return;
        }
    }

    SQLiteStatement insertStatement(m_database, "INSERT INTO ItemTable VALUES (?, ?)");
    if (insertStatement.prepare() != SQLITE_OK) {
        LOG_ERROR("Failed to prepare insert statement - cannot write to local storage database");
        return;
    }

    SQLiteStatement deleteStatement(m_database, "DELETE FROM ItemTable WHERE key=?");
    if (deleteStatement.prepare() != SQLITE_OK) {
        LOG_ERROR("Failed to prepare delete statement - cannot write to local storage database");
        return;
    }

    SQLiteTransaction transaction(m_database);
    transaction.begin();

    for (auto it = changedItems.begin(), end = changedItems.end(); it != end; ++it) {
        // A null value means that the key/value pair should be deleted.
        SQLiteStatement& statement = it->value.isNull() ? deleteStatement : insertStatement;

        statement.bindText(1, it->key);

        // If we're inserting a key/value pair, bind the value as well.
        if (!it->value.isNull())
            statement.bindBlob(2, it->value);

        int result = statement.step();
        if (result != SQLITE_DONE) {
            LOG_ERROR("Failed to update item in the local storage database - %i", result);
            break;
        }

        statement.reset();
    }

    transaction.commit();
}
void IconDataCache::writeToDatabase(SQLDatabase& db)
{
    if (m_iconURL.isEmpty())
        return;
    
    // First we'll try an update in case we're already in the DB
    SQLStatement updateAttempt(db, "UPDATE Icon SET stamp = ?, data = ? WHERE url = ?;");
    if (updateAttempt.prepare() != SQLResultOk) {
        LOG_ERROR("Failed to prepare SQL statement to update icon data for url %s", m_iconURL.ascii().data());
        return;
    }
    
    // Bind the url and timestamp
    if (updateAttempt.bindInt64(1, m_stamp) != SQLResultOk) {
        LOG_ERROR("Failed to bind iconID to SQL statement to update icon data for url %s", m_iconURL.ascii().data());
        return;    
    }
    if (updateAttempt.bindText16(3, m_iconURL) != SQLResultOk) {
        LOG_ERROR("Failed to bind url to SQL statement to update icon data for url %s", m_iconURL.ascii().data());
        return;    
    }
    
    // If we *have* image data, bind it to this statement - Otherwise the DB will get "null" for the blob data, 
    // signifying that this icon doesn't have any data    
    if (m_image && !m_image->dataBuffer().isEmpty())
        if (updateAttempt.bindBlob(2, m_image->dataBuffer().data(), m_image->dataBuffer().size()) != SQLResultOk) {
            LOG_ERROR("Failed to bind icon data to SQL statement to update icon data for url %s", m_iconURL.ascii().data());
            return;
        }
    
    if (updateAttempt.step() != SQLResultDone) {
        LOG_ERROR("Failed to update icon data for IconURL %s", m_iconURL.ascii().data());
        return;
    }
    
    // If the UPDATE command actually altered a row, we're done
    if (db.lastChanges())
        return;
        
        
    // Otherwise, we've never inserted this Icon URL before, so we'll do so now
    updateAttempt.finalize();
    SQLStatement insertStatement(db, "INSERT INTO Icon (url,stamp,data) VALUES (?, ?, ?);");
    insertStatement.prepare();
        
    // Bind the url and timestamp
    insertStatement.bindText16(1, m_iconURL);
    insertStatement.bindInt64(2, m_stamp);
        
    // Then, if we *have* data, we bind it.  Otherwise the DB will get "null" for the blob data, 
    // signifying that this icon doesn't have any data
    if (m_image && !m_image->dataBuffer().isEmpty())
        insertStatement.bindBlob(3, m_image->dataBuffer().data(), m_image->dataBuffer().size());
    
    // Finally we step and make sure the step was successful
    if (insertStatement.step() != SQLResultDone)
        LOG_ERROR("Unable to set icon data for IconURL %s", m_iconURL.ascii().data());
}
Exemple #5
0
static statement *insertTypePrototype( statement *func, statement *locale ) {
/***************************************************************************/

/* insert a prototype of the external function into the types protoype section*/

    char        *line;
    char        *name;
    char        *libname;
    char        *sp;
    char        *ret;
    var_rec     *finger;
    statement   *rc;
    long        size;
    TypeInfo    *typ;
    char        typbuf[64];
    TypeInfo    tmptype;

    assert( func );
    assert( locale );

    name =  func->data.sp.name;

    /* determine return type */
    if( !func->data.sp.subroutine ) {
        typ = &func->data.sp.ret_type;
        sp = FUNCTION;
        if( _IsRefType( func->data.sp.typ_id ) ) {
            strcpy( typbuf, REF_MODIFIER );
            strcat( typbuf, typ->name );
            ret = typbuf;
            if( !func->data.sp.typ_id ) {
                func->data.sp.fake = TRUE;
            }
        } else {
            ret = typ->name;
        }
    } else {
        ret = "";
        sp = SUBROUTINE;
    }

    /* calculate size of statement */
    libname = GetLibName();
    size = strlen( name ) + strlen( sp ) + strlen( ret ) + strlen( libname );
    size += sizeof( DECL_THIS_VAR ) + sizeof( LIBRARY  ) + sizeof( LEFT_PAREN )
          + sizeof( RIGHT_PAREN ) + sizeof( DLL_SUFFIX ) + sizeof( DQUOTE )
          + sizeof( DQUOTE ) + OVERHEAD;

    finger = func->data.sp.parm_list;
    while( finger ) {
        size += sizeof( COMMA_DELIM ) + strlen( finger->name ) + 3;
        if( _IsRefType( finger->typ_id ) ) {
            size += sizeof( REF_MODIFIER );
        }
        size += strlen( finger->type.name );
        finger = finger->next;
    }


    /* build the statement */
    line = alloca( size );
    finger = func->data.sp.parm_list;
    if( Options & OPT_GEN_C_CODE ) {
        sprintf( line, "%s %s %s %s ", sp, ret, name, LEFT_PAREN );
    } else {
        sprintf( line, "%s %s %s %s %s", sp, ret, name, LEFT_PAREN,
                                        DECL_THIS_VAR );
        if( finger ) {
            strcat( line, COMMA_DELIM );
        }
    }
    while( finger ) {
        if( _IsRefType( finger->typ_id ) ) {
            strcat( line, REF_MODIFIER );
        }
        strcat( line, finger->type.name );
        strcat( line, " " );
        strcat( line, finger->name );
        if( finger->next ) {
            strcat( line, COMMA_DELIM );
        }
        finger = finger->next;
    }
    strcat( line, RIGHT_PAREN );
    strcat( line, " " );
    strcat( line, LIBRARY );
    strcat( line, DQUOTE );
    strcat( line, libname );
    strcat( line, DLL_SUFFIX );
    strcat( line, DQUOTE );
    strcat( line, "\n" );

    /* insert statement into our statement linked list */
    rc = insertStatement( locale, line );

    // beware - when pointers get added to the sp_header structure they
    //          must be explicitly copied here or else things will get freed
    //          twice.  This code should be rewritten. DRW
    memcpy( &(rc->data), &(func->data), sizeof( spec ) );
    rc->data.sp.name = MemStrDup( name );
    copyTypeInfo( &rc->data.sp.ret_type, &func->data.sp.ret_type );
    finger = func->data.sp.parm_list;
    SRU.curr.sp.parm_list = NULL;
    SRU.curr.sp.last_parm = NULL;
    tmptype.name = THIS_TYPE;
    tmptype.isref = FALSE;
    AddParm( &tmptype, THIS_VARIABLE, NULL );
    while( finger ) {
        AddParm( &finger->type, finger->name, finger->array );
        finger = finger->next;
    }
    rc->data.sp.parm_list = SRU.curr.sp.parm_list;
    rc->data.sp.last_parm = SRU.curr.sp.last_parm;
    rc->typ = SRU_SUBPROG;
    return( rc );
}
Exemple #6
0
void EndSubProgram( void ) {
/**************************/

/* determine wether to munge the current subprogram to make an
   external call, and then closes this subprogram
*/

    char        *line;
    var_rec     *finger;
    int         size;
    char        *ret;
    char        *name;
    statement   *curr;

    if( !inSubPgm ) {
        return;
    }

    inSubPgm = FALSE;

    assert( SRU.subprog );

    /* determine whether we should munge the  subprogram */
    if( SRU.subprog->data.sp.user_code > 1 ) {
        return;
    } else if( !(SRU.subprog->data.sp.typ_id && SRU.subprog->data.sp.ret_stmt)
                && SRU.subprog->data.sp.user_code ) {
        return;
    }

    /* munge subprogram */

    /* calculate size of statement */
    name = SRU.subprog->data.sp.name;
    size = strlen( name );
    size += sizeof(LEFT_PAREN) + sizeof(RIGHT_PAREN) + sizeof(THIS_VARIABLE);
    finger = SRU.subprog->data.sp.parm_list;
    while( finger ) {
        size += strlen( finger->name ) + sizeof( COMMA_DELIM );
        finger = finger->next;
    }
    if( !SRU.subprog->data.sp.subroutine ) {
        size += sizeof( RETURN_STATEMENT );
    }

    /* allocate memory and create function call */
    line = alloca( size );
    if( !SRU.subprog->data.sp.subroutine ) {
        ret = RETURN_STATEMENT;
    } else {
        ret = "";
    }

    finger = SRU.subprog->data.sp.parm_list;
    if( Options & OPT_GEN_C_CODE ) {
        sprintf( line, "%s%s%s", ret, name, LEFT_PAREN );
    } else {
        sprintf( line, "%s%s%s%s", ret, name, LEFT_PAREN, THIS_VARIABLE );
        if( finger ) {
            strcat( line, COMMA_DELIM );
        }
    }
    while( finger ) {
        strcat( line, finger->name );
        if( finger->next ) {
            strcat( line, COMMA_DELIM );
        }
        finger = finger->next;
    }
    strcat( line, RIGHT_PAREN );
    strcat( line, "\n" );

    /* determine where to place the new call, and place it there */
    if( SRU.subprog->data.sp.typ_id && (SRU.subprog->data.sp.user_code == 1) &&
        SRU.subprog->data.sp.ret_stmt ) {
        curr = SRU.subprog;
        while( curr && ( curr->next != SRU.subprog->data.sp.ret_stmt ) ) {
            curr = curr->next;
        }
        if( curr ) {
            replaceStatement( curr, line );
        } else {
            insertStatement( SRU.subprog, line );
        }
    } else {
        insertStatement( SRU.subprog, line );
    }
}
Exemple #7
0
/*
 * Add calls to register and unregister expressions/arrays with the memory
 * management wrapper.
 */
bool RegisterPointers::addRegUnregCalls()
{
	string msg;

	if(definedInSystemHeader)
	{
		msg = "\t\t\tVariable " + NAME(varSymbol) + " is in system headers";
		WARNING(TOOL, msg);
		return false;
	}

	if(compilerGenerated)
	{
		msg = "\t\t\tVariable " + NAME(varSymbol) + " is compiler-generated";
		WARNING(TOOL, msg);
		return false;
	}

	if(addrUsedInIO)
	{
		msg = "\t\t\tVariable " + NAME(varSymbol) + " has its address taken in "
				+ "a function known to not require registering/unregistering";
		WARNING(TOOL, msg);
		return false;
	}

	//Add register/unregister calls
	SgStatement* prevStmt = NULL;
	SgExprStatement* funcCall = NULL;
	SgType* type = NULL;
	SgExpression* expr = NULL;

	if(isGlobal)
	{
		type = varName->get_type();
		SgName name = varName->get_name();
		int numVals = 1;

		if(isSgPointerType(type))
			return false;	//If its a pointer, it points to a static array, dynamic array, or scalar
							//which has its address taken (all of which have already been registered)

		SgFunctionDeclaration* main = findMain(getScope(varName));
		ROSE_ASSERT(main);
		expr = buildVarRefExp(name, varName->get_scope());
		if(isSgArrayType(type))
			numVals = getArrayElementCount(isSgArrayType(type));
		else
			expr = buildAddressOfOp(expr);

		funcCall = MMCallBuilder::buildRegisterPointerCall(expr, buildUnsignedIntVal(numVals),
				main->get_definition());
		insertStatement(getFirstStatement(main->get_definition()), funcCall);//, false, true);
		funcCall = MMCallBuilder::buildUnregisterPointerCall(expr, main->get_definition());
		instrumentEndOfFunction(main, funcCall);
	}
	else if(expression) //Address-of expressions
	{
		prevStmt = getEnclosingStatement(expression);
		funcCall = MMCallBuilder::buildRegisterPointerCall(expression, buildUnsignedIntVal(1),
				getScope(expression));
		insertStatement(prevStmt, funcCall);
		funcCall = MMCallBuilder::buildUnregisterPointerCall(expression, getScope(expression));
		instrumentEndOfFunction(getEnclosingFunctionDeclaration(prevStmt), funcCall);
	}
	else //Array types
	{
		SgVarRefExp* varRef = buildVarRefExp(varName, getScope(varName));

		int numVals = getArrayElementCount(isSgArrayType(varName->get_type()));
		funcCall = MMCallBuilder::buildRegisterPointerCall(varRef, buildUnsignedIntVal(numVals),
				varName->get_scope());
		insertStatement(varName->get_declaration(), funcCall, false, true);
		varRef = buildVarRefExp(varName, getScope(varName));
		funcCall = MMCallBuilder::buildUnregisterPointerCall(varRef, varName->get_scope());
		instrumentEndOfFunction(getEnclosingFunctionDeclaration(varName), funcCall);
	}

	return true;
}