src/Functions.h
author David Anderson <dvander@alliedmods.net>
Mon Dec 24 02:56:02 2012 -0800 (2012-12-24)
changeset 235 43287eef2fff
parent 195 c1ba166f3fc4
child 263 ba85a47ee414
permissions -rw-r--r--
Move PrimitiveType_String to a dedicated string type.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of SourcePawn.
     5  *
     6  * SourcePawn 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  * SourcePawn 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  * SourcePawn. 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 "Types.h"
    24 #include "Opcodes.h"
    25 
    26 namespace ke {
    27 
    28 class Code;
    29 class Module;
    30 class String;
    31 
    32 class Function : public Object
    33 {
    34     Barriered<Code> code_;
    35     Barriered<FunctionType> type_;
    36     Barriered<String> name_;
    37     SourcePosition pos_;
    38 
    39  public:
    40     static Function *New(Zone *zone, Handle<Code> code, Handle<FunctionType> type, Handle<String> name,
    41                          const SourcePosition &pos);
    42 
    43     Code *code() const {
    44         return code_;
    45     }
    46     FunctionType *type() const {
    47         return type_;
    48     }
    49     String *name() const {
    50         return name_;
    51     }
    52     const SourcePosition &pos() {
    53         return pos_;
    54     }
    55     unsigned nargs() {
    56         return type()->parameters()->length();
    57     }
    58     Type *argType(size_t index) {
    59         return type()->parameterAt(index);
    60     }
    61 
    62     // Begin GC Descriptor.
    63     static inline size_t offsetOfCode() {
    64         return OFFSETOF(Function, code_);
    65     }
    66     static inline size_t offsetOfType() {
    67         return OFFSETOF(Function, type_);
    68     }
    69     static inline size_t offsetOfName() {
    70         return OFFSETOF(Function, name_);
    71     }
    72     // End GC Descriptor.
    73 
    74     static inline Function *cast(Object *obj) {
    75         assert(!obj || obj->is(MapKind_Function));
    76         return reinterpret_cast<Function *>(obj);
    77     }
    78 };
    79 
    80 // A Native is a function with a very specialized calling convention, intended
    81 // for earlier ISourcePawnEngine API compatibility.
    82 class Native : public Object
    83 {
    84     Barriered<FunctionType> type_;
    85 
    86     // Index into the Zone's native table.
    87     size_t index_;
    88 
    89   public:
    90     static Native *New(Zone *zone, Handle<FunctionType> type, size_t index);
    91 
    92     FunctionType *type() {
    93         return type_;
    94     }
    95     size_t index() const {
    96         return index_;
    97     }
    98 
    99     // Begin GC Descriptor.
   100     static inline size_t offsetOfType() {
   101         return OFFSETOF(Native, type_);
   102     }
   103     // End GC Descriptor.
   104 
   105     static inline Native *cast(Object *obj) {
   106         assert(!obj || obj->is(MapKind_Native));
   107         return reinterpret_cast<Native *>(obj);
   108     }
   109 };
   110 
   111 }
   112 
   113 #endif // _include_jitcraft_function_h_