/*
 * Copyright (C) 2012 David Anderson
 *
 * This file is part of SourcePawn.
 *
 * SourcePawn is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 * 
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
 */
#ifndef _include_jitcraft_compile_context_h_
#define _include_jitcraft_compile_context_h_

#include <stdarg.h>
#include <string>
#include "../Messages.h"
#include "../PoolAllocator.h"
#include "../Handles.h"
#include "../Modules.h"
#include "../Packages.h"

namespace ke {

class Zone;
class ParseTree;
class String;
class ImportScope;
class GlobalScope;
struct SourcePosition;
struct TranslationUnit;
class ImportStatement;

struct CompileError
{
    CompileError()
      : message(NULL)
    {
    }
    CompileError(unsigned line, unsigned col, const char *message)
      : line(line),
        col(col),
        message(message)
    {
    }

    unsigned line;
    unsigned col;
    const char *message;
};

struct TranslationUnit : public PoolObject
{
    TranslationUnit(Handle<Module> module)
      : tree_(NULL),
        importScope_(NULL),
        globalScope_(NULL),
        module_(module),
        enoughMemory_(true),
        dependencyFailed_(false)
    {
    }
    
    void destroy();

    ParseTree *tree_;
    PoolList<TranslationUnit *> dependents_;
    ImportScope *importScope_;
    GlobalScope *globalScope_;
    ScopedRoot<Module> module_;

    ParseTree *tree() const {
        return tree_;
    }
    PoolList<TranslationUnit *> &dependents() {
        return dependents_;
    }
    bool failed() const {
        return errors_.length() ||
               !enoughMemory_ ||
               dependencyFailed_;
    }
    void setImportScope(ImportScope *scope) {
        assert(!importScope_);
        importScope_ = scope;
    }
    void setGlobalScope(GlobalScope *scope) {
        assert(!globalScope_);
        globalScope_ = scope;
    }
    ImportScope *importScope() const {
        return importScope_;
    }
    GlobalScope *globalScope() const {
        return globalScope_;
    }
    Handle<Module> module() {
        return module_;
    }

    void setDependencyFailed() {
        dependencyFailed_ = true;
    }
    void setOutOfMemory() {
        enoughMemory_ = false;
    }
    void appendError(const char *report, unsigned line, unsigned col) {
        enoughMemory_ |= errors_.append(CompileError(line, col, report));
    }
    const Vector<CompileError> *errors() {
        return &errors_;
    }

  private:
    bool enoughMemory_;
    bool dependencyFailed_;
    Vector<CompileError> errors_;
};

class CompileContext
{
    Zone *zone_;
    Vector<TranslationUnit *> units_;
    Vector<TranslationUnit *> unparsed_;
    TranslationUnit *active_;

  private:
    char *loadSource(Handle<Module> module, size_t *length);
    bool parse(TranslationUnit *unit);
    bool evaluateImports(TranslationUnit *unit);
    bool propagateFailure();
    bool add(Handle<Module> module);

  public:
    CompileContext(Zone *zone);
    ~CompileContext();

    bool initialize(Handle<Package> package, const char *name);
    bool compile();
    void enter(TranslationUnit *unit);
    void leave();

    void reportErrorVa(const SourcePosition &pos, Message msg, va_list ap);
    void reportError(const SourcePosition &pos, Message msg, ...);
};

class AutoEnterCompileContext
{
    CompileContext &cc_;

  public:
    AutoEnterCompileContext(CompileContext &cc, TranslationUnit *unit)
      : cc_(cc)
    {
      cc_.enter(unit);
    }
    ~AutoEnterCompileContext() {
      cc_.leave();
    }
};

} // namespace ke

#endif // _include_jitcraft_compile_context_h_
