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

return scope: first support #5972

Merged
merged 14 commits into from
Nov 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/declaration.d
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ enum STCout = (1L << 12); // out parameter
enum STClazy = (1L << 13); // lazy parameter
enum STCforeach = (1L << 14); // variable for foreach loop
// (1L << 15)
enum STCvariadic = (1L << 16); // variadic function argument
enum STCvariadic = (1L << 16); // the 'variadic' parameter in: T foo(T a, U b, V variadic...)
enum STCctorinit = (1L << 17); // can only be set inside constructor
enum STCtemplateparameter = (1L << 18); // template parameter
enum STCscope = (1L << 19);
Expand Down Expand Up @@ -123,10 +123,11 @@ enum STCtemp = (1L << 40); // temporary variable
enum STCrvalue = (1L << 41); // force rvalue for variables
enum STCnogc = (1L << 42); // @nogc
enum STCvolatile = (1L << 43); // destined for volatile in the back end
enum STCreturn = (1L << 44); // 'return ref' for function parameters
enum STCreturn = (1L << 44); // 'return ref' or 'return scope' for function parameters
enum STCautoref = (1L << 45); // Mark for the already deduced 'auto ref' parameter
enum STCinference = (1L << 46); // do attribute inference
enum STCexptemp = (1L << 47); // temporary variable that has lifetime restricted to an expression
enum STCmaybescope = (1L << 48); // parameter might be 'scope'

enum STC_TYPECTOR = (STCconst | STCimmutable | STCshared | STCwild);
enum STC_FUNCATTR = (STCref | STCnothrow | STCnogc | STCpure | STCproperty | STCsafe | STCtrusted | STCsystem);
Expand Down Expand Up @@ -1015,6 +1016,8 @@ extern (C++) class VarDeclaration : Declaration
{
Initializer _init;
uint offset;
uint sequenceNumber; // order the variables are declared
Copy link
Member

Choose a reason for hiding this comment

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

order the variables are declared???
Thought you were the native speaker :).
Did you mean order the declaration of variables?

__gshared uint nextSequenceNumber; // the counter for sequenceNumber
Copy link
Member

Choose a reason for hiding this comment

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

Also seems to work for nested scopes. Could we have some weird semantic order of nested functions or so that conflicts with this sequence number scheme?

FuncDeclarations nestedrefs; // referenced by these lexically nested functions
bool isargptr; // if parameter that _argptr points to
structalign_t alignment;
Expand All @@ -1028,6 +1031,7 @@ extern (C++) class VarDeclaration : Declaration
int canassign; // it can be assigned to
bool overlapped; // if it is a field and has overlapping
bool overlapUnsafe; // if it is an overlapping field and the overlaps are unsafe
bool doNotInferScope; // do not infer 'scope' for this variable
ubyte isdataseg; // private data for isDataseg 0 unset, 1 true, 2 false
Dsymbol aliassym; // if redone as alias to another symbol
VarDeclaration lastVar; // Linked list of variables for goto-skips-init detection
Expand All @@ -1047,7 +1051,7 @@ extern (C++) class VarDeclaration : Declaration
final extern (D) this(Loc loc, Type type, Identifier id, Initializer _init, StorageClass storage_class = STCundefined)
{
super(id);
//printf("VarDeclaration('%s')\n", id->toChars());
//printf("VarDeclaration('%s')\n", id.toChars());
assert(id);
debug
{
Expand All @@ -1057,12 +1061,14 @@ extern (C++) class VarDeclaration : Declaration
//*(char*)0=0;
}
}

assert(type || _init);
this.type = type;
this._init = _init;
this.loc = loc;
ctfeAdrOnStack = -1;
this.storage_class = storage_class;
sequenceNumber = ++nextSequenceNumber;
}

override Dsymbol syntaxCopy(Dsymbol s)
Expand Down Expand Up @@ -2405,6 +2411,19 @@ extern (C++) class VarDeclaration : Declaration
{
v.visit(this);
}

/**********************************
* Determine if `this` has a lifetime that lasts past
* the destruction of `v`
* Params:
* v = variable to test against
* Returns:
* true if it does
*/
final bool enclosesLifetimeOf(VarDeclaration v) const pure
{
return sequenceNumber < v.sequenceNumber;
}
}

/***********************************************************
Expand Down
3 changes: 3 additions & 0 deletions src/declaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ class VarDeclaration : public Declaration
public:
Initializer *_init;
unsigned offset;
unsigned sequenceNumber; // order the variables are declared
FuncDeclarations nestedrefs; // referenced by these lexically nested functions
bool isargptr; // if parameter that _argptr points to
structalign_t alignment;
Expand All @@ -239,6 +240,8 @@ class VarDeclaration : public Declaration
bool mynew; // it is a class new'd with custom operator new
int canassign; // it can be assigned to
bool overlapped; // if it is a field and has overlapping
bool overlapUnsafe; // if it is an overlapping field and the overlaps are unsafe
bool doNotInferScope; // do not infer 'scope' for this variable
unsigned char isdataseg; // private data for isDataseg
Dsymbol *aliassym; // if redone as alias to another symbol
VarDeclaration *lastVar; // Linked list of variables for goto-skips-init detection
Expand Down
7 changes: 5 additions & 2 deletions src/dtemplate.d
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,7 @@ extern (C++) final class TemplateDeclaration : ScopeDsymbol
// https://issues.dlang.org/show_bug.cgi?id=12876
// Optimize argument to allow CT-known length matching
farg = farg.optimize(WANTvalue, (fparam.storageClass & (STCref | STCout)) != 0);
//printf("farg = %s %s\n", farg.type.toChars(), fargtoChars());
//printf("farg = %s %s\n", farg.type.toChars(), farg.toChars());

RootObject oarg = farg;
if ((fparam.storageClass & STCref) && (!(fparam.storageClass & STCauto) || farg.isLvalue()))
Expand Down Expand Up @@ -2501,6 +2501,7 @@ void functionResolve(Match* m, Dsymbol dstart, Loc loc, Scope* sc, Objects* tiar

int applyTemplate(TemplateDeclaration td)
{
//printf("applyTemplate()\n");
// skip duplicates
if (td == td_best)
return 0;
Expand Down Expand Up @@ -3675,7 +3676,9 @@ MATCH deduceType(RootObject o, Scope* sc, Type tparam, TemplateParameters* param
{
Parameter a = Parameter.getNth(t.parameters, i);
Parameter ap = Parameter.getNth(tp.parameters, i);
if (a.storageClass != ap.storageClass || !deduceType(a.type, sc, ap.type, parameters, dedtypes))

if (!a.isCovariant(ap) ||
!deduceType(a.type, sc, ap.type, parameters, dedtypes))
{
result = MATCHnomatch;
return;
Expand Down
Loading