-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathpackage.c
More file actions
1919 lines (1502 loc) · 43.4 KB
/
package.c
File metadata and controls
1919 lines (1502 loc) · 43.4 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
#include "package.h"
#include "program.h"
#include "use.h"
#include "../codegen/codegen.h"
#include "../ast/source.h"
#include "../ast/parser.h"
#include "../ast/ast.h"
#include "../ast/token.h"
#include "../expr/literal.h"
#include "../../libponyrt/gc/serialise.h"
#include "../../libponyrt/mem/pool.h"
#include "../../libponyrt/sched/scheduler.h"
#include "ponyassert.h"
#include <blake2.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#define EXTENSION ".pony"
#ifdef PLATFORM_IS_WINDOWS
# define PATH_SLASH '\\'
# define PATH_LIST_SEPARATOR ';'
#else
# define PATH_SLASH '/'
# define PATH_LIST_SEPARATOR ':'
#endif
#ifdef PLATFORM_IS_VISUAL_STUDIO
/** Disable warning about "getenv" begin unsafe. The alternatives, s_getenv and
* _dupenv_s are incredibly inconvenient and expensive to use. Since such a
* warning could make sense for other function calls, we only disable it for
* this file.
*/
# pragma warning(disable:4996)
#endif
DECLARE_HASHMAP_SERIALISE(package_set, package_set_t, package_t)
// Per package state
struct package_t
{
const char* path; // Absolute path
const char* qualified_name; // For pretty printing, eg "builtin/U32"
const char* id; // Hygienic identifier
const char* filename; // Directory name
const char* symbol; // Wart to use for symbol names
ast_t* ast;
package_set_t dependencies;
package_group_t* group;
size_t group_index;
size_t next_hygienic_id;
size_t low_index;
bool allow_ffi;
bool on_stack;
};
// Minimal package data structure for signature computation.
typedef struct package_signature_t
{
const char* filename;
package_group_t* group;
size_t group_index;
} package_signature_t;
// A strongly connected component in the package dependency graph
struct package_group_t
{
char* signature;
package_set_t members;
};
// Per defined magic package state
struct magic_package_t
{
const char* path;
const char* src;
const char* mapped_path;
struct magic_package_t* next;
};
DECLARE_STACK(package_stack, package_stack_t, package_t)
DEFINE_STACK(package_stack, package_stack_t, package_t)
DEFINE_LIST_SERIALISE(package_group_list, package_group_list_t, package_group_t,
NULL, package_group_free, package_group_pony_type())
static size_t package_hash(package_t* pkg)
{
// Hash the full string instead of the stringtab pointer. We want a
// deterministic hash in order to enable deterministic hashmap iteration,
// which in turn enables deterministic package signatures.
return (size_t)ponyint_hash_str(pkg->qualified_name);
}
static bool package_cmp(package_t* a, package_t* b)
{
return a->qualified_name == b->qualified_name;
}
DEFINE_HASHMAP_SERIALISE(package_set, package_set_t, package_t, package_hash,
package_cmp, NULL, package_pony_type())
// Find the magic source code associated with the given path, if any
static magic_package_t* find_magic_package(const char* path, pass_opt_t* opt)
{
for(magic_package_t* p = opt->magic_packages; p != NULL; p = p->next)
{
if(path == p->path)
return p;
}
return NULL;
}
// Attempt to parse the specified source file and add it to the given AST
// @return true on success, false on error
static bool parse_source_file(ast_t* package, const char* file_path,
pass_opt_t* opt)
{
pony_assert(package != NULL);
pony_assert(file_path != NULL);
pony_assert(opt != NULL);
if(opt->print_filenames)
printf("Opening %s\n", file_path);
const char* error_msg = NULL;
source_t* source = source_open(file_path, &error_msg);
if(source == NULL)
{
if(error_msg == NULL)
error_msg = "couldn't open file";
errorf(opt->check.errors, file_path, "%s %s", error_msg, file_path);
return false;
}
return module_passes(package, opt, source);
}
// Attempt to parse the specified source code and add it to the given AST
// @return true on success, false on error
static bool parse_source_code(ast_t* package, const char* src,
pass_opt_t* opt)
{
pony_assert(src != NULL);
pony_assert(opt != NULL);
if(opt->print_filenames)
printf("Opening magic source\n");
source_t* source = source_open_string(src);
pony_assert(source != NULL);
return module_passes(package, opt, source);
}
void path_cat(const char* part1, const char* part2, char result[FILENAME_MAX])
{
size_t len1 = 0;
size_t lensep = 0;
if(part1 != NULL)
{
len1 = strlen(part1);
lensep = 1;
}
size_t len2 = strlen(part2);
if((len1 + lensep + len2) >= FILENAME_MAX)
{
result[0] = '\0';
return;
}
if(part1 != NULL)
{
memcpy(result, part1, len1);
result[len1] = PATH_SLASH;
memcpy(&result[len1 + 1], part2, len2 + 1);
} else {
memcpy(result, part2, len2 + 1);
}
}
static int string_compare(const void* a, const void* b)
{
return strcmp(*(const char**)a, *(const char**)b);
}
// Attempt to parse the source files in the specified directory and add them to
// the given package AST
// @return true on success, false on error
static bool parse_files_in_dir(ast_t* package, const char* dir_path,
pass_opt_t* opt)
{
PONY_ERRNO err = 0;
PONY_DIR* dir = pony_opendir(dir_path, &err);
errors_t* errors = opt->check.errors;
if(dir == NULL)
{
switch(err)
{
case EACCES: errorf(errors, dir_path, "permission denied"); break;
case ENOENT: errorf(errors, dir_path, "does not exist"); break;
case ENOTDIR: errorf(errors, dir_path, "not a directory"); break;
default: errorf(errors, dir_path, "unknown error"); break;
}
return false;
}
size_t count = 0;
size_t buf_size = 4 * sizeof(const char*);
const char** entries = (const char**)ponyint_pool_alloc_size(buf_size);
PONY_DIRINFO* d;
while((d = pony_dir_entry_next(dir)) != NULL)
{
// Handle only files with the specified extension that don't begin with
// a dot. This avoids including UNIX hidden files in a build.
const char* name = stringtab(pony_dir_info_name(d));
if(name[0] == '.')
continue;
const char* p = strrchr(name, '.');
if((p != NULL) && (strcmp(p, EXTENSION) == 0))
{
if((count * sizeof(const char*)) == buf_size)
{
size_t new_buf_size = buf_size * 2;
entries = (const char**)ponyint_pool_realloc_size(buf_size,
new_buf_size, entries);
buf_size = new_buf_size;
}
entries[count++] = name;
}
}
pony_closedir(dir);
// In order for package signatures to be deterministic, file parsing order
// must be deterministic too.
qsort(entries, count, sizeof(const char*), string_compare);
bool r = true;
for(size_t i = 0; i < count; i++)
{
char fullpath[FILENAME_MAX];
path_cat(dir_path, entries[i], fullpath);
r &= parse_source_file(package, fullpath, opt);
}
ponyint_pool_free_size(buf_size, entries);
return r;
}
// Check whether the directory specified by catting the given base and path
// exists
// @return The resulting directory path, which should not be deleted and is
// valid indefinitely. NULL is directory cannot be found.
static const char* try_path(const char* base, const char* path,
bool* out_found_notdir)
{
char composite[FILENAME_MAX];
char file[FILENAME_MAX];
path_cat(base, path, composite);
if(pony_realpath(composite, file) != file)
return NULL;
struct stat s;
int err = stat(file, &s);
if(err == -1)
return NULL;
if(!S_ISDIR(s.st_mode))
{
if(out_found_notdir != NULL)
*out_found_notdir = true;
return NULL;
}
return stringtab(file);
}
static bool is_root(const char* path)
{
pony_assert(path != NULL);
#if defined(PLATFORM_IS_WINDOWS)
pony_assert(path[0] != '\0');
pony_assert(path[1] == ':');
if((path[2] == '\0'))
return true;
if((path[2] == '\\') && (path[3] == '\0'))
return true;
#else
if((path[0] == '/') && (path[1] == '\0'))
return true;
#endif
return false;
}
// Try base/../pony_packages/path, and keep adding .. to look another level up
// until we are looking in /pony_packages/path
static const char* try_package_path(const char* base, const char* path,
bool* out_found_notdir)
{
char path1[FILENAME_MAX];
char path2[FILENAME_MAX];
path_cat(NULL, base, path1);
do
{
path_cat(path1, "..", path2);
if(pony_realpath(path2, path1) != path1)
break;
path_cat(path1, "pony_packages", path2);
const char* result = try_path(path2, path, out_found_notdir);
if(result != NULL)
return result;
} while(!is_root(path1));
return NULL;
}
// Attempt to find the specified package directory in our search path
// @return The resulting directory path, which should not be deleted and is
// valid indefinitely. NULL is directory cannot be found.
static const char* find_path(ast_t* from, const char* path,
bool* out_is_relative, bool* out_found_notdir, pass_opt_t* opt)
{
if(out_is_relative != NULL)
*out_is_relative = false;
if(out_found_notdir != NULL)
*out_found_notdir = false;
// First check for an absolute path
if(is_path_absolute(path))
return try_path(NULL, path, out_found_notdir);
// Get the base directory
const char* base;
if((from == NULL) || (ast_id(from) == TK_PROGRAM))
{
base = NULL;
} else {
from = ast_nearest(from, TK_PACKAGE);
package_t* pkg = (package_t*)ast_data(from);
base = pkg->path;
}
// Try a path relative to the base
const char* result = try_path(base, path, out_found_notdir);
if(result != NULL)
{
if(out_is_relative != NULL)
*out_is_relative = true;
return result;
}
// If it's a relative path, don't try elsewhere
if(!is_path_relative(path))
{
// Check ../pony_packages and further up the tree
if(base != NULL)
{
result = try_package_path(base, path, out_found_notdir);
if(result != NULL)
return result;
// Check ../pony_packages from the compiler target
if((from != NULL) && (ast_id(from) == TK_PACKAGE))
{
ast_t* target = ast_child(ast_parent(from));
package_t* pkg = (package_t*)ast_data(target);
base = pkg->path;
result = try_package_path(base, path, out_found_notdir);
if(result != NULL)
return result;
}
}
// Try the search paths
for(strlist_t* p = opt->package_search_paths; p != NULL;
p = strlist_next(p))
{
result = try_path(strlist_data(p), path, out_found_notdir);
if(result != NULL)
return result;
}
}
return NULL;
}
// Convert the given ID to a hygenic string. The resulting string should not be
// deleted and is valid indefinitely.
static const char* id_to_string(const char* prefix, size_t id)
{
if(prefix == NULL)
prefix = "";
size_t len = strlen(prefix);
size_t buf_size = len + 32;
char* buffer = (char*)ponyint_pool_alloc_size(buf_size);
snprintf(buffer, buf_size, "%s$" __zu, prefix, id);
return stringtab_consume(buffer, buf_size);
}
static bool symbol_in_use(ast_t* program, const char* symbol)
{
ast_t* package = ast_child(program);
while(package != NULL)
{
package_t* pkg = (package_t*)ast_data(package);
if(pkg->symbol == symbol)
return true;
package = ast_sibling(package);
}
return false;
}
static const char* string_to_symbol(const char* string)
{
bool prefix = false;
if(!((string[0] >= 'a') && (string[0] <= 'z')) &&
!((string[0] >= 'A') && (string[0] <= 'Z')))
{
// If it doesn't start with a letter, prefix an underscore.
prefix = true;
}
size_t len = strlen(string);
size_t buf_size = len + prefix + 1;
char* buf = (char*)ponyint_pool_alloc_size(buf_size);
memcpy(buf + prefix, string, len + 1);
if(prefix)
buf[0] = '_';
for(size_t i = prefix; i < len; i++)
{
if(
(buf[i] == '_') ||
((buf[i] >= 'a') && (buf[i] <= 'z')) ||
((buf[i] >= '0') && (buf[i] <= '9'))
)
{
// Do nothing.
} else if((buf[i] >= 'A') && (buf[i] <= 'Z')) {
// Force lower case.
buf[i] |= 0x20;
} else {
// Smash a non-symbol character to an underscore.
buf[i] = '_';
}
}
return stringtab_consume(buf, buf_size);
}
static const char* symbol_suffix(const char* symbol, size_t suffix)
{
size_t len = strlen(symbol);
size_t buf_size = len + 32;
char* buf = (char*)ponyint_pool_alloc_size(buf_size);
snprintf(buf, buf_size, "%s" __zu, symbol, suffix);
return stringtab_consume(buf, buf_size);
}
static const char* create_package_symbol(ast_t* program, const char* filename)
{
const char* symbol = string_to_symbol(filename);
size_t suffix = 1;
while(symbol_in_use(program, symbol))
{
symbol = symbol_suffix(symbol, suffix);
suffix++;
}
return symbol;
}
// Create a package AST, set up its state and add it to the given program
ast_t* create_package(ast_t* program, const char* name,
const char* qualified_name, pass_opt_t* opt)
{
ast_t* package = ast_blank(TK_PACKAGE);
uint32_t pkg_id = program_assign_pkg_id(program);
package_t* pkg = POOL_ALLOC(package_t);
pkg->path = name;
pkg->qualified_name = qualified_name;
pkg->id = id_to_string(NULL, pkg_id);
const char* p = strrchr(pkg->path, PATH_SLASH);
if(p == NULL)
p = pkg->path;
else
p = p + 1;
pkg->filename = stringtab(p);
if(pkg_id > 1)
pkg->symbol = create_package_symbol(program, pkg->filename);
else
pkg->symbol = NULL;
pkg->ast = package;
package_set_init(&pkg->dependencies, 1);
pkg->group = NULL;
pkg->group_index = -1;
pkg->next_hygienic_id = 0;
pkg->low_index = -1;
ast_setdata(package, pkg);
ast_scope(package);
ast_append(program, package);
ast_set(program, pkg->path, package, SYM_NONE, false);
ast_set(program, pkg->id, package, SYM_NONE, false);
strlist_t* safe = opt->safe_packages;
if((safe != NULL) && (strlist_find(safe, pkg->path) == NULL))
pkg->allow_ffi = false;
else
pkg->allow_ffi = true;
pkg->on_stack = false;
return package;
}
// Check that the given path exists and add it to our package search paths
static bool add_path(const char* path, pass_opt_t* opt)
{
#ifdef PLATFORM_IS_WINDOWS
// The Windows implementation of stat() cannot cope with trailing a \ on a
// directory name, so we remove it here if present. It is not safe to modify
// the given path since it may come direct from getenv(), so we copy.
char buf[FILENAME_MAX];
strcpy(buf, path);
size_t len = strlen(path);
if(path[len - 1] == '\\')
{
buf[len - 1] = '\0';
path = buf;
}
#endif
struct stat s;
int err = stat(path, &s);
if((err != -1) && S_ISDIR(s.st_mode))
{
path = stringtab(path);
strlist_t* search = opt->package_search_paths;
if(strlist_find(search, path) == NULL)
opt->package_search_paths = strlist_append(search, path);
}
return true;
}
// Safely concatenate paths and add it to package search paths
static bool add_relative_path(const char* path, const char* relpath,
pass_opt_t* opt)
{
char buf[FILENAME_MAX];
if(strlen(path) + strlen(relpath) >= FILENAME_MAX)
return false;
strcpy(buf, path);
strcat(buf, relpath);
return add_path(buf, opt);
}
static bool add_safe(const char* path, pass_opt_t* opt)
{
path = stringtab(path);
strlist_t* safe = opt->safe_packages;
if(strlist_find(safe, path) == NULL)
opt->safe_packages = strlist_append(safe, path);
return true;
}
// Determine the absolute path of the directory the current executable is in
// and add it to our search path
static bool add_pony_installation_dir(const char* path, pass_opt_t* opt)
{
// TODO STA:
// validate path and add error if it doesn't exist.
// we should really validate it contains all the directories
// we expect
bool success = true;
add_path(path, opt);
// Allow ponyc to find the lib directory when it is installed.
#ifdef PLATFORM_IS_WINDOWS
success = add_relative_path(path, "..\\lib", opt);
#else
const char* link_arch = opt->link_arch != NULL ? opt->link_arch
: PONY_ARCH;
size_t lib_len = 8 + strlen(link_arch);
char* lib_path = (char*)ponyint_pool_alloc_size(lib_len);
snprintf(lib_path, lib_len, "../lib/%s", link_arch);
success = add_relative_path(path, lib_path, opt);
ponyint_pool_free_size(lib_len, lib_path);
if(!success)
return false;
// for when run from build directory
lib_len = 5 + strlen(link_arch);
lib_path = (char*)ponyint_pool_alloc_size(lib_len);
snprintf(lib_path, lib_len, "lib/%s", link_arch);
success = add_relative_path(path, lib_path, opt);
ponyint_pool_free_size(lib_len, lib_path);
#endif
if(!success)
return false;
// Allow ponyc to find the packages directory when it is installed.
#ifdef PLATFORM_IS_WINDOWS
success = add_relative_path(path, "..\\packages", opt);
#else
success = add_relative_path(path, "../packages", opt);
#endif
if(!success)
return false;
// Check two levels up. This allows ponyc to find the packages directory
// when it is built from source.
#ifdef PLATFORM_IS_WINDOWS
success = add_relative_path(path, "..\\..\\packages", opt);
#else
success = add_relative_path(path, "../../packages", opt);
#endif
if(!success)
return false;
return true;
}
static bool add_exec_dir(pass_opt_t* opt)
{
char path[FILENAME_MAX];
bool success = get_compiler_exe_directory(path, opt->argv0);
errors_t* errors = opt->check.errors;
if(!success)
{
errorf(errors, NULL, "Error determining executable path or directory.");
return false;
}
add_path(path, opt);
return add_pony_installation_dir(path, opt);
}
bool package_init(pass_opt_t* opt)
{
// Command line --path entries have already been added to
// package_search_paths during option parsing. We need the standard library
// paths to come first so that --path directories cannot shadow stdlib
// packages. This is the same approach used to fix PONYPATH shadowing in
// https://github.com/ponylang/ponyc/issues/3779 — save the existing paths,
// add stdlib paths first, then re-append the saved paths after.
strlist_t* cmdline_paths = opt->package_search_paths;
opt->package_search_paths = NULL;
if(!add_exec_dir(opt))
{
strlist_free(cmdline_paths);
errorf(opt->check.errors, NULL, "Error adding package paths relative to ponyc binary location");
return false;
}
// Re-append command line paths after the standard library paths.
for(strlist_t* p = cmdline_paths; p != NULL; p = strlist_next(p))
{
const char* path = strlist_data(p);
if(strlist_find(opt->package_search_paths, path) == NULL)
opt->package_search_paths = strlist_append(opt->package_search_paths,
path);
}
strlist_free(cmdline_paths);
package_add_paths(getenv("PONYPATH"), opt);
// Finally we add OS specific paths.
#ifdef PLATFORM_IS_POSIX_BASED
add_path("/usr/local/lib", opt);
add_path("/opt/local/lib", opt);
#endif
#ifdef PLATFORM_IS_DRAGONFLY
add_path("/usr/local/cxx_atomics", opt);
#endif
// Convert all the safe packages to their full paths.
strlist_t* full_safe = NULL;
strlist_t* safe = opt->safe_packages;
while(safe != NULL)
{
const char* path;
safe = strlist_pop(safe, &path);
// Lookup (and hence normalise) path.
path = find_path(NULL, path, NULL, NULL, opt);
if(path == NULL)
{
strlist_free(full_safe);
strlist_free(safe);
opt->safe_packages = NULL;
return false;
}
full_safe = strlist_push(full_safe, path);
}
opt->safe_packages = full_safe;
return true;
}
bool package_init_lib(pass_opt_t* opt, const char* pony_installation)
{
// TODO STA: does this need more than this subset of package_init?
package_add_paths(getenv("PONYPATH"), opt);
if(!add_pony_installation_dir(pony_installation, opt))
{
errorf(opt->check.errors, NULL, "Error adding package paths relative to ponyc installation location");
return false;
}
return true;
}
bool handle_path_list(const char* paths, path_fn f, pass_opt_t* opt)
{
if(paths == NULL)
return true;
bool ok = true;
while(true)
{
// Find end of next path
const char* p = strchr(paths, PATH_LIST_SEPARATOR);
size_t len;
if(p != NULL)
{
// Separator found
len = p - paths;
} else {
// Separator not found, this is the last path in the list
len = strlen(paths);
}
if(len >= FILENAME_MAX)
{
errorf(opt->check.errors, NULL, "Path too long in %s", paths);
} else {
char path[FILENAME_MAX];
memcpy(path, paths, len);
path[len] = '\0';
ok = f(path, opt) && ok;
}
if(p == NULL) // No more separators
break;
paths = p + 1;
}
return ok;
}
void package_add_paths(const char* paths, pass_opt_t* opt)
{
handle_path_list(paths, add_path, opt);
}
bool package_add_safe(const char* paths, pass_opt_t* opt)
{
add_safe("builtin", opt);
return handle_path_list(paths, add_safe, opt);
}
void package_add_magic_src(const char* path, const char* src, pass_opt_t* opt)
{
magic_package_t* n = POOL_ALLOC(magic_package_t);
n->path = stringtab(path);
n->src = src;
n->mapped_path = NULL;
n->next = opt->magic_packages;
opt->magic_packages = n;
}
void package_add_magic_path(const char* path, const char* mapped_path,
pass_opt_t* opt)
{
magic_package_t* n = POOL_ALLOC(magic_package_t);
n->path = stringtab(path);
n->src = NULL;
n->mapped_path = stringtab(mapped_path);
n->next = opt->magic_packages;
opt->magic_packages = n;
}
void package_clear_magic(pass_opt_t* opt)
{
magic_package_t* p = opt->magic_packages;
while(p != NULL)
{
magic_package_t* next = p->next;
POOL_FREE(magic_package_t, p);
p = next;
}
opt->magic_packages = NULL;
}
ast_t* program_load(const char* path, pass_opt_t* opt)
{
ast_t* program = ast_blank(TK_PROGRAM);
ast_scope(program);
opt->program_pass = PASS_PARSE;
// Always load builtin package first, then the specified one.
if(package_load(program, stringtab("builtin"), opt) == NULL ||
package_load(program, path, opt) == NULL)
{
ast_free(program);
return NULL;
}
// Reorder packages so specified package is first.
ast_t* builtin = ast_pop(program);
ast_append(program, builtin);
if(!ast_passes_program(program, opt))
{
ast_free(program);
return NULL;
}
return program;
}
ast_t* package_load(ast_t* from, const char* path, pass_opt_t* opt)
{
pony_assert(from != NULL);
magic_package_t* magic = find_magic_package(path, opt);
const char* full_path = path;
const char* qualified_name = path;
ast_t* program = ast_nearest(from, TK_PROGRAM);
if(magic == NULL)
{
// Lookup (and hence normalise) path
bool is_relative = false;
bool found_notdir = false;
full_path = find_path(from, path, &is_relative, &found_notdir, opt);
if(full_path == NULL)
{
errorf(opt->check.errors, path, "couldn't locate this path");
if(found_notdir)
errorf_continue(opt->check.errors, path, "note that a compiler "
"invocation or a 'use' directive must refer to a directory");
return NULL;
}
if((from != program) && is_relative)
{
// Package to load is relative to from, build the qualified name
// The qualified name should be relative to the program being built
// and account for the relative path we were provided not being relative
// the program being built
ast_t* parent = ast_nearest(from, TK_PACKAGE);
package_t* parent_pkg = (package_t*)ast_data(parent);
if(parent_pkg != NULL)
{
const char* package_path = path;
size_t relatives = 0;
while(true)
{
if(strncmp("../", package_path, 3) == 0)
{
package_path += 3;
relatives += 1;
} else if(strncmp("./", package_path, 2) == 0)
{
package_path += 2;
}
else
{
break;
}
}
const char* base_name = parent_pkg->qualified_name;
size_t base_name_len = strlen(base_name);
while(relatives > 0 && base_name_len > 0)