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