@@ -498,52 +498,105 @@ private __gshared StringTable!(Symbol*) *stringTab;
498
498
/* ********************************************
499
499
* Figure out whether a data symbol should be dllimported
500
500
* Params:
501
- * var = declaration of the symbol
501
+ * symbl = declaration of the symbol
502
502
* Returns:
503
503
* true if symbol should be imported from a DLL
504
504
*/
505
- bool isDllImported (Dsymbol var )
505
+ bool isDllImported (Dsymbol symbl )
506
506
{
507
+ // Windows is the only platform which dmd supports, that uses the DllImport/DllExport scheme.
507
508
if (! (target.os & Target.OS .Windows))
508
509
return false ;
509
- if (var.isImportedSymbol())
510
+
511
+ // If function does not have a body, check to see if its marked as DllImport or is set to be exported.
512
+ // If a global variable has both export + extern, it is DllImport
513
+ if (symbl.isImportedSymbol())
510
514
return true ;
511
- if (driverParams.symImport == SymImport.none)
512
- return false ;
513
- if (auto vd = var.isDeclaration())
515
+
516
+ // Functions can go through the generated trampoline function.
517
+ // Not efficient, but it works.
518
+ if (symbl.isFuncDeclaration())
519
+ return false ; // can always jump through import table
520
+
521
+ // Global variables are allowed, but not TLS or read only memory.
522
+ if (auto vd = symbl.isDeclaration())
523
+ {
514
524
if (! vd.isDataseg() || vd.isThreadlocal())
515
525
return false ;
516
- if (var.isFuncDeclaration())
517
- return false ; // can always jump through import table
518
- if ( auto tid = var.isTypeInfoDeclaration() )
526
+ }
527
+
528
+ final switch (driverParams.symImport )
519
529
{
530
+ case SymImport.none:
531
+ // If DllImport overriding is disabled, do not change dllimport status.
532
+ return false ;
533
+
534
+ case SymImport.defaultLibsOnly:
535
+ case SymImport.all:
536
+ // If to access anything in druntime/phobos you need DllImport, verify against this.
537
+ break ;
538
+ }
539
+ const systemLibraryNeedDllImport = true ;
540
+
541
+ // For TypeInfo's check to see if its in druntime and DllImport it
542
+ if (auto tid = symbl.isTypeInfoDeclaration())
543
+ {
544
+ // Built in TypeInfo's are defined in druntime
520
545
if (builtinTypeInfo(tid.tinfo))
521
- return true ;
546
+ return systemLibraryNeedDllImport;
547
+
548
+ // Convert TypeInfo to its symbol
522
549
if (auto ad = isAggregate(tid.type))
523
- var = ad;
550
+ symbl = ad;
524
551
}
525
- if (driverParams.symImport == SymImport.defaultLibsOnly)
552
+
526
553
{
527
- auto m = var.getModule();
554
+ // Filter the symbol based upon the module it is in.
555
+
556
+ auto m = symbl.getModule();
528
557
if (! m || ! m.md)
529
558
return false ;
530
- const id = m.md.packages.length ? m.md.packages[0 ] : null ;
531
- if (id && id != Id.core && id != Id.std)
532
- return false ;
533
- if (! id && m.md.id != Id.std && m.md.id != Id.object)
534
- return false ;
559
+
560
+ if (driverParams.symImport == SymImport.all || m.isExplicitlyOutOfBinary)
561
+ {
562
+ // If a module is specified as being out of binary (-extI), then it is allowed to be DllImport.
563
+ }
564
+ else if (systemLibraryNeedDllImport)
565
+ {
566
+ // Filter out all modules that are not in druntime/phobos if we are only doing default libs only
567
+
568
+ const id = m.md.packages.length ? m.md.packages[0 ] : null ;
569
+ if (id && id != Id.core && id != Id.std)
570
+ return false ;
571
+ if (! id && m.md.id != Id.std && m.md.id != Id.object)
572
+ return false ;
573
+ }
535
574
}
536
- else if (driverParams.symImport != SymImport.all)
537
- return false ;
538
- if (auto mod = var.isModule())
539
- return ! mod.isRoot(); // non-root ModuleInfo symbol
540
- if (var.inNonRoot())
575
+
576
+ // If symbol is a ModuleInfo, check to see if module is being compiled.
577
+ if (auto mod = symbl.isModule())
578
+ {
579
+ const isBeingCompiled = mod.isRoot();
580
+ return ! isBeingCompiled; // non-root ModuleInfo symbol
581
+ }
582
+
583
+ // Check to see if a template has been instatiated in current compilation,
584
+ // if it is defined in a external module, its DllImport.
585
+ if (symbl.inNonRoot())
541
586
return true ; // not instantiated, and defined in non-root
542
- if (auto ti = var.isInstantiated()) // && !defineOnDeclare(sym, false))
587
+
588
+ // If a template has been instatiated, only DllImport if it is codegen'ing
589
+ if (auto ti = symbl.isInstantiated()) // && !defineOnDeclare(sym, false))
543
590
return ! ti.needsCodegen(); // instantiated but potentially culled (needsCodegen())
544
- if (auto vd = var.isVarDeclaration())
591
+
592
+ // If a variable declaration and is extern
593
+ if (auto vd = symbl.isVarDeclaration())
594
+ {
595
+ // Shouldn't this be including an export check too???
545
596
if (vd.storage_class & STC .extern_)
546
597
return true ; // externally defined global variable
598
+ }
599
+
547
600
return false ;
548
601
}
549
602
0 commit comments