Skip to content

Commit

Permalink
Merge pull request #1365 from jacob-carlborg/uda
Browse files Browse the repository at this point in the history
Add @Attribute syntax
  • Loading branch information
WalterBright committed Dec 15, 2012
2 parents 0002539 + e25e566 commit 15389cf
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/parse.c
Expand Up @@ -379,8 +379,26 @@ Dsymbols *Parser::parseDeclDefs(int once)
s = new UserAttributeDeclaration(exps, a);
break;
}
stc = parseAttribute();
goto Lstc;
else if (peek(&token)->value == TOKidentifier)
{
if (isPredefinedAttribute(peek(&token)->ident))
{
stc = parseAttribute();
goto Lstc;
}
else
{
nextToken();
Expressions* exprs = new Expressions();
Expression* expr = parsePrimaryExp();
if (token.value == TOKlparen)
expr = new CallExp(loc, expr, parseArguments());
exprs->push(expr);
a = parseBlock();
s = new UserAttributeDeclaration(exprs, a);
break;
}
}
#endif

Lstc:
Expand Down Expand Up @@ -6926,4 +6944,11 @@ void initPrecedence()
precedence[TOKdeclaration] = PREC_expr;
}


bool isPredefinedAttribute (Identifier* identifier)
{
return identifier == Id::property ||
identifier == Id::safe ||
identifier == Id::trusted ||
identifier == Id::system ||
identifier == Id::disable;
}
1 change: 1 addition & 0 deletions src/parse.h
Expand Up @@ -183,5 +183,6 @@ enum PREC
extern enum PREC precedence[TOKMAX];

void initPrecedence();
bool isPredefinedAttribute (Identifier* identifier);

#endif /* DMD_PARSE_H */
123 changes: 123 additions & 0 deletions test/runnable/uda.d
Expand Up @@ -106,13 +106,136 @@ void test5()

/************************************************/

enum Test6;
@Test6 int x6;
pragma(msg, __traits(getAttributes, x6));

void test6()
{
alias Tuple!(__traits(getAttributes, x6)) tp;

assert(tp.length == 1);

if (!is(Test6 == tp[0]))
assert(0);
}

/************************************************/

struct Test7
{
int a;
string b;
}

@Test7(3, "foo") int x7;
pragma(msg, __traits(getAttributes, x7));

void test7()
{
alias Tuple!(__traits(getAttributes, x7)) tp;

assert(tp.length == 1);

if (!is(Test7 == typeof(tp[0])))
assert(0);

assert(tp[0] == Test7(3, "foo"));
}

/************************************************/

struct Test8 (string foo) {}

@Test8!"foo" int x8;
pragma(msg, __traits(getAttributes, x8));

void test8()
{
alias Tuple!(__traits(getAttributes, x8)) tp;

assert(tp.length == 1);

if (!is(Test8!("foo") == tp[0]))
assert(0);
}

/************************************************/

struct Test9 (string foo) {}

@Test9!("foo") int x9;
pragma(msg, __traits(getAttributes, x9));

void test9()
{
alias Tuple!(__traits(getAttributes, x9)) tp;

assert(tp.length == 1);

if (!is(Test9!("foo") == tp[0]))
assert(0);
}

/************************************************/

struct Test10 (string foo)
{
int a;
}

@Test10!"foo"(3) int x10;
pragma(msg, __traits(getAttributes, x10));

void test10()
{
alias Tuple!(__traits(getAttributes, x10)) tp;

assert(tp.length == 1);

if (!is(Test10!("foo") == typeof(tp[0])))
assert(0);

assert(tp[0] == Test10!("foo")(3));
}

/************************************************/

struct Test11 (string foo)
{
int a;
}

@Test11!("foo")(3) int x11;
pragma(msg, __traits(getAttributes, x11));

void test11()
{
alias Tuple!(__traits(getAttributes, x11)) tp;

assert(tp.length == 1);

if (!is(Test11!("foo") == typeof(tp[0])))
assert(0);

assert(tp[0] == Test11!("foo")(3));
}

/************************************************/

int main()
{
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
test10();
test11();

printf("Success\n");
return 0;
Expand Down

0 comments on commit 15389cf

Please sign in to comment.