Skip to content
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

Implementation for init= #12387

Merged
merged 24 commits into from
Mar 1, 2019
Merged

Conversation

benharsh
Copy link
Member

@benharsh benharsh commented Feb 26, 2019

This PR implements support for the new kind of copy initializer, named "init=". This copy initializer will follow similar rules as a regular initializer (e.g. initialize fields in order). The init= method will be invoked for compiler-inserted copies of records, and for initialization of variables from an expression. A simple case is initializing from the same type:

var orig : MyRecord;
var copy : MyRecord = orig; // copy.init=(orig)

A more interesting case is when initializing from a different type:

record R {
  var x : int;

  proc init=(other : int) {
    this.x = other;
  }
}

var r : R = 5; // r.init=(5);

Generic types require an additional type formal that represents the intended instantiation:

record R {
  type T;
  var x : T;

  proc init=(type ThisType, other : ThisType.T) {
    this.T = ThisType.T;
    this.x = other;
  }
}

var x : R(int) = 5;
var y : R(string) = "hello";

As a result of this functionality, atomic variables can be initialized from values:

var x : atomic int = 5;

This PR allows for the old-style copy initializers to still be invoked, unless the user has implemented their own init= methods.

Testing:

  • local
  • memleaks
  • valgrind
  • gasnet

@benharsh
Copy link
Member Author

This PR updates the "records.chpl" primer to use init=, but would pass if we used the old-style copy initializer. Do we want to make this change where users would see it?

@mppf mppf self-requested a review February 27, 2019 19:32
Copy link
Member

@mppf mppf left a comment

Choose a reason for hiding this comment

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

Looking good to me. I had some questions and relatively minor feedback. Thanks!

Also do you know if we have any tests/futures of init= than use the in intent? Arguably that creates an infinite loop in compilation (since the in intent would normally mean copy-initializing, right?) but if somebody is confused it'd be nice if we gave a reasonable error.

compiler/AST/AggregateType.cpp Outdated Show resolved Hide resolved
compiler/AST/AggregateType.cpp Show resolved Hide resolved
@@ -3551,7 +3551,7 @@ GenRet CallExpr::codegen() {

// Handle setting LLVM invariant on const records after
// they are initialized
if (fn->isInitializer()) {
if (fn->isInitializer() || fn->isCopyInit()) {
Copy link
Member

Choose a reason for hiding this comment

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

These adjustments seem strange to me since I consider init= a kind of initializer. Maybe there's a way to rename isInitializer to make it less confusing. That would be reasonable to create a future work issue about.

compiler/include/flags_list.h Outdated Show resolved Hide resolved
@@ -155,7 +155,7 @@ void normalize() {
}
USR_FATAL_CONT(fn, "Type '%s' defines a constructor here", ct->symbol->name);

} else if (fn->isInitializer() == true) {
} else if (fn->isInitializer() || fn->isCopyInit()) {
Copy link
Member

Choose a reason for hiding this comment

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

This change occurs enough in this PR that I'm starting to think it'd be worth adding FnSymbol::isCopyOrRegularInit or something like that...

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 would like something like this too, and something that would handle the strcmp cases too. Do you want that to be done for this PR?

Copy link
Member

Choose a reason for hiding this comment

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

Only if you think it'll help you get the code correct. If you choose to put it off, let's make an issue and put it to get worked on soon.

modules/internal/ChapelSyncvar.chpl Outdated Show resolved Hide resolved
modules/internal/OwnedObject.chpl Show resolved Hide resolved
modules/internal/OwnedObject.chpl Show resolved Hide resolved
modules/standard/BigInteger.chpl Show resolved Hide resolved
test/classes/initializers/initequals/basic.chpl Outdated Show resolved Hide resolved
@benharsh benharsh merged commit b927f3a into chapel-lang:master Mar 1, 2019
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.

None yet

2 participants