Files
swift-mirror/lib/Syntax/Format.cpp
David Farler c343298b8f [Syntax] Implement return-statement and integer-literal-expr
A return statement needs something to return, so implement
integer-literal-expression too. This necessarily also forced
UnknownExprSyntax, UnknownStmtSyntax, and UnknownDeclSyntax,
which are stand-in token buckets for when we don't know
how to transform/migrate an AST.

This commit also contains the core function for caching
SyntaxData children. This is highly tricky code, with some
detailed comments in SyntaxData.{h,cpp}. The gist is that
we have to atomically swap in a SyntaxData pointer into the
child field, so we can maintain pointer identity of SyntaxData
nodes, while still being able to cache them internally.

To prove that this works, there is a multithreaded test that
checks that two threads can ask for a child that hasn't been
cached yet without crashing or violating pointer identity.

https://bugs.swift.org/browse/SR-4010
2017-02-22 18:45:29 -08:00

54 lines
1.5 KiB
C++

//===--- Format.cpp - Declaration Syntax Formatting Impl. -------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Syntax/Format.h"
#include "swift/Syntax/DeclSyntax.h"
#include "swift/Syntax/ExprSyntax.h"
using namespace swift;
using namespace swift::syntax;
using llvm::cast;
StructDeclSyntax
FormatSyntaxRewriter::rewriteStructDecl(StructDeclSyntax Struct) {
return Struct;
}
Syntax syntax::format(Syntax Tree) {
switch (Tree.getKind()) {
case SyntaxKind::StructDecl: {
auto Struct = Tree.castTo<StructDeclSyntax>();
auto LeftBrace = Struct.getLeftBraceToken();
if (!LeftBrace->LeadingTrivia.contains(TriviaKind::Newline)) {
auto NewLeading = Trivia::newlines(1) + LeftBrace->LeadingTrivia;
const auto NewMembers =
format(Struct.getMembers()).castTo<DeclMembersSyntax>();
LeftBrace = LeftBrace->withLeadingTrivia(NewLeading);
return Struct.withMembers(NewMembers).withLeftBrace(LeftBrace);
}
break;
}
case SyntaxKind::DeclMembers: {
auto Members = Tree.castTo<DeclMembersSyntax>();
// TODO
break;
}
default:
return Tree;
}
return Tree;
}