xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/d/dmd/module.h (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1 
2 /* Compiler implementation of the D programming language
3  * Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved
4  * written by Walter Bright
5  * http://www.digitalmars.com
6  * Distributed under the Boost Software License, Version 1.0.
7  * http://www.boost.org/LICENSE_1_0.txt
8  * https://github.com/dlang/dmd/blob/master/src/dmd/module.h
9  */
10 
11 #pragma once
12 
13 #include "root/root.h"
14 #include "dsymbol.h"
15 
16 class ClassDeclaration;
17 struct ModuleDeclaration;
18 struct Macro;
19 struct Escape;
20 class VarDeclaration;
21 class Library;
22 
23 enum PKG
24 {
25     PKGunknown, // not yet determined whether it's a package.d or not
26     PKGmodule,  // already determined that's an actual package.d
27     PKGpackage  // already determined that's an actual package
28 };
29 
30 class Package : public ScopeDsymbol
31 {
32 public:
33     PKG isPkgMod;
34     unsigned tag;       // auto incremented tag, used to mask package tree in scopes
35     Module *mod;        // != NULL if isPkgMod == PKGmodule
36 
37     Package(Identifier *ident);
38     const char *kind() const;
39 
40     static DsymbolTable *resolve(Identifiers *packages, Dsymbol **pparent, Package **ppkg);
41 
isPackage()42     Package *isPackage() { return this; }
43 
44     bool isAncestorPackageOf(const Package * const pkg) const;
45 
46     void semantic(Scope *);
47     Dsymbol *search(const Loc &loc, Identifier *ident, int flags = SearchLocalsOnly);
accept(Visitor * v)48     void accept(Visitor *v) { v->visit(this); }
49 
50     Module *isPackageMod();
51 };
52 
53 class Module : public Package
54 {
55 public:
56     static Module *rootModule;
57     static DsymbolTable *modules;       // symbol table of all modules
58     static Modules amodules;            // array of all modules
59     static Dsymbols deferred;   // deferred Dsymbol's needing semantic() run on them
60     static Dsymbols deferred2;  // deferred Dsymbol's needing semantic2() run on them
61     static Dsymbols deferred3;  // deferred Dsymbol's needing semantic3() run on them
62     static unsigned dprogress;  // progress resolving the deferred list
63     static void _init();
64 
65     static AggregateDeclaration *moduleinfo;
66 
67 
68     const char *arg;    // original argument name
69     ModuleDeclaration *md; // if !NULL, the contents of the ModuleDeclaration declaration
70     File *srcfile;      // input source file
71     const char* srcfilePath; // the path prefix to the srcfile if it applies
72     File *objfile;      // output .obj file
73     File *hdrfile;      // 'header' file
74     File *docfile;      // output documentation file
75     unsigned errors;    // if any errors in file
76     unsigned numlines;  // number of lines in source file
77     int isDocFile;      // if it is a documentation input file, not D source
78     bool isPackageFile; // if it is a package.d
79     Strings contentImportedFiles;  // array of files whose content was imported
80     int needmoduleinfo;
81 
82     int selfimports;            // 0: don't know, 1: does not, 2: does
83     bool selfImports();         // returns true if module imports itself
84 
85     int rootimports;            // 0: don't know, 1: does not, 2: does
86     bool rootImports();         // returns true if module imports root module
87 
88     int insearch;
89     Identifier *searchCacheIdent;
90     Dsymbol *searchCacheSymbol; // cached value of search
91     int searchCacheFlags;       // cached flags
92 
93     // module from command line we're imported from,
94     // i.e. a module that will be taken all the
95     // way to an object file
96     Module *importedFrom;
97 
98     Dsymbols *decldefs;         // top level declarations for this Module
99 
100     Modules aimports;             // all imported modules
101 
102     unsigned debuglevel;        // debug level
103     Strings *debugids;      // debug identifiers
104     Strings *debugidsNot;       // forward referenced debug identifiers
105 
106     unsigned versionlevel;      // version level
107     Strings *versionids;    // version identifiers
108     Strings *versionidsNot;     // forward referenced version identifiers
109 
110     Macro *macrotable;          // document comment macros
111     Escape *escapetable;        // document comment escapes
112 
113     size_t nameoffset;          // offset of module name from start of ModuleInfo
114     size_t namelen;             // length of module name in characters
115 
116     Module(const char *arg, Identifier *ident, int doDocComment, int doHdrGen);
117     static Module* create(const char *arg, Identifier *ident, int doDocComment, int doHdrGen);
118 
119     static Module *load(Loc loc, Identifiers *packages, Identifier *ident);
120 
121     const char *kind() const;
122     File *setOutfile(const char *name, const char *dir, const char *arg, const char *ext);
123     void setDocfile();
124     bool read(Loc loc); // read file, returns 'true' if succeed, 'false' otherwise.
125     Module *parse();    // syntactic parse
126     void importAll(Scope *sc);
127     void semantic(Scope *);    // semantic analysis
128     void semantic2(Scope *);   // pass 2 semantic analysis
129     void semantic3(Scope *);   // pass 3 semantic analysis
130     int needModuleInfo();
131     Dsymbol *search(const Loc &loc, Identifier *ident, int flags = SearchLocalsOnly);
132     bool isPackageAccessible(Package *p, Prot protection, int flags = 0);
133     Dsymbol *symtabInsert(Dsymbol *s);
134     void deleteObjFile();
135     static void addDeferredSemantic(Dsymbol *s);
136     static void addDeferredSemantic2(Dsymbol *s);
137     static void addDeferredSemantic3(Dsymbol *s);
138     static void runDeferredSemantic();
139     static void runDeferredSemantic2();
140     static void runDeferredSemantic3();
141     static void clearCache();
142     int imports(Module *m);
143 
isRoot()144     bool isRoot() { return this->importedFrom == this; }
145     // true if the module source file is directly
146     // listed in command line.
147     bool isCoreModule(Identifier *ident);
148 
149     // Back end
150 
151     int doppelganger;           // sub-module
152     Symbol *cov;                // private uint[] __coverage;
153     unsigned *covb;             // bit array of valid code line numbers
154 
155     Symbol *sictor;             // module order independent constructor
156     Symbol *sctor;              // module constructor
157     Symbol *sdtor;              // module destructor
158     Symbol *ssharedctor;        // module shared constructor
159     Symbol *sshareddtor;        // module shared destructor
160     Symbol *stest;              // module unit test
161 
162     Symbol *sfilename;          // symbol for filename
163 
isModule()164     Module *isModule() { return this; }
accept(Visitor * v)165     void accept(Visitor *v) { v->visit(this); }
166 };
167 
168 
169 struct ModuleDeclaration
170 {
171     Loc loc;
172     Identifier *id;
173     Identifiers *packages;            // array of Identifier's representing packages
174     bool isdeprecated;  // if it is a deprecated module
175     Expression *msg;
176 
177     ModuleDeclaration(Loc loc, Identifiers *packages, Identifier *id);
178 
179     const char *toChars();
180 };
181