Rename AST dir to Compiler.

This commit is contained in:
ykiko
2024-10-01 20:30:33 +08:00
parent 796ace1925
commit 7d15263c8f
16 changed files with 25 additions and 15 deletions

View File

@@ -0,0 +1,19 @@
#pragma once
#include <Support/ADT.h>
#include <clang/Frontend/CompilerInstance.h>
namespace clice {
// TODO:
class Preamble;
std::unique_ptr<clang::CompilerInvocation> createInvocation(StringRef filename,
StringRef content,
std::vector<const char*>& args,
Preamble* preamble = nullptr);
std::unique_ptr<clang::CompilerInstance> createInstance(std::shared_ptr<clang::CompilerInvocation> invocation);
} // namespace clice

View File

@@ -0,0 +1,25 @@
#pragma once
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Basic/Diagnostic.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendActions.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Sema/Sema.h>
#include <clang/Tooling/CompilationDatabase.h>
#include <clang/Tooling/Syntax/Tokens.h>
#include <clang/Lex/PPCallbacks.h>
#include "clang/Sema/TemplateDeduction.h"
namespace clice {
class Diagnostic : public clang::DiagnosticConsumer {
public:
void BeginSourceFile(const clang::LangOptions& Opts, const clang::Preprocessor* PP) override;
void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic& Info) override;
void EndSourceFile() override;
};
} // namespace clice

View File

@@ -0,0 +1,47 @@
#include "Diagnostic.h"
namespace clice {
/// Represents a full conditional preprocessing block, from #if to #endif.
struct IfBlock {
/// Represents a single conditional branch (e.g., #if, #ifdef, #elif).
struct Branch {
/// Location of the directive.
/// NOTE: The `#` is a separate token and not necessarily adjacent.
clang::SourceLocation location;
/// Location of the condition expression.
clang::SourceLocation condition;
/// Evaluated value of the condition.
clang::PPCallbacks::ConditionValueKind value;
};
/// Initial #if, #ifdef, or #ifndef directive.
Branch if_;
/// #elif, #elifdef, or #elifndef directives.
std::vector<Branch> elifs;
/// Location of the #else directive, if present.
clang::SourceLocation elseLoc;
/// Location of the #endif directive.
clang::SourceLocation endifLoc;
};
struct Directive {
/// map from the location of if/ifdef/ifndef to the corresponding if block.
llvm::DenseMap<clang::SourceLocation, IfBlock> ifBlocks;
};
struct Directives {
clang::Preprocessor& preproc;
clang::SourceManager& sourceManager;
llvm::DenseMap<clang::FileID, Directive> x;
clang::CommentHandler* handler();
std::unique_ptr<clang::PPCallbacks> callback();
};
} // namespace clice

View File

@@ -0,0 +1,36 @@
#pragma once
#include "Preamble.h"
namespace clice {
struct ParsedAST {
clang::Sema& sema;
clang::ASTContext& context;
clang::Preprocessor& preproc;
clang::FileManager& fileManager;
clang::SourceManager& sourceManager;
clang::syntax::TokenBuffer tokenBuffer;
std::unique_ptr<Directives> directive;
std::unique_ptr<clang::FrontendAction> action;
std::unique_ptr<clang::CompilerInstance> instance;
static std::unique_ptr<ParsedAST> build(llvm::StringRef filename,
llvm::StringRef content,
std::vector<const char*>& args,
Preamble* preamble = nullptr);
clang::FileID getFileID(llvm::StringRef filename) const {
auto entry = fileManager.getFileRef(filename);
if(!entry) {
// TODO:
}
return sourceManager.translateFile(entry.get());
}
llvm::ArrayRef<clang::syntax::Token> spelledTokens(clang::FileID fileID) const {
return tokenBuffer.spelledTokens(fileID);
}
};
} // namespace clice

View File

@@ -0,0 +1,21 @@
#pragma once
#include "Directive.h"
namespace clice {
// FIXME: currently, we do not use preamble.
// NOTE: if preamble is used for a TU, the header tokens can not find in TokenBuffer.
/// Represents the preamble of a translation unit.
/// We build preamble for the translation after first edit.
/// The preamble is used to speed up the reparse of the translation unit.
struct Preamble {
clang::PrecompiledPreamble data;
static std::unique_ptr<Preamble> build(llvm::StringRef filename,
llvm::StringRef content,
std::vector<const char*>& args);
};
} // namespace clice

View File

@@ -0,0 +1,67 @@
#pragma once
#include "ParsedAST.h"
#include <clang/Sema/Lookup.h>
#include <clang/Sema/Template.h>
#include <stack>
namespace clice {
/// This class is used to resolve dependent names in the AST.
/// For dependent names, we cannot know the any information about the name until
/// the template is instantiated. This can be frustrating, you cannot get
/// completion, you cannot get go-to-definition, etc. To avoid this, we just use
/// some heuristics to simplify the dependent names as normal type/expression.
/// For example, `std::vector<T>::value_type` can be simplified as `T`.
class DependentNameResolver {
public:
DependentNameResolver(clang::Sema& sema, clang::ASTContext& context) : sema(sema), context(context) {}
clang::QualType resolve(clang::NamedDecl* ND);
clang::QualType resolve(clang::QualType type);
clang::QualType resolve(const clang::DependentNameType* DNT);
clang::QualType resolve(const clang::DependentTemplateSpecializationType* DTST);
/// lookup member in a given nested name specifier
bool lookup(llvm::SmallVector<clang::NamedDecl*>& result,
const clang::NestedNameSpecifier* NNS,
const clang::IdentifierInfo* II);
bool lookup(llvm::SmallVector<clang::NamedDecl*>& result,
const clang::QualType type,
const clang::IdentifierInfo* II);
// lookup member in a given class template
// FIXME: search in base classes
bool lookup(llvm::SmallVector<clang::NamedDecl*>& result,
clang::ClassTemplateDecl* CTD,
const clang::IdentifierInfo* II,
llvm::ArrayRef<clang::TemplateArgument> arguments);
std::vector<clang::TemplateArgument> resugar(llvm::ArrayRef<clang::TemplateArgument> arguments);
/// we use `Sema::SubstType` to substitute the template arguments in dependent type.
/// but it doesn't substitute the template arguments in alias type.
/// i.e. `typename base::type`, when base is `std::vector<T>`, it will ignore the `T`.
/// so before actually substituting the type, we need to dealias the type.
clang::QualType dealias(clang::QualType type);
/// replace the template arguments in the type, using the arguments in the frame
clang::QualType substitute(clang::QualType type);
private:
struct Frame {
clang::NamedDecl* decl;
std::vector<clang::TemplateArgument> arguments;
};
clang::Sema& sema;
clang::ASTContext& context;
std::vector<Frame> frames;
};
} // namespace clice

View File

@@ -0,0 +1,31 @@
#include "ParsedAST.h"
#include <clang/AST/ASTTypeTraits.h>
namespace clice {
class SelectionTree {
public:
enum Selection {
Unselected,
Partial,
Complete,
};
struct Node {
Node* parent;
llvm::SmallVector<const Node*> children;
clang::DynTypedNode ASTNode;
};
const Node* commonAncestor() const;
const Node& root() const;
SelectionTree(clang::ASTContext& context, const clang::syntax::TokenBuffer& tokens, unsigned start, unsigned end);
private:
std::deque<Node> m_Nodes;
const Node* m_Root;
};
} // namespace clice