]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/d/dmd/identifier.c
2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved
4 * written by Walter Bright
5 * http://www.digitalmars.com
6 * Distributed under the Boost Software License, Version 1.0.
7 * http://www.boost.org/LICENSE_1_0.txt
8 * https://github.com/D-Programming-Language/dmd/blob/master/src/identifier.c
11 #include "root/dsystem.h"
12 #include "root/root.h"
14 #include "identifier.h"
20 Identifier::Identifier(const char *string
, size_t length
, int value
)
22 //printf("Identifier('%s', %d)\n", string, value);
23 this->string
= string
;
28 Identifier::Identifier(const char *string
)
30 //printf("Identifier('%s')\n", string);
31 this->string
= string
;
32 this->value
= TOKidentifier
;
33 this->len
= strlen(string
);
36 Identifier
*Identifier::create(const char *string
)
38 return new Identifier(string
);
41 bool Identifier::equals(RootObject
*o
)
43 return this == o
|| strncmp(string
,o
->toChars(),len
+1) == 0;
46 int Identifier::compare(RootObject
*o
)
48 return strncmp(string
, o
->toChars(), len
+ 1);
51 const char *Identifier::toChars()
56 int Identifier::getValue() const
61 const char *Identifier::toHChars2()
65 if (this == Id::ctor
) p
= "this";
66 else if (this == Id::dtor
) p
= "~this";
67 else if (this == Id::unitTest
) p
= "unittest";
68 else if (this == Id::dollar
) p
= "$";
69 else if (this == Id::withSym
) p
= "with";
70 else if (this == Id::result
) p
= "result";
71 else if (this == Id::returnLabel
) p
= "return";
76 if (strncmp(p
, "_staticCtor", 11) == 0)
78 else if (strncmp(p
, "_staticDtor", 11) == 0)
80 else if (strncmp(p
, "__invariant", 11) == 0)
88 void Identifier::print()
90 fprintf(stderr
, "%s",string
);
93 int Identifier::dyncast() const
95 return DYNCAST_IDENTIFIER
;
98 StringTable
Identifier::stringtable
;
100 Identifier
*Identifier::generateId(const char *prefix
)
104 return generateId(prefix
, ++i
);
107 Identifier
*Identifier::generateId(const char *prefix
, size_t i
)
110 buf
.writestring(prefix
);
111 buf
.printf("%llu", (ulonglong
)i
);
113 char *id
= buf
.peekString();
117 /********************************************
118 * Create an identifier in the string table.
121 Identifier
*Identifier::idPool(const char *s
, size_t len
)
123 StringValue
*sv
= stringtable
.update(s
, len
);
124 Identifier
*id
= (Identifier
*) sv
->ptrvalue
;
127 id
= new Identifier(sv
->toDchars(), len
, TOKidentifier
);
128 sv
->ptrvalue
= (char *)id
;
133 Identifier
*Identifier::idPool(const char *s
, size_t len
, int value
)
135 StringValue
*sv
= stringtable
.insert(s
, len
, NULL
);
137 Identifier
*id
= new Identifier(sv
->toDchars(), len
, value
);
138 sv
->ptrvalue
= (char *)id
;
142 /**********************************
143 * Determine if string is a valid Identifier.
148 bool Identifier::isValidIdentifier(const char *p
)
156 if (*p
>= '0' && *p
<= '9') // beware of isdigit() on signed chars
164 const char *q
= utf_decodeChar((const utf8_t
*)p
, len
, &idx
, &dc
);
168 if (!((dc
>= 0x80 && isUniAlpha(dc
)) || isalnum(dc
) || dc
== '_'))
177 Identifier
*Identifier::lookup(const char *s
, size_t len
)
179 StringValue
*sv
= stringtable
.lookup(s
, len
);
182 return (Identifier
*)sv
->ptrvalue
;
185 void Identifier::initTable()
187 stringtable
._init(28000);