Exemple #1
0
Util::JsonArray*
Extern::addExternAttributes(const IR::Declaration_Instance* di,
                            const IR::ExternBlock* block) {
    auto attributes = new Util::JsonArray();
    auto paramIt = block->getConstructorParameters()->parameters.begin();
    for (auto arg : *di->arguments) {
        auto name = arg->toString();
        if (arg->is<IR::Constant>()) {
            auto cVal = arg->to<IR::Constant>();
            if (arg->type->is<IR::Type_Bits>()) {
                json->add_extern_attribute(name, "hexstr",
                                           stringRepr(cVal->value), attributes);
            } else {
                BUG("%1%: unhandled constant constructor param", cVal->toString());
            }
        } else if (arg->is<IR::Declaration_ID>()) {
            auto declId = arg->to<IR::Declaration_ID>();
            json->add_extern_attribute(name, "string", declId->toString(), attributes);
        } else if (arg->type->is<IR::Type_Enum>()) {
            json->add_extern_attribute(name, "string", arg->toString(), attributes);
        } else {
            BUG("%1%: unknown constructor param type", arg->type);
        }
        ++paramIt;
    }
    return attributes;
}
Exemple #2
0
string StringProperty::serialize(string indent) const
{
    return stringRepr(value);
}
Exemple #3
0
string Serializable::serializeElements(string indent) const
{
    string r;
    
    if( type != "" )
        r += TAB + indent + "\"type\" : " + stringRepr(type) + ",\n";
    if( name != "" )
        r += TAB + indent + "\"name\" : " + stringRepr(name) + ",\n";
    
    int size = properties.size();
    
    int i = 0;
    for(vector<Property>::const_iterator itr = properties.begin();
        itr!=properties.end();
        itr++ )
    {
        void* ptr = (char*)(this) + itr->offset;
        
        switch( itr->type )
        {
            case kString:
                r += TAB + indent + "\"" + itr->name + "\" : " + 
                    stringRepr(*(reinterpret_cast<string*>(ptr)));
            break;
            
            case kDouble:
                r += TAB + indent + "\"" + itr->name + "\" : " +
                    floatToString(*(reinterpret_cast<double*>(ptr)));
            break;
            
            case kInt:
                r += TAB + indent + "\"" + itr->name + "\" : " +
                    intToString(*(reinterpret_cast<int*>(ptr)));
            break;
            
            case kVectorDouble:
                {
                    r += TAB + indent + "\"" + itr->name + "\" : [\n";
                    vector<double>* vptr = (vector<double>*)ptr;
                    
                    vector<double>::iterator itr = vptr->begin();
                    if( itr != vptr->end() )
                    while(true)
                    {
                        r += TAB + TAB + indent + floatToString(*itr);
                        itr++;
                        if( itr==vptr->end() )
                        {
                            r += "\n";
                            break;
                        }
                        r += ",\n";
                    }
                    
                    r += TAB + indent + "]";
                }
            break;
            
            case kVectorInt:
                {
                    r += TAB + indent + "\"" + itr->name + "\" : [\n";
                    vector<int>* vptr = (vector<int>*)ptr;
                    
                    vector<int>::iterator itr = vptr->begin();
                    if( itr != vptr->end() )
                    while(true)
                    {
                        r += TAB + TAB + indent + intToString(*itr);
                        itr++;
                        if( itr==vptr->end() )
                        {
                            r += "\n";
                            break;
                        }
                        r += ",\n";
                    }
                    
                    r += TAB + indent + "]";
                }
            break;
            
            case kVectorString:
                {
                    r += TAB + indent + "\"" + itr->name + "\" : [\n";
                    vector<string>* vptr = (vector<string>*)ptr;
                    
                    vector<string>::iterator itr = vptr->begin();
                    if( itr != vptr->end() )
                    while(true)
                    {
                        r += TAB + TAB + indent + stringRepr(*itr);
                        itr++;
                        if( itr==vptr->end() )
                        {
                            r += "\n";
                            break;
                        }
                        r += ",\n";
                    }
                                        
                    r += TAB + indent + "]";
                }
            break;
            
            case kObject:
                r += TAB + indent + "\"" + itr->name + "\" : " +
                    (reinterpret_cast<Serializable*>(ptr))->serialize(indent + TAB);
            break;
        }
        
        if( i!=(size-1) )
            r += ",";
        r += "\n";
        
        i++;
    }
    
    return r;
}