Store the file name per compilation unit, not per compiled function
authorSimon Hausmann <simon.hausmann@digia.com>
Thu, 15 Aug 2013 07:23:42 +0000 (09:23 +0200)
committerSimon Hausmann <simon.hausmann@digia.com>
Thu, 15 Aug 2013 08:35:47 +0000 (10:35 +0200)
Change-Id: Id699897b241a9ff6495689e5685092d311acb8c2
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
src/qml/compiler/qv4codegen.cpp
src/qml/compiler/qv4codegen_p.h
src/qml/compiler/qv4compileddata_p.h
src/qml/compiler/qv4compiler.cpp
src/qml/compiler/qv4jsir.cpp
src/qml/compiler/qv4jsir_p.h
src/qml/jsruntime/qv4function_p.h

index 969b358..3e1cfc2 100644 (file)
@@ -490,10 +490,11 @@ V4IR::Function *Codegen::operator()(const QString &fileName,
 {
     assert(node);
 
-    _fileName = fileName;
     _module = module;
     _env = 0;
 
+    _module->setFileName(fileName);
+
     ScanFunctions scan(this, sourceCode);
     scan(node);
 
@@ -510,8 +511,8 @@ V4IR::Function *Codegen::operator()(const QString &fileName,
                                   AST::FunctionExpression *ast,
                                   V4IR::Module *module)
 {
-    _fileName = fileName;
     _module = module;
+    _module->setFileName(fileName);
     _env = 0;
 
     ScanFunctions scan(this, sourceCode);
@@ -1808,7 +1809,6 @@ V4IR::Function *Codegen::defineFunction(const QString &name, AST::Node *ast,
 
     enterEnvironment(ast);
     V4IR::Function *function = _module->newFunction(name, _function);
-    function->sourceFile = _fileName;
 
     V4IR::BasicBlock *entryBlock = function->newBasicBlock(groupStartBlock());
     V4IR::BasicBlock *exitBlock = function->newBasicBlock(groupStartBlock(), V4IR::Function::DontInsertBlock);
@@ -2581,7 +2581,7 @@ void Codegen::throwSyntaxErrorOnEvalOrArgumentsInStrictMode(V4IR::Expr *expr, co
 void Codegen::throwSyntaxError(const SourceLocation &loc, const QString &detail)
 {
     QV4::DiagnosticMessage *msg = new QV4::DiagnosticMessage;
-    msg->fileName = _fileName;
+    msg->fileName = _module->fileName;
     msg->offset = loc.begin();
     msg->startLine = loc.startLine;
     msg->startColumn = loc.startColumn;
@@ -2597,7 +2597,7 @@ void Codegen::throwSyntaxError(const SourceLocation &loc, const QString &detail)
 void Codegen::throwReferenceError(const SourceLocation &loc, const QString &detail)
 {
     if (_context)
-        _context->throwReferenceError(QV4::Value::fromString(_context, detail), _fileName, loc.startLine);
+        _context->throwReferenceError(QV4::Value::fromString(_context, detail), _module->fileName, loc.startLine);
     else if (_errorHandler)
         throwSyntaxError(loc, detail);
     else
index 75ca915..847cabb 100644 (file)
@@ -420,7 +420,6 @@ protected:
     void throwReferenceError(const AST::SourceLocation &loc, const QString &detail);
 
 private:
-    QString _fileName;
     Result _expr;
     QString _property;
     UiMember _uiMember;
index 70e9d90..d02946e 100644 (file)
@@ -83,6 +83,7 @@ struct Unit
     uint functionTableSize;
     uint offsetToFunctionTable;
     uint indexOfRootFunction;
+    quint32 sourceFileIndex;
 
     const String *stringAt(int idx) const {
         const uint *offsetTable = reinterpret_cast<const uint*>((reinterpret_cast<const char *>(this)) + offsetToStringTable);
@@ -111,7 +112,6 @@ struct Function
     QV4::Value (*code)(ExecutionContext *, const uchar *);
     quint32 index; // in CompilationUnit's function table
     quint32 nameIndex;
-    quint32 sourceFileIndex;
     qint64 flags;
     quint32 nFormals;
     quint32 formalsOffset;
@@ -246,6 +246,8 @@ struct CompilationUnit
     int refCount;
     Unit *data;
 
+    QString fileName() const { return data->stringAt(data->sourceFileIndex)->qString(); }
+
     QV4::String **runtimeIdentifiers; // Array
 
     QV4::Function *linkToEngine(QV4::ExecutionEngine *engine);
index 61d2d94..07687cc 100644 (file)
@@ -69,9 +69,9 @@ int QV4::Compiler::JSUnitGenerator::getStringId(const QString &string) const
 
 QV4::CompiledData::Unit *QV4::Compiler::JSUnitGenerator::generateUnit()
 {
+    registerString(irModule->fileName);
     foreach (QQmlJS::V4IR::Function *f, irModule->functions) {
         registerString(*f->name);
-        registerString(f->sourceFile);
         for (int i = 0; i < f->formals.size(); ++i)
             registerString(*f->formals.at(i));
         for (int i = 0; i < f->locals.size(); ++i)
@@ -98,6 +98,7 @@ QV4::CompiledData::Unit *QV4::Compiler::JSUnitGenerator::generateUnit()
     unit->offsetToStringTable = sizeof(QV4::CompiledData::Unit);
     unit->functionTableSize = irModule->functions.size();
     unit->offsetToFunctionTable = unit->offsetToStringTable + unit->stringTableSize * sizeof(uint);
+    unit->sourceFileIndex = getStringId(irModule->fileName);
 
     // write strings and string table
     uint *stringTable = (uint *)(data + unit->offsetToStringTable);
@@ -141,7 +142,6 @@ void QV4::Compiler::JSUnitGenerator::writeFunction(char *f, int index, QQmlJS::V
     QV4::CompiledData::Function *function = (QV4::CompiledData::Function *)f;
     function->index = index;
     function->nameIndex = getStringId(*irFunction->name);
-    function->sourceFileIndex = getStringId(irFunction->sourceFile);
     function->flags = 0;
     if (irFunction->hasDirectEval)
         function->flags |= CompiledData::Function::HasDirectEval;
index aea696d..51aa2d0 100644 (file)
@@ -610,6 +610,15 @@ Module::~Module()
     }
 }
 
+void Module::setFileName(const QString &name)
+{
+    if (fileName.isEmpty())
+        fileName = name;
+    else {
+        Q_ASSERT(fileName == name);
+    }
+}
+
 Function::~Function()
 {
     // destroy the Stmt::Data blocks manually, because memory pool cleanup won't
index d322ea8..a53b69d 100644 (file)
@@ -669,11 +669,14 @@ struct Q_QML_EXPORT Module {
     MemoryPool pool;
     QVector<Function *> functions;
     Function *rootFunction;
+    QString fileName;
 
     Function *newFunction(const QString &name, Function *outer);
 
     Module() : rootFunction(0) {}
     ~Module();
+
+    void setFileName(const QString &name);
 };
 
 struct Function {
@@ -689,8 +692,6 @@ struct Function {
     QVector<Function *> nestedFunctions;
     Function *outer;
 
-    QString sourceFile;
-
     int insideWithOrCatch;
 
     uint hasDirectEval: 1;
index 4484331..e2274a2 100644 (file)
@@ -130,7 +130,7 @@ struct Function {
         nestedFunctions.append(f);
     }
 
-    inline QString sourceFile() const { return compilationUnit->data->stringAt(compiledFunction->sourceFileIndex)->qString(); }
+    inline QString sourceFile() const { return compilationUnit->fileName(); }
 
     inline bool usesArgumentsObject() const { return compiledFunction->flags & CompiledData::Function::UsesArgumentsObject; }
     inline bool isStrict() const { return compiledFunction->flags & CompiledData::Function::IsStrict; }