Skip to content

[AST] Eliminate IfConfigStmt #9413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 16, 2017

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented May 9, 2017

NOTE: This conflicts with #7955 , I will rebase this PR when it's merged. landed

Resolves: SR-4426

#if in statements position doesn't have to be Stmt.
Modeling it as IfConfigDecl simplify the code and AST.

  • Make IfConfigDecl be able to hold ASTNode elements
  • Parse #if as IfConfigDecl
  • Stop enclosing toplevel #if into TopLevelCodeDecl.
  • New methods on IfConfigDecl for retrieving active elements as Decls. This replaces IfConfigDecl::getActiveMembers().
  • Eliminate IfConfigStmt
#if true
  #if true
  func foo() {}
  #endif
#else
  func bar() {}
#endif

is now dumped as:

(source_file
  (if_config_decl
    #if: active
      (boolean_literal_expr type='<null>' value=true)
      (elements
        (if_config_decl
          #if: active
            (boolean_literal_expr type='<null>' value=true)
            (elements
              (func_decl "foo()" ...)))
        (func_decl "foo()" ...))
    #else:
      (elements
        (func_decl "bar()" ...)))
  (func_decl "foo()" ...))

@rintaro
Copy link
Member Author

rintaro commented May 9, 2017

@slavapestov @jrose-apple
What do you think?

@rintaro
Copy link
Member Author

rintaro commented May 9, 2017

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor

@swift-ci Please test source compatibility

/// All elements must be Decl.
const AsDeclRange getAsDecls() const {
return AsDeclRange(getActiveClauseElements(), AsDeclWithSkippingIfConfig());
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new methods looks overkill since they only have one client.

@@ -24,6 +24,7 @@
#include "swift/AST/IfConfigClause.h"
#include "swift/AST/TypeAlignments.h"
#include "swift/Basic/NullablePtr.h"
#include "swift/Basic/STLExtras.h"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accidental addition.

Indent += 2;
for (auto &Clause : ICD->getClauses()) {
OS << '\n';
OS.indent(Indent);
PrintWithColorRAII(OS, StmtColor) << (Clause.Cond ? "#if:" : "#else");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add : after #else.

@rintaro rintaro force-pushed the ast-eliminate-ifconfigstmt branch 3 times, most recently from 10c93c4 to 1266ada Compare May 9, 2017 13:56
@rintaro
Copy link
Member Author

rintaro commented May 9, 2017

@swift-ci Please smoke test

@rintaro
Copy link
Member Author

rintaro commented May 9, 2017

@swift-ci Please test source compatibility

Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test that shows that we still reject statements within #if within types, and at the top level in -parse-as-library code?


for (auto &Clause : ICD->getClauses()) {
if (&Clause == &*ICD->getClauses().begin())
Printer << tok::pound_if << " "; // FIXME: print condition
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you print something here, even a comment, so that people will know it's not a bug? Or, well, that it's a known bug?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is just a unimplemented feature.
For now, added a commit for printing /* condition */ here.

@rintaro
Copy link
Member Author

rintaro commented May 9, 2017

@swift-ci Please smoke test

@rintaro
Copy link
Member Author

rintaro commented May 9, 2017

@CodaFi
Copy link
Contributor

CodaFi commented May 15, 2017

@rintaro Is there anything blocking you from merging this now?

@rintaro
Copy link
Member Author

rintaro commented May 16, 2017

@CodaFi
Although I'm OK to merge this prior to #7955, I prefer to land #7955 first.
Also, this PR hasn't been explicitly approved by anyone yet.

@jrose-apple
Copy link
Contributor

I didn't see any problems but I wasn't looking too closely. Slava may also still have opinions.

rintaro added 4 commits May 16, 2017 12:09
Resolves: https://bugs.swift.org/browse/SR-4426

* Make IfConfigDecl be able to hold ASTNodes
* Parse #if as IfConfigDecl
* Stop enclosing toplevel #if into TopLevelCodeDecl.
* Eliminate IfConfigStmt
Now that, all TopLevelCodeDecl is real. Just don't need this.
* Removed extra newlines.
* Print '/* condition */' for condition part.
@rintaro rintaro force-pushed the ast-eliminate-ifconfigstmt branch from 26629d8 to 2ad22a8 Compare May 16, 2017 03:29
@rintaro
Copy link
Member Author

rintaro commented May 16, 2017

@swift-ci Please smoke test

@rintaro rintaro requested a review from slavapestov May 16, 2017 03:34
Now that, IfConfigDecl holds ASTNode regardless statements position or
declarations postion. We can construct it in single method.
@rintaro rintaro force-pushed the ast-eliminate-ifconfigstmt branch from 552dea0 to c8d717e Compare May 16, 2017 11:53
@rintaro
Copy link
Member Author

rintaro commented May 16, 2017

@swift-ci Please smoke test

@rintaro
Copy link
Member Author

rintaro commented May 19, 2017

@slavapestov Could you review this?

@CodaFi
Copy link
Contributor

CodaFi commented May 30, 2017

Just to get the validation tests in

@swift-ci please test

@rintaro
Copy link
Member Author

rintaro commented May 31, 2017

@slavapestov Ping
Any change to merge this?

@slavapestov slavapestov merged commit 732b215 into swiftlang:master Jun 16, 2017
@rintaro
Copy link
Member Author

rintaro commented Jun 17, 2017

Thanks!

@rintaro rintaro deleted the ast-eliminate-ifconfigstmt branch June 17, 2017 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants