comparison dmd/Module.d @ 79:43073c7c7769

updated to 2.035 also implemented a few missing functions still crashes in Import.importAll though
author Trass3r
date Mon, 30 Aug 2010 03:57:51 +0200
parents ad4792a1cfd6
children 722df8e7509c
comparison
equal deleted inserted replaced
78:b98fa8a4bf04 79:43073c7c7769
10 import dmd.Import; 10 import dmd.Import;
11 import dmd.ClassDeclaration; 11 import dmd.ClassDeclaration;
12 import dmd.ModuleDeclaration; 12 import dmd.ModuleDeclaration;
13 import dmd.File; 13 import dmd.File;
14 import dmd.Identifier; 14 import dmd.Identifier;
15 import dmd.Json;
15 import dmd.Dsymbol; 16 import dmd.Dsymbol;
16 import dmd.ModuleInfoDeclaration; 17 import dmd.ModuleInfoDeclaration;
17 import dmd.FuncDeclaration; 18 import dmd.FuncDeclaration;
18 import dmd.Loc; 19 import dmd.Loc;
19 import dmd.Macro; 20 import dmd.Macro;
395 396
396 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) 397 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
397 { 398 {
398 assert(false); 399 assert(false);
399 } 400 }
401
402 override void toJsonBuffer(OutBuffer buf)
403 {
404 buf.writestring("{\n");
405
406 JsonProperty(buf, Pname, md.toChars());
407
408 JsonProperty(buf, Pkind, kind());
409
410 JsonProperty(buf, Pfile, srcfile.toChars());
411
412 if (comment)
413 JsonProperty(buf, Pcomment, comment);
414
415 JsonString(buf, Pmembers);
416 buf.writestring(" : [\n");
417
418 size_t offset = buf.offset;
419 foreach (Dsymbol s; members)
420 {
421 if (offset != buf.offset)
422 {
423 buf.writestring(",");
424 offset = buf.offset;
425 }
426 s.toJsonBuffer(buf);
427 }
428
429 buf.writestring("]\n");
430
431 buf.writestring("}\n");
432 }
400 433
401 override string kind() 434 override string kind()
402 { 435 {
403 return "module"; 436 return "module";
404 } 437 }
610 /* If it starts with the string "Ddoc", then it's a documentation 643 /* If it starts with the string "Ddoc", then it's a documentation
611 * source file. 644 * source file.
612 */ 645 */
613 if (buflen >= 4 && memcmp(buf, "Ddoc".ptr, 4) == 0) 646 if (buflen >= 4 && memcmp(buf, "Ddoc".ptr, 4) == 0)
614 { 647 {
615 comment = buf + 4; 648 comment = cast(string) ((buf + 4)[0 .. buflen]);
616 isDocFile = 1; 649 isDocFile = 1;
617 if (!docfile) 650 if (!docfile)
618 setDocfile(); 651 setDocfile();
619 return; 652 return;
620 } 653 }
625 ///Html h = new Html(srcname, buf, buflen); 658 ///Html h = new Html(srcname, buf, buflen);
626 ///h.extractCode(dbuf); 659 ///h.extractCode(dbuf);
627 ///buf = dbuf.data; 660 ///buf = dbuf.data;
628 ///buflen = dbuf.offset; 661 ///buflen = dbuf.offset;
629 662
630 version (IN_GCC) { 663 version (IN_GCC)
664 {
631 // dump extracted source 665 // dump extracted source
632 ///if (dump_source) 666 ///if (dump_source)
633 /// d_gcc_dump_source(srcname, "d.utf-8", buf, buflen); 667 /// d_gcc_dump_source(srcname, "d.utf-8", buf, buflen);
634 } 668 }
635 } 669 }
671 amodules.push(cast(void*)this); 705 amodules.push(cast(void*)this);
672 } 706 }
673 } 707 }
674 } 708 }
675 709
710 override void importAll(Scope prevsc)
711 {
712 //writef("+Module.importAll(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
713
714 if (scope_ !is null)
715 return; // already done
716
717 /* Note that modules get their own scope, from scratch.
718 * This is so regardless of where in the syntax a module
719 * gets imported, it is unaffected by context.
720 * Ignore prevsc.
721 */
722 Scope sc = Scope.createGlobal(this); // create root scope
723
724 // Add import of "object" if this module isn't "object"
725 if (ident != Id.object)
726 {
727 if (members.dim == 0 || members[0].ident != Id.object)
728 {
729 Import im = new Import(Loc(), null, Id.object, null, 0);
730 members.shift(im);
731 }
732 }
733
734 if (!symtab)
735 {
736 // Add all symbols into module's symbol table
737 symtab = new DsymbolTable();
738 foreach (Dsymbol s; members)
739 s.addMember(null, sc.scopesym, 1);
740 }
741 // anything else should be run after addMember, so version/debug symbols are defined
742
743 /* Set scope for the symbols so that if we forward reference
744 * a symbol, it can possibly be resolved on the spot.
745 * If this works out well, it can be extended to all modules
746 * before any semantic() on any of them.
747 */
748 setScope(sc); // remember module scope for semantic
749 foreach (Dsymbol s; members)
750 s.setScope(sc);
751
752 foreach (Dsymbol s; members)
753 s.importAll(sc);
754
755 sc = sc.pop();
756 sc.pop(); // 2 pops because Scope::createGlobal() created 2
757 }
758
676 void semantic() // semantic analysis 759 void semantic() // semantic analysis
677 { 760 {
678 int i;
679
680 if (semanticstarted) 761 if (semanticstarted)
681 return; 762 return;
682 763
683 //printf("+Module.semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent); 764 //printf("+Module.semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
684 semanticstarted = 1; 765 semanticstarted = 1;
685 766
686 // Note that modules get their own scope, from scratch. 767 // Note that modules get their own scope, from scratch.
687 // This is so regardless of where in the syntax a module 768 // This is so regardless of where in the syntax a module
688 // gets imported, it is unaffected by context. 769 // gets imported, it is unaffected by context.
689 Scope sc = Scope.createGlobal(this); // create root scope 770 Scope sc = scope_; // // see if already got one from importAll()
690 771 if (!sc)
691 //printf("Module = %p, linkage = %d\n", sc.scopesym, sc.linkage); 772 {
692 773 writef("test2\n");
774 Scope.createGlobal(this); // create root scope
775 }
776
777 //writef("Module = %p, linkage = %d\n", sc.scopesym, sc.linkage);
778
779 static if (false)
780 {
693 // Add import of "object" if this module isn't "object" 781 // Add import of "object" if this module isn't "object"
694 if (ident !is Id.object) 782 if (ident !is Id.object)
695 { 783 {
696 auto im = new Import(Loc(0), null, Id.object, null, 0); 784 auto im = new Import(Loc(0), null, Id.object, null, 0);
697 members.shift(im); 785 members.shift(im);
709 * If this works out well, it can be extended to all modules 797 * If this works out well, it can be extended to all modules
710 * before any semantic() on any of them. 798 * before any semantic() on any of them.
711 */ 799 */
712 foreach(Dsymbol s; members) 800 foreach(Dsymbol s; members)
713 s.setScope(sc); 801 s.setScope(sc);
802 }
714 803
715 // Pass 1 semantic routines: do public side of the definition 804 // Pass 1 semantic routines: do public side of the definition
716 foreach (Dsymbol s; members) 805 foreach (Dsymbol s; members)
717 { 806 {
718 //writef("\tModule('%s'): '%s'.semantic()\n", toChars(), s.toChars()); 807 //writef("\tModule('%s'): '%s'.semantic()\n", toChars(), s.toChars());
719 s.semantic(sc); 808 s.semantic(sc);
720 runDeferredSemantic(); 809 runDeferredSemantic();
721 } 810 }
722 811
723 sc = sc.pop(); 812 if (!scope_)
724 sc.pop(); // 2 pops because Scope.createGlobal() created 2 813 {
814 sc = sc.pop();
815 sc.pop(); // 2 pops because Scope.createGlobal() created 2
816 }
725 semanticRun = semanticstarted; 817 semanticRun = semanticstarted;
726 //printf("-Module.semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent); 818 //printf("-Module.semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
727 } 819 }
728 820
729 void semantic2() // pass 2 semantic analysis 821 void semantic2() // pass 2 semantic analysis
786 semanticRun = semanticstarted; 878 semanticRun = semanticstarted;
787 } 879 }
788 880
789 override void inlineScan() // scan for functions to inline 881 override void inlineScan() // scan for functions to inline
790 { 882 {
791 int i;
792
793 if (semanticstarted >= 4) 883 if (semanticstarted >= 4)
794 return; 884 return;
795 885
796 assert(semanticstarted == 3); 886 assert(semanticstarted == 3);
797 semanticstarted = 4; 887 semanticstarted = 4;
1142 1232
1143 override Dsymbol search(Loc loc, Identifier ident, int flags) 1233 override Dsymbol search(Loc loc, Identifier ident, int flags)
1144 { 1234 {
1145 /* Since modules can be circularly referenced, 1235 /* Since modules can be circularly referenced,
1146 * need to stop infinite recursive searches. 1236 * need to stop infinite recursive searches.
1237 * This is done with the cache.
1147 */ 1238 */
1148 1239
1149 //printf("%s Module.search('%s', flags = %d) insearch = %d\n", toChars(), ident.toChars(), flags, insearch); 1240 //printf("%s Module.search('%s', flags = %d) insearch = %d\n", toChars(), ident.toChars(), flags, insearch);
1150 Dsymbol s; 1241 Dsymbol s;
1151 if (insearch) 1242 if (insearch)
1152 s = null; 1243 s = null;
1153 else if (searchCacheIdent == ident && searchCacheFlags == flags && searchCacheSymbol) 1244 else if (searchCacheIdent == ident && searchCacheFlags == flags)
1154 { 1245 {
1155 s = searchCacheSymbol; 1246 s = searchCacheSymbol;
1156 //printf("%s Module.search('%s', flags = %d) insearch = %d searchCacheSymbol = %s\n", toChars(), ident.toChars(), flags, insearch, searchCacheSymbol ? searchCacheSymbol.toChars() : "null"); 1247 //printf("%s Module.search('%s', flags = %d) insearch = %d searchCacheSymbol = %s\n", toChars(), ident.toChars(), flags, insearch, searchCacheSymbol ? searchCacheSymbol.toChars() : "null");
1157 } 1248 }
1158 else 1249 else
1174 objfile.remove(); 1265 objfile.remove();
1175 if (docfile) 1266 if (docfile)
1176 docfile.remove(); 1267 docfile.remove();
1177 } 1268 }
1178 1269
1270 override Dsymbol symtabInsert(Dsymbol s)
1271 {
1272 searchCacheIdent = null; // symbol is inserted, so invalidate cache
1273 return Package.symtabInsert(s);
1274 }
1275
1179 /******************************************* 1276 /*******************************************
1180 * Can't run semantic on s now, try again later. 1277 * Can't run semantic on s now, try again later.
1181 */ 1278 */
1182 void addDeferredSemantic(Dsymbol s) 1279 void addDeferredSemantic(Dsymbol s)
1183 { 1280 {
1188 1285
1189 if (sd == s) 1286 if (sd == s)
1190 return; 1287 return;
1191 } 1288 }
1192 1289
1193 //printf("Module::addDeferredSemantic('%s')\n", s->toChars()); 1290 //printf("Module::addDeferredSemantic('%s')\n", s.toChars());
1194 deferred.push(cast(void*)s); 1291 deferred.push(cast(void*)s);
1195 } 1292 }
1196 1293
1197 /****************************************** 1294 /******************************************
1198 * Run semantic() on deferred symbols. 1295 * Run semantic() on deferred symbols.