1313#ifndef SWIFT_AST_SOURCEFILE_H
1414#define SWIFT_AST_SOURCEFILE_H
1515
16+ #include " swift/AST/ASTNode.h"
1617#include " swift/AST/FileUnit.h"
1718#include " swift/AST/Import.h"
1819#include " swift/AST/SynthesizedFileUnit.h"
1920#include " swift/Basic/Debug.h"
2021#include " llvm/ADT/Hashing.h"
2122#include " llvm/ADT/SetVector.h"
2223#include " llvm/ADT/SmallPtrSet.h"
24+ #include " llvm/ADT/STLExtras.h"
2325
2426namespace swift {
2527
@@ -151,11 +153,11 @@ class SourceFile final : public FileUnit {
151153 // / been validated.
152154 llvm::SetVector<ValueDecl *> UnvalidatedDeclsWithOpaqueReturnTypes;
153155
154- // / The list of top-level declarations in the source file. This is \c None if
156+ // / The list of top-level items in the source file. This is \c None if
155157 // / they have not yet been parsed.
156158 // / FIXME: Once addTopLevelDecl/prependTopLevelDecl
157159 // / have been removed, this can become an optional ArrayRef.
158- Optional<std::vector<Decl * >> Decls ;
160+ Optional<std::vector<ASTNode >> Items ;
159161
160162 // / The list of hoisted declarations. See Decl::isHoisted().
161163 // / This is only used by lldb.
@@ -195,42 +197,48 @@ class SourceFile final : public FileUnit {
195197 friend ASTContext;
196198
197199public:
200+ // / For source files created to hold the source code created by expanding
201+ // / a macro, this is the AST node that describes the macro expansion.
202+ // /
203+ // / The source location of this AST node is the place in the source that
204+ // / triggered the creation of the macro expansion whose resulting source
205+ // / code is in this source file. This field is only valid when
206+ // / the \c SourceFileKind is \c MacroExpansion.
207+ const ASTNode macroExpansion;
208+
198209 // / Appends the given declaration to the end of the top-level decls list. Do
199210 // / not add any additional uses of this function.
200- void addTopLevelDecl (Decl *d) {
201- // Force decl parsing if we haven't already.
202- (void )getTopLevelDecls ();
203- Decls->push_back (d);
204- }
211+ void addTopLevelDecl (Decl *d);
205212
206213 // / Prepends a declaration to the top-level decls list.
207214 // /
208215 // / FIXME: This entrypoint exists to support LLDB. Calls to this function are
209216 // / always a mistake, and additional uses should not be added.
210217 // /
211218 // / See rdar://58355191
212- void prependTopLevelDecl (Decl *d) {
213- // Force decl parsing if we haven't already.
214- (void )getTopLevelDecls ();
215- Decls->insert (Decls->begin (), d);
216- }
219+ void prependTopLevelDecl (Decl *d);
217220
218221 // / Add a hoisted declaration. See Decl::isHoisted().
219222 void addHoistedDecl (Decl *d);
220223
224+ // / Retrieves an immutable view of the list of top-level items in this file.
225+ ArrayRef<ASTNode> getTopLevelItems () const ;
226+
221227 // / Retrieves an immutable view of the list of top-level decls in this file.
228+ // /
229+ // / NOTE: Please use getTopLevelItems() instead.
222230 ArrayRef<Decl *> getTopLevelDecls () const ;
223231
224232 // / Retrieves an immutable view of the list of hoisted decls in this file.
225233 // / See Decl::isHoisted().
226234 ArrayRef<Decl *> getHoistedDecls () const ;
227235
228- // / Retrieves an immutable view of the top-level decls if they have already
236+ // / Retrieves an immutable view of the top-level items if they have already
229237 // / been parsed, or \c None if they haven't. Should only be used for dumping.
230- Optional<ArrayRef<Decl * >> getCachedTopLevelDecls () const {
231- if (!Decls )
238+ Optional<ArrayRef<ASTNode >> getCachedTopLevelItems () const {
239+ if (!Items )
232240 return None;
233- return llvm::makeArrayRef (*Decls );
241+ return llvm::makeArrayRef (*Items );
234242 }
235243
236244 // / Retrieve the parsing options for the file.
@@ -326,7 +334,8 @@ class SourceFile final : public FileUnit {
326334 llvm::StringMap<SourceFilePathInfo> getInfoForUsedFilePaths () const ;
327335
328336 SourceFile (ModuleDecl &M, SourceFileKind K, Optional<unsigned > bufferID,
329- ParsingOptions parsingOpts = {}, bool isPrimary = false );
337+ ParsingOptions parsingOpts = {}, bool isPrimary = false ,
338+ ASTNode macroExpansion = ASTNode());
330339
331340 ~SourceFile ();
332341
@@ -487,6 +496,11 @@ class SourceFile final : public FileUnit {
487496 return BufferID;
488497 }
489498
499+ // / When this source file is enclosed within another source file, for example
500+ // / because it describes a macro expansion, return the source file it was
501+ // / enclosed in.
502+ SourceFile *getEnclosingSourceFile () const ;
503+
490504 // / If this buffer corresponds to a file on disk, returns the path.
491505 // / Otherwise, return an empty string.
492506 StringRef getFilename () const ;
@@ -506,7 +520,7 @@ class SourceFile final : public FileUnit {
506520 // FIXME: Ideally the parser state should be an output of
507521 // ParseSourceFileRequest, but the evaluator doesn't currently support
508522 // move-only outputs for cached requests.
509- (void )getTopLevelDecls ();
523+ (void )getTopLevelItems ();
510524
511525 auto *state = DelayedParserState.get ();
512526 assert (state && " Didn't set any delayed parser state!" );
@@ -545,6 +559,7 @@ class SourceFile final : public FileUnit {
545559 case SourceFileKind::Library:
546560 case SourceFileKind::Interface:
547561 case SourceFileKind::SIL:
562+ case SourceFileKind::MacroExpansion:
548563 return false ;
549564 }
550565 llvm_unreachable (" bad SourceFileKind" );
0 commit comments