objmodel.h 5.36 KB
Newer Older
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1
// Copyright (c) 2014 Dropbox, Inc.
Kevin Modzelewski's avatar
Kevin Modzelewski committed
2
//
Kevin Modzelewski's avatar
Kevin Modzelewski committed
3 4 5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Kevin Modzelewski's avatar
Kevin Modzelewski committed
6
//
Kevin Modzelewski's avatar
Kevin Modzelewski committed
7
//    http://www.apache.org/licenses/LICENSE-2.0
Kevin Modzelewski's avatar
Kevin Modzelewski committed
8
//
Kevin Modzelewski's avatar
Kevin Modzelewski committed
9 10 11 12 13 14 15 16 17 18
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef PYSTON_RUNTIME_OBJMODEL_H
#define PYSTON_RUNTIME_OBJMODEL_H

#include <stdint.h>
19
#include <string>
Kevin Modzelewski's avatar
Kevin Modzelewski committed
20 21 22 23 24 25 26 27 28 29

#include "core/types.h"

namespace pyston {

class Box;
class BoxedClass;
class BoxedInt;
class BoxedList;
class BoxedString;
30
class BoxedGenerator;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
31

32 33
// user-level raise functions that implement python-level semantics
extern "C" void raise0() __attribute__((__noreturn__));
34
extern "C" void raise3(Box*, Box*, Box*) __attribute__((__noreturn__));
35 36
void raiseExc(Box* exc_obj) __attribute__((__noreturn__));

37
// helper function for raising from the runtime:
38 39
void raiseExcHelper(BoxedClass*, const char* fmt, ...) __attribute__((__noreturn__));

40 41
BoxedModule* getCurrentModule();

Kevin Modzelewski's avatar
Kevin Modzelewski committed
42 43 44
extern "C" const std::string* getNameOfClass(BoxedClass* cls);

// TODO sort this
45
extern "C" bool softspace(Box* b, bool newval);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
46 47 48
extern "C" void my_assert(bool b);
extern "C" Box* getattr(Box* obj, const char* attr);
extern "C" void setattr(Box* obj, const char* attr, Box* attr_val);
49
extern "C" void delattr(Box* obj, const char* attr);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
50
extern "C" bool nonzero(Box* obj);
51 52 53
extern "C" Box* runtimeCall(Box*, ArgPassSpec, Box*, Box*, Box*, Box**, const std::vector<const std::string*>*);
extern "C" Box* callattr(Box*, std::string*, bool, ArgPassSpec, Box*, Box*, Box*, Box**,
                         const std::vector<const std::string*>*);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
54
extern "C" BoxedString* str(Box* obj);
55
extern "C" BoxedString* repr(Box* obj);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
56
extern "C" BoxedString* reprOrNull(Box* obj); // similar to repr, but returns NULL on exception
Kevin Modzelewski's avatar
Kevin Modzelewski committed
57
extern "C" BoxedString* strOrNull(Box* obj);  // similar to str, but returns NULL on exception
58
extern "C" bool isinstance(Box* obj, Box* cls, int64_t flags);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
59
extern "C" BoxedInt* hash(Box* obj);
60
// extern "C" Box* abs_(Box* obj);
61
Box* open(Box* arg1, Box* arg2);
62
// extern "C" Box* chr(Box* arg);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
63 64 65
extern "C" Box* compare(Box*, Box*, int);
extern "C" BoxedInt* len(Box* obj);
extern "C" void print(Box* obj);
66
// extern "C" Box* trap();
Kevin Modzelewski's avatar
Kevin Modzelewski committed
67 68
extern "C" i64 unboxedLen(Box* obj);
extern "C" Box* binop(Box* lhs, Box* rhs, int op_type);
69
extern "C" Box* augbinop(Box* lhs, Box* rhs, int op_type);
70
extern "C" Box* getGlobal(BoxedModule* m, std::string* name);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
71
extern "C" void delGlobal(BoxedModule* m, std::string* name);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
72 73
extern "C" Box* getitem(Box* value, Box* slice);
extern "C" void setitem(Box* target, Box* slice, Box* value);
74
extern "C" void delitem(Box* target, Box* slice);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
75 76
extern "C" Box* getclsattr(Box* obj, const char* attr);
extern "C" Box* unaryop(Box* operand, int op_type);
77
extern "C" Box* import(const std::string* name);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
78 79
extern "C" Box* importFrom(Box* obj, const std::string* attr);
extern "C" void importStar(Box* from_module, BoxedModule* to_module);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
80
extern "C" void checkUnpackingLength(i64 expected, i64 given);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
81
extern "C" void assertNameDefined(bool b, const char* name, BoxedClass* exc_cls, bool local_var_msg);
82
extern "C" void assertFail(BoxedModule* inModule, Box* msg);
83
extern "C" bool isSubclass(BoxedClass* child, BoxedClass* parent);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
84
extern "C" BoxedClosure* createClosure(BoxedClosure* parent_closure);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
85

Travis Hance's avatar
Travis Hance committed
86 87
class BinopRewriteArgs;
extern "C" Box* binopInternal(Box* lhs, Box* rhs, int op_type, bool inplace, BinopRewriteArgs* rewrite_args);
88

Travis Hance's avatar
Travis Hance committed
89 90
class CallRewriteArgs;
Box* typeCallInternal(BoxedFunction* f, CallRewriteArgs* rewrite_args, ArgPassSpec argspec, Box* arg1, Box* arg2,
91 92
                      Box* arg3, Box** args, const std::vector<const std::string*>* keyword_names);

93 94 95 96 97
enum LookupScope {
    CLASS_ONLY = 1,
    INST_ONLY = 2,
    CLASS_OR_INST = 3,
};
Travis Hance's avatar
Travis Hance committed
98
extern "C" Box* callattrInternal(Box* obj, const std::string* attr, LookupScope, CallRewriteArgs* rewrite_args,
99 100
                                 ArgPassSpec argspec, Box* arg1, Box* arg2, Box* arg3, Box** args,
                                 const std::vector<const std::string*>* keyword_names);
101
extern "C" void delattr_internal(Box* obj, const std::string& attr, bool allow_custom,
Travis Hance's avatar
Travis Hance committed
102 103 104
                                 DelattrRewriteArgs* rewrite_args);
struct CompareRewriteArgs;
Box* compareInternal(Box* lhs, Box* rhs, int op_type, CompareRewriteArgs* rewrite_args);
105
Box* getattr_internal(Box* obj, const std::string& attr, bool check_cls, bool allow_custom,
Travis Hance's avatar
Travis Hance committed
106
                      GetattrRewriteArgs* rewrite_args);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
107

Travis Hance's avatar
Travis Hance committed
108
Box* typeLookup(BoxedClass* cls, const std::string& attr, GetattrRewriteArgs* rewrite_args);
109

Kevin Modzelewski's avatar
Kevin Modzelewski committed
110 111 112 113 114 115
extern "C" void raiseAttributeErrorStr(const char* typeName, const char* attr) __attribute__((__noreturn__));
extern "C" void raiseAttributeError(Box* obj, const char* attr) __attribute__((__noreturn__));
extern "C" void raiseNotIterableError(const char* typeName) __attribute__((__noreturn__));

Box* typeCall(Box*, BoxedList*);
Box* typeNew(Box*, Box*);
116
bool isUserDefined(BoxedClass* cls);
117

Travis Hance's avatar
Travis Hance committed
118
Box* callCLFunc(CLFunction* f, CallRewriteArgs* rewrite_args, int num_output_args, BoxedClosure* closure,
119
                BoxedGenerator* generator, Box* oarg1, Box* oarg2, Box* oarg3, Box** oargs);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
120 121
}
#endif