|
1 | 1 | import { Target } from "./compiler"; |
2 | 2 | import { GETTER_PREFIX, SETTER_PREFIX, PATH_DELIMITER } from "./constants"; |
3 | | -import { DiagnosticCode, DiagnosticMessage, DiagnosticEmitter } from "./diagnostics"; |
| 3 | +import { DiagnosticCode, DiagnosticMessage, DiagnosticEmitter, DiagnosticCategory } from "./diagnostics"; |
4 | 4 | import { Type, typesToString } from "./types"; |
5 | 5 | import { I64 } from "./util"; |
6 | 6 | import { |
@@ -40,19 +40,20 @@ import { |
40 | 40 | VariableDeclaration, |
41 | 41 | VariableStatement, |
42 | 42 |
|
43 | | - hasModifier |
| 43 | + hasModifier, |
| 44 | + mangleInternalName |
44 | 45 |
|
45 | 46 | } from "./ast"; |
46 | 47 |
|
47 | 48 | class QueuedExport { |
48 | | - isForeign: bool; |
| 49 | + isReExport: bool; |
49 | 50 | referencedName: string; |
50 | 51 | member: ExportMember; |
51 | 52 | } |
52 | 53 |
|
53 | 54 | class QueuedImport { |
54 | 55 | internalName: string; |
55 | | - importName: string; |
| 56 | + referencedName: string; |
56 | 57 | declaration: ImportDeclaration; |
57 | 58 | } |
58 | 59 |
|
@@ -130,61 +131,63 @@ export class Program extends DiagnosticEmitter { |
130 | 131 | } |
131 | 132 | } |
132 | 133 |
|
133 | | - // at this point queued exports should be resolvable |
134 | | - for (let [exportName, queuedExport] of queuedExports) { // all file-level exports |
135 | | - if (queuedExport.isForeign) { |
136 | | - const seen: Set<QueuedExport> = new Set(); |
137 | | - while (queuedExports.has(queuedExport.referencedName)) { |
138 | | - queuedExport = <QueuedExport>queuedExports.get(queuedExport.referencedName); |
139 | | - if (seen.has(queuedExport)) |
140 | | - break; |
141 | | - seen.add(queuedExport); |
142 | | - } |
143 | | - if (this.exports.has(queuedExport.referencedName)) { |
144 | | - const element: Element = <Element>this.exports.get(queuedExport.referencedName); |
145 | | - if (!this.exports.has(exportName)) |
146 | | - this.exports.set(exportName, element); |
147 | | - if (queuedExport.member.range.source.isEntry) |
148 | | - element.globalExportName = queuedExport.member.externalIdentifier.name; |
149 | | - } else |
150 | | - this.error(DiagnosticCode.Cannot_find_name_0, queuedExport.member.externalIdentifier.range, queuedExport.referencedName); |
151 | | - } else /* local */ { |
152 | | - if (this.elements.has(queuedExport.referencedName)) { |
153 | | - const element: Element = <Element>this.elements.get(queuedExport.referencedName); |
154 | | - if (!this.exports.has(exportName)) |
155 | | - this.exports.set(exportName, element); |
156 | | - if (queuedExport.member.range.source.isEntry) |
157 | | - element.globalExportName = queuedExport.member.externalIdentifier.name; |
158 | | - } else |
159 | | - this.error(DiagnosticCode.Cannot_find_name_0, queuedExport.member.externalIdentifier.range, queuedExport.referencedName); |
| 134 | + let element: Element | null; |
| 135 | + |
| 136 | + // queued imports should be resolvable now |
| 137 | + for (let i: i32 = 0; i < queuedImports.length;) { |
| 138 | + const queuedImport: QueuedImport = queuedImports[i]; |
| 139 | + element = this.tryResolveImport(queuedImport.referencedName, queuedExports); |
| 140 | + if (element) { |
| 141 | + this.elements.set(queuedImport.internalName, element); |
| 142 | + queuedImports.splice(i, 1); |
| 143 | + } else { |
| 144 | + this.error(DiagnosticCode.Module_0_has_no_exported_member_1, queuedImport.declaration.range, (<ImportStatement>queuedImport.declaration.parent).path.value, queuedImport.declaration.externalIdentifier.name); |
| 145 | + ++i; |
160 | 146 | } |
161 | 147 | } |
162 | 148 |
|
163 | | - // at this point queued imports should be resolvable as well |
164 | | - for (let i: i32 = 0, k: i32 = queuedImports.length; i < k; ++i) { |
165 | | - const queuedImport: QueuedImport = queuedImports[i]; |
166 | | - const internalName: string = queuedImport.internalName; |
167 | | - const seen: Set<QueuedExport> = new Set(); |
168 | | - let importName: string = queuedImport.importName; |
169 | | - while (queuedExports.has(importName)) { |
170 | | - const queuedExport: QueuedExport = <QueuedExport>queuedExports.get(importName); |
171 | | - importName = queuedExport.referencedName; |
172 | | - if (seen.has(queuedExport)) |
| 149 | + // queued exports should be resolvable noww |
| 150 | + for (let [exportName, queuedExport] of queuedExports) { |
| 151 | + let currentExport: QueuedExport | null = queuedExport; |
| 152 | + do { |
| 153 | + if (currentExport.isReExport) { |
| 154 | + element = <Element | null>this.exports.get(currentExport.referencedName); |
| 155 | + if (element) { |
| 156 | + this.exports.set(exportName, element); |
| 157 | + break; |
| 158 | + } |
| 159 | + currentExport = <QueuedExport | null>queuedExports.get(currentExport.referencedName); |
| 160 | + if (!currentExport) |
| 161 | + this.error(DiagnosticCode.Module_0_has_no_exported_member_1, queuedExport.member.externalIdentifier.range, (<StringLiteralExpression>(<ExportStatement>queuedExport.member.parent).path).value, queuedExport.member.externalIdentifier.name); |
| 162 | + } else { |
| 163 | + element = <Element | null>this.elements.get(currentExport.referencedName); |
| 164 | + if (element) |
| 165 | + this.exports.set(exportName, element); |
| 166 | + else |
| 167 | + this.error(DiagnosticCode.Cannot_find_name_0, queuedExport.member.range, queuedExport.member.identifier.name); |
173 | 168 | break; |
174 | | - seen.add(queuedExport); |
175 | | - } |
176 | | - if (this.exports.has(importName)) { |
177 | | - if (this.elements.has(internalName)) |
178 | | - this.error(DiagnosticCode.Duplicate_identifier_0, queuedImport.declaration.identifier.range, internalName); |
179 | | - else { |
180 | | - const element: Element = <Element>this.exports.get(importName); |
181 | | - this.elements.set(internalName, element); |
182 | 169 | } |
183 | | - } else |
184 | | - this.error(DiagnosticCode.Cannot_find_name_0, queuedImport.declaration.externalIdentifier.range, importName); |
| 170 | + } while (currentExport); |
185 | 171 | } |
186 | 172 | } |
187 | 173 |
|
| 174 | + private tryResolveImport(referencedName: string, queuedExports: Map<string,QueuedExport>): Element | null { |
| 175 | + let element: Element | null; |
| 176 | + do { |
| 177 | + element = <Element | null>this.exports.get(referencedName); |
| 178 | + if (element) |
| 179 | + return element; |
| 180 | + const queuedExport: QueuedExport | null = <QueuedExport | null>queuedExports.get(referencedName); |
| 181 | + if (!queuedExport) |
| 182 | + return null; |
| 183 | + if (queuedExport.isReExport) { |
| 184 | + referencedName = queuedExport.referencedName; |
| 185 | + continue; |
| 186 | + } |
| 187 | + return <Element | null>this.elements.get(queuedExport.referencedName); |
| 188 | + } while (true); |
| 189 | + } |
| 190 | + |
188 | 191 | private initializeClass(declaration: ClassDeclaration): void { |
189 | 192 | const internalName: string = declaration.internalName; |
190 | 193 | if (this.elements.has(internalName)) { |
@@ -288,21 +291,79 @@ export class Program extends DiagnosticEmitter { |
288 | 291 | } |
289 | 292 |
|
290 | 293 | private initializeExport(member: ExportMember, internalPath: string | null, queuedExports: Map<string,QueuedExport>): void { |
291 | | - const exportName: string = member.range.source.internalPath + PATH_DELIMITER + member.externalIdentifier.name; |
292 | | - if (queuedExports.has(exportName)) { |
293 | | - this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, member.externalIdentifier.range, exportName); |
| 294 | + const externalName: string = member.range.source.internalPath + PATH_DELIMITER + member.externalIdentifier.name; |
| 295 | + |
| 296 | + if (this.exports.has(externalName)) { |
| 297 | + this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, member.externalIdentifier.range, externalName); |
294 | 298 | return; |
295 | 299 | } |
296 | | - const queuedExport: QueuedExport = new QueuedExport(); |
| 300 | + |
| 301 | + let referencedName: string; |
| 302 | + |
| 303 | + // export local element |
297 | 304 | if (internalPath == null) { |
298 | | - queuedExport.isForeign = false; |
299 | | - queuedExport.referencedName = member.range.source.internalPath + PATH_DELIMITER + member.identifier.name; |
| 305 | + referencedName = member.range.source.internalPath + PATH_DELIMITER + member.identifier.name; |
| 306 | + |
| 307 | + // resolve right away if the element exists |
| 308 | + if (this.elements.has(referencedName)) { |
| 309 | + this.exports.set(externalName, <Element>this.elements.get(referencedName)); |
| 310 | + return; |
| 311 | + } |
| 312 | + |
| 313 | + // otherwise queue it |
| 314 | + if (queuedExports.has(externalName)) { |
| 315 | + this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, member.externalIdentifier.range, externalName); |
| 316 | + return; |
| 317 | + } |
| 318 | + const queuedExport: QueuedExport = new QueuedExport(); |
| 319 | + queuedExport.isReExport = false; |
| 320 | + queuedExport.referencedName = referencedName; // -> internal name |
| 321 | + queuedExport.member = member; |
| 322 | + queuedExports.set(externalName, queuedExport); |
| 323 | + |
| 324 | + // export external element |
300 | 325 | } else { |
301 | | - queuedExport.isForeign = true; |
302 | | - queuedExport.referencedName = (<string>internalPath) + PATH_DELIMITER + member.identifier.name; |
| 326 | + referencedName = (<string>internalPath) + PATH_DELIMITER + member.externalIdentifier.name; |
| 327 | + |
| 328 | + // resolve right away if the export exists |
| 329 | + if (this.exports.has(referencedName)) { |
| 330 | + this.exports.set(externalName, <Element>this.exports.get(referencedName)); |
| 331 | + return; |
| 332 | + } |
| 333 | + |
| 334 | + // walk already known queued exports |
| 335 | + const seen: Set<QueuedExport> = new Set(); |
| 336 | + while (queuedExports.has(referencedName)) { |
| 337 | + const queuedExport: QueuedExport = <QueuedExport>queuedExports.get(referencedName); |
| 338 | + if (queuedExport.isReExport) { |
| 339 | + if (this.exports.has(queuedExport.referencedName)) { |
| 340 | + this.exports.set(externalName, <Element>this.exports.get(referencedName)); |
| 341 | + return; |
| 342 | + } |
| 343 | + referencedName = queuedExport.referencedName; |
| 344 | + if (seen.has(queuedExport)) |
| 345 | + break; |
| 346 | + seen.add(queuedExport); |
| 347 | + } else { |
| 348 | + if (this.elements.has(queuedExport.referencedName)) { |
| 349 | + this.exports.set(externalName, <Element>this.elements.get(referencedName)); |
| 350 | + return; |
| 351 | + } |
| 352 | + break; |
| 353 | + } |
| 354 | + } |
| 355 | + |
| 356 | + // otherwise queue it |
| 357 | + if (queuedExports.has(externalName)) { |
| 358 | + this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, member.externalIdentifier.range, externalName); |
| 359 | + return; |
| 360 | + } |
| 361 | + const queuedReExport: QueuedExport = new QueuedExport(); |
| 362 | + queuedReExport.isReExport = true; |
| 363 | + queuedReExport.referencedName = referencedName; // -> export name |
| 364 | + queuedReExport.member = member; |
| 365 | + queuedExports.set(externalName, queuedReExport); |
303 | 366 | } |
304 | | - queuedExport.member = member; |
305 | | - queuedExports.set(exportName, queuedExport); |
306 | 367 | } |
307 | 368 |
|
308 | 369 | private initializeFunction(declaration: FunctionDeclaration): void { |
@@ -330,29 +391,48 @@ export class Program extends DiagnosticEmitter { |
330 | 391 | } |
331 | 392 |
|
332 | 393 | private initializeImport(declaration: ImportDeclaration, internalPath: string, queuedExports: Map<string,QueuedExport>, queuedImports: QueuedImport[]): void { |
333 | | - const importName: string = internalPath + PATH_DELIMITER + declaration.externalIdentifier.name; |
334 | | - let resolvedImportName: string = importName; |
| 394 | + const internalName: string = declaration.internalName; |
| 395 | + if (this.elements.has(internalName)) { |
| 396 | + this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName); |
| 397 | + return; |
| 398 | + } |
| 399 | + |
| 400 | + let referencedName: string = internalPath + PATH_DELIMITER + declaration.externalIdentifier.name; |
| 401 | + |
| 402 | + // resolve right away if the export exists |
| 403 | + if (this.exports.has(referencedName)) { |
| 404 | + this.elements.set(internalName, <Element>this.exports.get(referencedName)); |
| 405 | + return; |
| 406 | + } |
| 407 | + |
| 408 | + // walk already known queued exports |
335 | 409 | const seen: Set<QueuedExport> = new Set(); |
336 | | - while (queuedExports.has(resolvedImportName)) { |
337 | | - const queuedExport: QueuedExport = <QueuedExport>queuedExports.get(resolvedImportName); |
338 | | - resolvedImportName = queuedExport.referencedName; |
339 | | - if (seen.has(queuedExport)) |
| 410 | + while (queuedExports.has(referencedName)) { |
| 411 | + const queuedExport: QueuedExport = <QueuedExport>queuedExports.get(referencedName); |
| 412 | + if (queuedExport.isReExport) { |
| 413 | + if (this.exports.has(queuedExport.referencedName)) { |
| 414 | + this.elements.set(internalName, <Element>this.exports.get(referencedName)); |
| 415 | + return; |
| 416 | + } |
| 417 | + referencedName = queuedExport.referencedName; |
| 418 | + if (seen.has(queuedExport)) |
| 419 | + break; |
| 420 | + seen.add(queuedExport); |
| 421 | + } else { |
| 422 | + if (this.elements.has(queuedExport.referencedName)) { |
| 423 | + this.elements.set(internalName, <Element>this.elements.get(referencedName)); |
| 424 | + return; |
| 425 | + } |
340 | 426 | break; |
341 | | - seen.add(queuedExport); |
342 | | - } |
343 | | - const internalName: string = declaration.internalName; |
344 | | - if (this.exports.has(resolvedImportName)) { // resolvable right away |
345 | | - if (this.elements.has(internalName)) |
346 | | - this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName); |
347 | | - else |
348 | | - this.elements.set(internalName, <Element>this.exports.get(resolvedImportName)); |
349 | | - } else { // points to yet unresolved export |
350 | | - const queuedImport: QueuedImport = new QueuedImport(); |
351 | | - queuedImport.internalName = internalName; |
352 | | - queuedImport.importName = importName; |
353 | | - queuedImport.declaration = declaration; |
354 | | - queuedImports.push(queuedImport); |
| 427 | + } |
355 | 428 | } |
| 429 | + |
| 430 | + // otherwise queue it |
| 431 | + const queuedImport: QueuedImport = new QueuedImport(); |
| 432 | + queuedImport.internalName = internalName; |
| 433 | + queuedImport.referencedName = referencedName; |
| 434 | + queuedImport.declaration = declaration; |
| 435 | + queuedImports.push(queuedImport); |
356 | 436 | } |
357 | 437 |
|
358 | 438 | private initializeInterface(declaration: InterfaceDeclaration): void { |
|
0 commit comments