src/Functions.h
author Asher Baker <asherkin@gmail.com>
Mon Jun 10 01:04:43 2013 +0100 (2013-06-10)
changeset 263 ba85a47ee414
parent 195 c1ba166f3fc4
permissions -rw-r--r--
Normalise line endings.
asherkin@263
     1
/*
asherkin@263
     2
 * Copyright (C) 2012 David Anderson
asherkin@263
     3
 *
asherkin@263
     4
 * This file is part of SourcePawn.
asherkin@263
     5
 *
asherkin@263
     6
 * SourcePawn is free software: you can redistribute it and/or modify it under
asherkin@263
     7
 * the terms of the GNU General Public License as published by the Free
asherkin@263
     8
 * Software Foundation, either version 3 of the License, or (at your option)
asherkin@263
     9
 * any later version.
asherkin@263
    10
 * 
asherkin@263
    11
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
asherkin@263
    12
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
asherkin@263
    13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
asherkin@263
    14
 *
asherkin@263
    15
 * You should have received a copy of the GNU General Public License along with
asherkin@263
    16
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
asherkin@263
    17
 */
asherkin@263
    18
#ifndef _include_jitcraft_function_h_
asherkin@263
    19
#define _include_jitcraft_function_h_
asherkin@263
    20
asherkin@263
    21
#include "Heap.h"
asherkin@263
    22
#include "Barriers.h"
asherkin@263
    23
#include "Types.h"
asherkin@263
    24
#include "Opcodes.h"
asherkin@263
    25
asherkin@263
    26
namespace ke {
asherkin@263
    27
asherkin@263
    28
class Code;
asherkin@263
    29
class Module;
asherkin@263
    30
class String;
asherkin@263
    31
asherkin@263
    32
class Function : public Object
asherkin@263
    33
{
asherkin@263
    34
    Barriered<Code> code_;
asherkin@263
    35
    Barriered<FunctionType> type_;
asherkin@263
    36
    Barriered<String> name_;
asherkin@263
    37
    SourcePosition pos_;
asherkin@263
    38
asherkin@263
    39
 public:
asherkin@263
    40
    static Function *New(Zone *zone, Handle<Code> code, Handle<FunctionType> type, Handle<String> name,
asherkin@263
    41
                         const SourcePosition &pos);
asherkin@263
    42
asherkin@263
    43
    Code *code() const {
asherkin@263
    44
        return code_;
asherkin@263
    45
    }
asherkin@263
    46
    FunctionType *type() const {
asherkin@263
    47
        return type_;
asherkin@263
    48
    }
asherkin@263
    49
    String *name() const {
asherkin@263
    50
        return name_;
asherkin@263
    51
    }
asherkin@263
    52
    const SourcePosition &pos() {
asherkin@263
    53
        return pos_;
asherkin@263
    54
    }
asherkin@263
    55
    unsigned nargs() {
asherkin@263
    56
        return type()->parameters()->length();
asherkin@263
    57
    }
asherkin@263
    58
    Type *argType(size_t index) {
asherkin@263
    59
        return type()->parameterAt(index);
asherkin@263
    60
    }
asherkin@263
    61
asherkin@263
    62
    // Begin GC Descriptor.
asherkin@263
    63
    static inline size_t offsetOfCode() {
asherkin@263
    64
        return OFFSETOF(Function, code_);
asherkin@263
    65
    }
asherkin@263
    66
    static inline size_t offsetOfType() {
asherkin@263
    67
        return OFFSETOF(Function, type_);
asherkin@263
    68
    }
asherkin@263
    69
    static inline size_t offsetOfName() {
asherkin@263
    70
        return OFFSETOF(Function, name_);
asherkin@263
    71
    }
asherkin@263
    72
    // End GC Descriptor.
asherkin@263
    73
asherkin@263
    74
    static inline Function *cast(Object *obj) {
asherkin@263
    75
        assert(!obj || obj->is(MapKind_Function));
asherkin@263
    76
        return reinterpret_cast<Function *>(obj);
asherkin@263
    77
    }
asherkin@263
    78
};
asherkin@263
    79
asherkin@263
    80
// A Native is a function with a very specialized calling convention, intended
asherkin@263
    81
// for earlier ISourcePawnEngine API compatibility.
asherkin@263
    82
class Native : public Object
asherkin@263
    83
{
asherkin@263
    84
    Barriered<FunctionType> type_;
asherkin@263
    85
asherkin@263
    86
    // Index into the Zone's native table.
asherkin@263
    87
    size_t index_;
asherkin@263
    88
asherkin@263
    89
  public:
asherkin@263
    90
    static Native *New(Zone *zone, Handle<FunctionType> type, size_t index);
asherkin@263
    91
asherkin@263
    92
    FunctionType *type() {
asherkin@263
    93
        return type_;
asherkin@263
    94
    }
asherkin@263
    95
    size_t index() const {
asherkin@263
    96
        return index_;
asherkin@263
    97
    }
asherkin@263
    98
asherkin@263
    99
    // Begin GC Descriptor.
asherkin@263
   100
    static inline size_t offsetOfType() {
asherkin@263
   101
        return OFFSETOF(Native, type_);
asherkin@263
   102
    }
asherkin@263
   103
    // End GC Descriptor.
asherkin@263
   104
asherkin@263
   105
    static inline Native *cast(Object *obj) {
asherkin@263
   106
        assert(!obj || obj->is(MapKind_Native));
asherkin@263
   107
        return reinterpret_cast<Native *>(obj);
asherkin@263
   108
    }
asherkin@263
   109
};
asherkin@263
   110
asherkin@263
   111
}
asherkin@263
   112
asherkin@263
   113
#endif // _include_jitcraft_function_h_