flush
[m6w6/ext-psi] / src / types / let_val.h
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #ifndef PSI_TYPES_LET_VAL_H
27 #define PSI_TYPES_LET_VAL_H
28
29 #include "decl_var.h"
30 #include "num_exp.h"
31 #include "let_calloc.h"
32 #include "let_callback.h"
33 #include "let_func.h"
34
35 enum let_val_kind {
36 PSI_LET_NULL,
37 PSI_LET_NUMEXP,
38 PSI_LET_CALLOC,
39 PSI_LET_CALLBACK,
40 PSI_LET_FUNC,
41 PSI_LET_TMP,
42 };
43
44 #define PSI_LET_REFERENCE 0x1;
45
46 typedef struct let_val {
47 enum let_val_kind kind;
48 decl_var *var;
49 union {
50 num_exp *num;
51 let_calloc *alloc;
52 let_callback *callback;
53 let_func *func;
54 decl_var *var;
55 } data;
56 unsigned is_reference:1;
57 } let_val;
58
59
60 let_val* init_let_val(enum let_val_kind kind, void* data);
61 let_val* init_let_val_ex(decl_var *var, enum let_val_kind kind, void* data);
62 void free_let_val(let_val* let);
63 void dump_let_val(int fd, let_val *val, unsigned level, int last);
64
65 struct psi_data;
66 struct impl;
67
68 int validate_let_val(struct psi_data *data, let_val *val, decl_var *let_var, struct impl *impl);
69
70 static inline let_func *locate_let_val_func(let_val *val) {
71 if (val) {
72 switch (val->kind) {
73 case PSI_LET_CALLBACK:
74 return val->data.callback->func;
75 case PSI_LET_FUNC:
76 return val->data.func;
77 default:
78 break;
79 }
80 }
81
82 return NULL;
83 }
84
85 static inline decl_arg *locate_let_val_inner_ref(let_val *val) {
86 let_func *fn = locate_let_val_func(val);
87 return fn ? fn->ref: NULL;
88 }
89
90 static inline impl_var *locate_let_val_impl_var(let_val *val) {
91 let_func *fn = locate_let_val_func(val);
92 return fn ? fn->var : NULL;
93 }
94
95 static inline const char *locate_let_val_varname(let_val *val) {
96 impl_var *var;
97
98 if (val->var) {
99 return val->var->name;
100 }
101
102 var = locate_let_val_impl_var(val);
103
104 if (var) {
105 return &var->name[1];
106 }
107 return NULL;
108 }
109 #endif