Repository navigation
Expand file tree
/
Copy pathfunction.cpp
More file actions
5474 lines (4922 loc) · 170 KB
/
Copy pathfunction.cpp
File metadata and controls
5474 lines (4922 loc) · 170 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016-2022 Rene Rivera
* Copyright 2011 Steven Watanabe
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE.txt or copy at
* https://www.bfgroup.xyz/b2/LICENSE.txt)
*/
#include "jam.h"
#include "function.h"
#include "class.h"
#include "compile.h"
#include "constants.h"
#include "debugger.h"
#include "execcmd.h"
#include "filesys.h"
#include "frames.h"
#include "hash.h"
#include "lists.h"
#include "mem.h"
#include "outerr.h"
#include "output.h"
#include "pathsys.h"
#include "rules.h"
#include "search.h"
#include "variable.h"
#include "output.h"
#include "parse.h"
#include "startup.h"
#include "jam_strings.h"
#include "mod_sysinfo.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include <array>
#if defined(B2_DEBUG) && B2_DEBUG
// #define B2_FUNCTION_STACK_VALIDATE 1
#define B2_FUNCTION_STACK_VALIDATE 0
#else
#define B2_FUNCTION_STACK_VALIDATE 0
#endif
/*
#define FUNCTION_DEBUG_PROFILE
*/
#ifndef FUNCTION_DEBUG_PROFILE
#undef PROFILE_ENTER_LOCAL
#define PROFILE_ENTER_LOCAL(x) while (false)
#undef PROFILE_EXIT_LOCAL
#define PROFILE_EXIT_LOCAL(x)
#endif
int32_t glob( char const * s, char const * c );
#define INSTR_PUSH_EMPTY 0
#define INSTR_PUSH_CONSTANT 1
#define INSTR_PUSH_ARG 2
#define INSTR_PUSH_VAR 3
#define INSTR_PUSH_VAR_FIXED 57
#define INSTR_PUSH_GROUP 4
#define INSTR_PUSH_RESULT 5
#define INSTR_PUSH_APPEND 6
#define INSTR_SWAP 7
#define INSTR_JUMP_EMPTY 8
#define INSTR_JUMP_NOT_EMPTY 9
#define INSTR_JUMP 10
#define INSTR_JUMP_LT 11
#define INSTR_JUMP_LE 12
#define INSTR_JUMP_GT 13
#define INSTR_JUMP_GE 14
#define INSTR_JUMP_EQ 15
#define INSTR_JUMP_NE 16
#define INSTR_JUMP_IN 17
#define INSTR_JUMP_NOT_IN 18
#define INSTR_JUMP_NOT_GLOB 19
#define INSTR_FOR_INIT 56
#define INSTR_FOR_LOOP 20
#define INSTR_SET_RESULT 21
#define INSTR_RETURN 22
#define INSTR_POP 23
#define INSTR_PUSH_LOCAL 24
#define INSTR_POP_LOCAL 25
#define INSTR_SET 26
#define INSTR_APPEND 27
#define INSTR_DEFAULT 28
#define INSTR_PUSH_LOCAL_FIXED 58
#define INSTR_POP_LOCAL_FIXED 59
#define INSTR_SET_FIXED 60
#define INSTR_APPEND_FIXED 61
#define INSTR_DEFAULT_FIXED 62
#define INSTR_PUSH_LOCAL_GROUP 29
#define INSTR_POP_LOCAL_GROUP 30
#define INSTR_SET_GROUP 31
#define INSTR_APPEND_GROUP 32
#define INSTR_DEFAULT_GROUP 33
#define INSTR_PUSH_ON 34
#define INSTR_POP_ON 35
#define INSTR_SET_ON 36
#define INSTR_APPEND_ON 37
#define INSTR_DEFAULT_ON 38
#define INSTR_GET_ON 65
#define INSTR_CALL_RULE 39
#define INSTR_CALL_MEMBER_RULE 66
#define INSTR_APPLY_MODIFIERS 40
#define INSTR_APPLY_INDEX 41
#define INSTR_APPLY_INDEX_MODIFIERS 42
#define INSTR_APPLY_MODIFIERS_GROUP 43
#define INSTR_APPLY_INDEX_GROUP 44
#define INSTR_APPLY_INDEX_MODIFIERS_GROUP 45
#define INSTR_COMBINE_STRINGS 46
#define INSTR_GET_GRIST 64
#define INSTR_INCLUDE 47
#define INSTR_RULE 48
#define INSTR_ACTIONS 49
#define INSTR_PUSH_MODULE 50
#define INSTR_POP_MODULE 51
#define INSTR_CLASS 52
#define INSTR_BIND_MODULE_VARIABLES 63
#define INSTR_APPEND_STRINGS 53
#define INSTR_WRITE_FILE 54
#define INSTR_OUTPUT_STRINGS 55
#define INSTR_DEBUG_LINE 67
#define INSTR_FOR_POP 70
typedef struct instruction
{
uint32_t op_code;
int32_t arg;
} instruction;
typedef struct _subfunction
{
OBJECT * name;
FUNCTION * code;
int32_t local;
} SUBFUNCTION;
typedef struct _subaction
{
OBJECT * name;
FUNCTION * command;
int32_t flags;
} SUBACTION;
#define FUNCTION_BUILTIN 0
#define FUNCTION_JAM 1
struct argument
{
int32_t flags;
#define ARG_ONE 0
#define ARG_OPTIONAL 1
#define ARG_PLUS 2
#define ARG_STAR 3
#define ARG_VARIADIC 4
OBJECT * type_name;
OBJECT * arg_name;
int32_t index;
};
struct arg_list
{
int32_t size;
struct argument * args;
};
struct _function
{
int32_t type;
int32_t reference_count;
OBJECT * rulename;
struct arg_list * formal_arguments;
int32_t num_formal_arguments;
};
typedef struct _builtin_function
{
FUNCTION base;
function_builtin_t func;
int32_t flags;
} BUILTIN_FUNCTION;
typedef struct _jam_function
{
FUNCTION base;
int32_t code_size;
instruction * code;
int32_t num_constants;
OBJECT * * constants;
int32_t num_subfunctions;
SUBFUNCTION * functions;
int32_t num_subactions;
SUBACTION * actions;
FUNCTION * generic;
OBJECT * file;
int32_t line;
} JAM_FUNCTION;
namespace
{
typedef struct _stack STACK;
typedef STACK* stack_ptr;
template <typename T>
using remove_cref_t
= typename std::remove_const<
typename std::remove_reference<
typename std::remove_const<T>::type
>::type
>::type;
#if 1
template <typename...A>
struct select_last_impl {};
template <typename T>
struct select_last_impl<T> { using type = T; };
template <typename T, typename... A>
struct select_last_impl<T, A...> {
using type = typename select_last_impl<A...>::type; };
template <typename... A>
using select_last_t = typename select_last_impl<A...>::type;
#else
template<typename...>
struct type_list;
template<bool T, typename L>
struct select_last_impl;
template<typename... A>
struct select_last_impl<true, type_list<A...>>
{
template<typename T>
using type = T;
};
template<typename... A>
struct select_last_impl<false, type_list<A...>>
{
template<typename T, typename... R>
using type = typename select_last_impl<
(sizeof...(R) == 1), type_list<A...> >
::template type<R...>;
};
template<typename... A>
using select_last_t =
typename select_last_impl<(sizeof...(A) == 1), type_list<A...> >
::template type<A...>;
#endif
struct _stack
{
public:
_stack()
{
// The LIFO stack goes from start end (high/right) to (low/left).
// Byte size of the function stack.
int32_t const size = 1 << 21;
// The beginning of the stack memory.
start = BJAM_MALLOC( size );
// The current past-end/tail of the stack memory.
end = (char *)start + size;
// The next available stack location.
data = end;
}
#if defined(__clang__) && defined(__has_feature)
#if __has_feature(address_sanitizer)
// This function calls not properly type-erased callbacks
__attribute__((no_sanitize("undefined")))
#endif
#endif
void done()
{
if ( cleanups_size > cleanups_t::size_type(0) )
{
// Call any left over cleanup functions to deallocate items that
// reference resources.
while ( cleanups_size > 0 )
{
cleanups_size -= 1;
cleanups[cleanups_size].clean( this, nullptr );
}
}
BJAM_FREE( start );
start = end = data = nullptr;
}
// Get reference to the top i-th T item, optionally as a U. I.e. it skips
// the i-th T items returning it as U&.
template <typename T, typename U = T>
remove_cref_t<U> & top(int i = 0)
{
return *reinterpret_cast<U*>( nth<T>( i ) );
}
// Get a pointer to the last A-th item skipping over any A pre i-th items.
template <typename...A>
remove_cref_t< select_last_t<A...> > * get()
{
using U = remove_cref_t< select_last_t<A...> >;
return static_cast<U*>( advance<A...>(get_data()) );
}
// Move "v" to a new slot in ther stack. Returns a reference to the new item.
template <class T>
remove_cref_t<T> & push( T v, FRAME * frame = nullptr );
// Copy "v" into "n" new items at the top of the stack. Returns a pointer
// to the first, i.e. top/front most, new item.
template <class T>
remove_cref_t<T> * push( T v, int32_t n, FRAME * frame = nullptr );
// Removes the top/left most "T" item from the stack and returns a copy of
// it.
template <class T>
remove_cref_t<T> pop()
{
using U = remove_cref_t<T>;
// Copy the top/left item.
U result = top<U>();
// Remove the obsolete top item.
pop<T>( 1 );
return result;
}
// Removes the top/left "n" "T" items from the stack.
template <class T>
void pop( int32_t n )
{
// Debugging validation.
using U = remove_cref_t<T>;
for (auto c = 0; c < n; ++c)
cleanups[cleanups_size-c-1].check<U>().reset();
// The removal of the n items happens by skipping the stack pointer
// past the n items.
set_data( nth<T>( n ) );
// We also "skip" the cleanups. As the caller has dealt with that in
// some form.
cleanups_size -= n;
}
//
template <class T>
void swap( int32_t a, int32_t b )
{
std::swap( this->top<T>(a), this->top<T>(b) );
std::swap(
this->cleanups[cleanups_size-a-1],
this->cleanups[cleanups_size-b-1] );
}
// RAII checks that the stack returns to a starting point at the end of a
// scope.
struct check
{
check(FRAME * f, STACK * s)
{
#if B2_FUNCTION_STACK_VALIDATE
frame = f;
stack = s;
saved = s->get<char>();
#endif
}
check & operator()()
{
#if B2_FUNCTION_STACK_VALIDATE
if (stack->get<char>() != saved)
{
b2::list_ref msgs;
msgs.push_back( b2::rule_and_args_to_string( frame ) );
msgs.push_back( "stack check failed." );
b2::out_warning( *msgs, frame );
b2::ensure( false );
}
#endif
return *this;
}
private:
#if B2_FUNCTION_STACK_VALIDATE
FRAME * frame = nullptr;
STACK * stack = nullptr;
char * saved = nullptr;
#endif
};
private:
void * start = nullptr;
void * end = nullptr;
void * data = nullptr;
using cleanup_f = void(*)( _stack*, void* );
struct cleanup_t
{
cleanup_f clean = nullptr;
#if B2_FUNCTION_STACK_VALIDATE
const char * type_name = nullptr;
std::string native_stack;
std::string jam_stack;
#endif
inline cleanup_t() = default;
template <class T>
inline cleanup_t(cleanup_f f, FRAME * frame, T*_) : clean(f)
{
#if B2_FUNCTION_STACK_VALIDATE
type_name = typeid(T).name();
native_stack = b2::stacktrace::to_string();
jam_stack = b2::jam::backtrace::to_string(frame);
#endif
}
inline cleanup_t & reset()
{
clean = nullptr;
#if B2_FUNCTION_STACK_VALIDATE
type_name = nullptr;
native_stack = "";
jam_stack = "";
#endif
return *this;
}
template <class T>
inline cleanup_t & check()
{
#if B2_FUNCTION_STACK_VALIDATE
static const char * type_name_c = typeid(T).name();
if (type_name_c != this->type_name)
{
err_printf(
"Stack type mismatch, trying to pop '%s' have '%s'"
" originally from:\n"
"(jam, recent first)\n"
"%s\n"
"(native, recent first)\n"
"%s\n",
type_name_c, this->type_name, jam_stack.c_str(),
native_stack.c_str());
}
b2::ensure(type_name_c == this->type_name);
#endif
return *this;
}
};
using cleanups_t = std::array<cleanup_t, (1 << 21)/sizeof(void*)>;
cleanups_t cleanups;
cleanups_t::size_type cleanups_size = 0;
struct list_alignment_helper
{
char ch;
LIST * l;
};
#define LISTPTR_ALIGN_BASE ( sizeof( struct list_alignment_helper ) - sizeof( LIST * ) )
#define LISTPTR_ALIGN ( ( LISTPTR_ALIGN_BASE > sizeof( LIST * ) ) ? sizeof( LIST * ) : LISTPTR_ALIGN_BASE )
inline void set_data(void * d)
{
data = d;
}
inline void * get_data()
{
return data;
}
// Get a pointer from the current top/left onward to the nth item.
template <typename T>
remove_cref_t<T> * nth( int32_t n )
{
using U = remove_cref_t<T>;
return &( static_cast<U*>( get_data() )[n] );
}
template <typename T>
struct advance_size
{
struct _helper_1 { remove_cref_t<T> a; };
struct _helper_2 { remove_cref_t<T> a; remove_cref_t<T> b; };
static const std::ptrdiff_t value = sizeof(_helper_2) - sizeof(_helper_1);
};
template <typename...A>
struct sum_advance_size {};
template <typename T>
struct sum_advance_size<T>
{
static const std::ptrdiff_t value
= advance_size<T>::value;
};
template <typename T, typename...A>
struct sum_advance_size<T, A...>
{
static const std::ptrdiff_t value
= advance_size<T>::value + sum_advance_size<A...>::value;
};
template <typename...R>
static void * advance(void * p)
{
p = static_cast<char*>(p)
+ sum_advance_size<R...>::value
- advance_size< select_last_t<R...> >::value;
return p;
}
template <typename T>
static void cleanup_item(_stack * s, T*_=nullptr)
{
s->set_data( s->nth<T>( 1 ) );
}
template <typename T>
void cleanup_push(int32_t n, FRAME * frame, T*_ = nullptr);
};
template <>
void _stack::cleanup_item<LIST*>(_stack * s, LIST**)
{
list_free( s->top<LIST*>() );
s->set_data( s->nth<LIST*>( 1 ) );
}
template <class T>
remove_cref_t<T> & _stack::push( T v, FRAME * frame )
{
return *push<T>( v, 1, frame );
}
template <class T>
remove_cref_t<T> * _stack::push( T v, int32_t n, FRAME * frame )
{
using U = remove_cref_t<T>;
set_data( nth<T>( -n ) );
std::uninitialized_fill_n( nth<U>( 0 ), n, v );
cleanup_push<U>( n, frame );
return nth<U>( 0 );
}
template <typename T>
void _stack::cleanup_push( int32_t n, FRAME * frame, T*t )
{
using U = remove_cref_t<T>;
cleanup_t c((cleanup_f)&_stack::cleanup_item<T>, frame, (U*)(t));
std::uninitialized_fill_n( &cleanups[cleanups_size], n, c );
cleanups_size += n;
}
}
static STACK * stack_global()
{
static _stack singleton;
return &singleton;
}
LIST * frame_get_local( FRAME * frame, int32_t idx )
{
/* The only local variables are the arguments. */
return list_copy( lol_get( frame->args, idx ) );
}
static OBJECT * function_get_constant( JAM_FUNCTION * function, int32_t idx )
{
return function->constants[ idx ];
}
static LIST * function_get_variable( JAM_FUNCTION * function, FRAME * frame,
int32_t idx )
{
return list_copy( var_get( frame->module, function->constants[ idx ] ) );
}
static void function_set_variable( JAM_FUNCTION * function, FRAME * frame,
int32_t idx, LIST * value )
{
var_set( frame->module, function->constants[ idx ], value, VAR_SET );
}
static LIST * function_swap_variable( JAM_FUNCTION * function, FRAME * frame,
int32_t idx, LIST * value )
{
return var_swap( frame->module, function->constants[ idx ], value );
}
static void function_append_variable( JAM_FUNCTION * function, FRAME * frame,
int32_t idx, LIST * value )
{
var_set( frame->module, function->constants[ idx ], value, VAR_APPEND );
}
static void function_default_variable( JAM_FUNCTION * function, FRAME * frame,
int32_t idx, LIST * value )
{
var_set( frame->module, function->constants[ idx ], value, VAR_DEFAULT );
}
static void function_set_rule( JAM_FUNCTION * function, FRAME * frame,
STACK * s, int32_t idx )
{
SUBFUNCTION * sub = function->functions + idx;
new_rule_body( frame->module, sub->name, sub->code, !sub->local );
}
static void function_set_actions( JAM_FUNCTION * function, FRAME * frame,
STACK * s, int32_t idx )
{
SUBACTION * sub = function->actions + idx;
LIST * bindlist = s->pop<LIST *>();
new_rule_actions( frame->module, sub->name, sub->command, bindlist,
sub->flags );
}
/*
* Returns the index if name is "<", ">", "1", "2", ... or "19" otherwise
* returns -1.
*/
static int32_t get_argument_index( char const * s )
{
if ( s[ 0 ] != '\0')
{
if ( s[ 1 ] == '\0' )
{
switch ( s[ 0 ] )
{
case '<': return 0;
case '>': return 1;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return s[ 0 ] - '1';
}
}
else if ( s[ 0 ] == '1' && s[ 2 ] == '\0' )
{
switch( s[ 1 ] )
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return s[ 1 ] - '0' + 10 - 1;
}
}
}
return -1;
}
static LIST * function_get_named_variable( JAM_FUNCTION * function,
FRAME * frame, OBJECT * name )
{
int32_t const idx = get_argument_index( object_str( name ) );
return idx == -1
? list_copy( var_get( frame->module, name ) )
: list_copy( lol_get( frame->args, idx ) );
}
static void function_set_named_variable( JAM_FUNCTION * function, FRAME * frame,
OBJECT * name, LIST * value)
{
var_set( frame->module, name, value, VAR_SET );
}
static LIST * function_swap_named_variable( JAM_FUNCTION * function,
FRAME * frame, OBJECT * name, LIST * value )
{
return var_swap( frame->module, name, value );
}
static void function_append_named_variable( JAM_FUNCTION * function,
FRAME * frame, OBJECT * name, LIST * value)
{
var_set( frame->module, name, value, VAR_APPEND );
}
static void function_default_named_variable( JAM_FUNCTION * function,
FRAME * frame, OBJECT * name, LIST * value )
{
var_set( frame->module, name, value, VAR_DEFAULT );
}
#define QUOTE_EXPANDED(x) STRINGIFY(x)
#define STRINGIFY(x) #x
static LIST * function_call_rule( JAM_FUNCTION * function, FRAME * frame,
STACK * s, int32_t n_args, char const * unexpanded, OBJECT * file, int32_t line )
{
FRAME inner;
int32_t i;
b2::list_ref first( s->pop<LIST *>(), true );
LIST * result = L0;
OBJECT * rulename;
frame->file = file;
frame->line = line;
if ( first.empty() )
{
b2::list_ref msgs;
msgs.push_back( b2::rule_and_args_to_string( frame ) );
msgs.push_back( std::string("rulename \"")
+ unexpanded + "\" expands to empty string." );
b2::out_warning( *msgs, frame );
first.reset();
for ( i = 0; i < n_args; ++i )
list_free( s->pop<LIST *>() );
return result;
}
rulename = object_copy( list_front( *first ) );
inner.prev = frame;
inner.prev_user = frame->module->user_module ? frame : frame->prev_user;
inner.module = frame->module; /* This gets fixed up in evaluate_rule(). */
if ( n_args > LOL_MAX )
{
b2::list_ref msgs;
msgs.push_back(
"rules are limited to " QUOTE_EXPANDED(LOL_MAX) " arguments." );
b2::out_error( *msgs, frame );
}
for ( i = 0; i < n_args; ++i )
lol_add( inner.args, s->top<LIST*>( n_args - i - 1 ) );
for ( i = 0; i < n_args; ++i )
s->pop<LIST *>();
if ( !first.pop_front().empty() )
{
if ( inner.args->count == 0 )
lol_add( inner.args, first.release() );
else
{
LIST * * const l = &inner.args->list[ 0 ];
*l = list_append( first.release(), *l );
}
}
result = evaluate_rule( bindrule( rulename, inner.module ), rulename, &inner );
object_free( rulename );
return result;
}
static LIST * function_call_member_rule( JAM_FUNCTION * function, FRAME * frame, STACK * s, int32_t n_args, OBJECT * rulename, OBJECT * file, int32_t line )
{
if ( n_args > LOL_MAX )
{
b2::list_ref msgs;
msgs.push_back(
"member rules are limited to " QUOTE_EXPANDED(LOL_MAX) " arguments." );
b2::out_error( *msgs, frame );
}
b2::list_ref first(s->pop<LIST *>(), true);
b2::lists args;
for (b2::lists::size_type i = 0; i < n_args; ++i)
args.push_back(b2::list_ref(s->top<LIST*>( n_args - i - 1 ), true));
for (b2::lists::size_type i = 0; i < n_args; ++i)
s->pop<LIST *>();
return call_member_rule( rulename, frame, std::move(first), std::move(args) );
}
#undef QUOTE_EXPANDED
#undef STRINGIFY
/* Variable expansion */
typedef struct
{
int32_t sub1;
int32_t sub2;
} subscript_t;
typedef struct
{
PATHNAME f; /* :GDBSMR -- pieces */
PATHPART empty; /* :E -- default for empties */
PATHPART join; /* :J -- join list with char */
PATHPART prefix; /* :< */
PATHPART postfix; /* :> */
bool parent:1; /* :P -- go to parent directory */
bool filemods:1; /* one of the above applied */
bool downshift:1; /* :L -- downshift result */
bool upshift:1; /* :U -- upshift result */
bool to_slashes:1; /* :T -- convert "\" to "/" */
bool to_windows:1; /* :W -- convert cygwin to native paths */
bool opt_file:1; /* :O=F -- replace @() with the file part */
bool opt_content:1; /* :O=C -- repalce @() with the content (E) part */
} VAR_EDITS;
struct VAR_EXPANDED
{
LIST * value = L0;
LIST * inner = L0;
bool opt_file:1;
bool opt_content:1;
};
static VAR_EXPANDED apply_modifiers_impl( LIST * result, string * buf,
VAR_EDITS * edits, int32_t n, LISTITER iter, LISTITER end );
static void get_iters( subscript_t const subscript, LISTITER * const first,
LISTITER * const last, int32_t const length );
/*
* var_edit_parse() - parse : modifiers into PATHNAME structure
*
* The : modifiers in a $(varname:modifier) currently support replacing or
* omitting elements of a filename, and so they are parsed into a PATHNAME
* structure (which contains pointers into the original string).
*
* Modifiers of the form "X=value" replace the component X with the given value.
* Modifiers without the "=value" cause everything but the component X to be
* omitted. X is one of:
*
* G <grist>
* D directory name
* B base name
* S .suffix
* M (member)
* R root directory - prepended to whole path
*
* This routine sets:
*
* f->f_xxx.ptr = 0
* f->f_xxx.len = 0
* -> leave the original component xxx
*
* f->f_xxx.ptr = string
* f->f_xxx.len = strlen( string )
* -> replace component xxx with string
*
* f->f_xxx.ptr = ""
* f->f_xxx.len = 0
* -> omit component xxx
*
* var_edit_file() below and path_build() obligingly follow this convention.
*/
static int32_t var_edit_parse( char const * mods, VAR_EDITS * edits, int32_t havezeroed
)
{
while ( *mods )
{
PATHPART * fp;
bool opt = false;
switch ( *mods++ )
{
case 'L': edits->downshift = 1; continue;
case 'U': edits->upshift = 1; continue;
case 'P': edits->parent = edits->filemods = 1; continue;
case 'E': fp = &edits->empty; goto strval;
case 'J': fp = &edits->join; goto strval;
case 'G': fp = &edits->f.f_grist; goto fileval;
case 'R': fp = &edits->f.f_root; goto fileval;
case 'D': fp = &edits->f.f_dir; goto fileval;
case 'B': fp = &edits->f.f_base; goto fileval;
case 'S': fp = &edits->f.f_suffix; goto fileval;
case 'M': fp = &edits->f.f_member; goto fileval;
case 'T': edits->to_slashes = 1; continue;
case 'W': edits->to_windows = 1; continue;
case '<': fp = &edits->prefix; goto strval;
case '>': fp = &edits->postfix; goto strval;
case 'O': opt = true; goto strval;
default:
continue; /* Should complain, but so what... */
}
fileval:
/* Handle :CHARS, where each char (without a following =) selects a
* particular file path element. On the first such char, we deselect all
* others (by setting ptr = "", len = 0) and for each char we select
* that element (by setting ptr = 0).
*/
edits->filemods = 1;
if ( *mods != '=' )
{
if ( !havezeroed++ )
{
int32_t i;
for ( i = 0; i < 6; ++i )
{
edits->f.part[ i ].len = 0;
edits->f.part[ i ].ptr = "";
}
}
fp->ptr = 0;
continue;
}
strval:
/* Handle :O=??? */
if ( opt )
{
if ( *mods == '=' )
{
for (++mods; *mods; ++mods)
{
switch ( *mods )
{
case 'F': edits->opt_file = true; break;
case 'C': edits->opt_content = true; break;
}
}
}
}
else
{
/* Handle :X=value, or :X */
if ( *mods != '=' )
{
fp->ptr = "";
fp->len = 0;
}
else
{
fp->ptr = ++mods;
fp->len = int32_t(strlen( mods ));
mods += fp->len;
}
}
}
return havezeroed;
}
/*
* var_edit_file() - copy input target name to output, modifying filename.
*/
static void var_edit_file( char const * in, string * out, VAR_EDITS * edits )
{
if ( edits->filemods )
{
PATHNAME pathname;
/* Parse apart original filename, putting parts into "pathname". */
path_parse( in, &pathname );
/* Replace any pathname with edits->f */
if ( edits->f.f_grist .ptr ) pathname.f_grist = edits->f.f_grist;
if ( edits->f.f_root .ptr ) pathname.f_root = edits->f.f_root;
if ( edits->f.f_dir .ptr ) pathname.f_dir = edits->f.f_dir;
if ( edits->f.f_base .ptr ) pathname.f_base = edits->f.f_base;
if ( edits->f.f_suffix.ptr ) pathname.f_suffix = edits->f.f_suffix;
if ( edits->f.f_member.ptr ) pathname.f_member = edits->f.f_member;
/* If requested, modify pathname to point to parent. */
if ( edits->parent )
path_parent( &pathname );