#include "StringLitTable.h" #include "ClassDecl.h" #include "CodeContext.h" #include "c-types/CtLocal.h" #include "compiler.h" #include "code.h" const char StringLitTable::name[] = "StringLiteral"; const char StringLitTable::tableName[] = "StringLitTable"; const CtLocal *StringLitTable::stringType = 0; StringLitTable::StringLitTable() { if (!stringType) stringType = new CtLocal( StringDecl->cType() ); } size_t StringLitTable::operator[]( const string16 &text ) { const iterator hint = lower_bound( text ); size_t id; if (hint != end() && hint->first == text) id = hint->second; else { id = size(); insert( hint, make_pair( text, id ) ); } return id; } void StringLitTable::declare( ostream &out ) const { out << "static " << *stringType << " *" << name << ";\n"; } void StringLitTable::internAll( CodeContext &context ) const { if (empty()) context << name << " = 0;" << endCline; else { CodeContext tableInit( context ); tableInit << "static " << *stringType << ' ' << tableName << '[' << size() << "];" << endCline << name << " = " << tableName << ';' << endCline; for (const_iterator entry = begin(); entry != end(); ++entry) { const string16 &text = entry->first; const unsigned index = entry->second; CodeContext subcontext( tableInit ); subcontext.declare( "instance", *stringType ); const char *data; if (text.empty()) data = "0"; else { subcontext << "jchar chars[] = "; printEscaped( subcontext, text ); subcontext << ';' << endCline; data = "chars"; } static const CtType &nativeUtils = NativeUtilsDecl->cType(); subcontext << "instance = java_string_build_16(" << text.length() << ", " << data << ");" << endCline << name << '[' << index << "] = " << MANGLE_METH_HEAD(<<, "", string("intern")) << *stringType << MANGLE_METHOD_NAME_END_MARKER << nativeUtils << "(instance);" << endCline; } } }