本文整理汇总了C++中StructDeclaration类的典型用法代码示例。如果您正苦于以下问题:C++ StructDeclaration类的具体用法?C++ StructDeclaration怎么用?C++ StructDeclaration使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StructDeclaration类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: printf
Expression *semanticTraits(TraitsExp *e, Scope *sc)
{
#if LOGSEMANTIC
printf("TraitsExp::semantic() %s\n", e->toChars());
#endif
if (e->ident != Id::compiles && e->ident != Id::isSame &&
e->ident != Id::identifier && e->ident != Id::getProtection)
{
if (!TemplateInstance::semanticTiargs(e->loc, sc, e->args, 1))
return new ErrorExp();
}
size_t dim = e->args ? e->args->dim : 0;
if (e->ident == Id::isArithmetic)
{
return isTypeX(e, &isTypeArithmetic);
}
else if (e->ident == Id::isFloating)
{
return isTypeX(e, &isTypeFloating);
}
else if (e->ident == Id::isIntegral)
{
return isTypeX(e, &isTypeIntegral);
}
else if (e->ident == Id::isScalar)
{
return isTypeX(e, &isTypeScalar);
}
else if (e->ident == Id::isUnsigned)
{
return isTypeX(e, &isTypeUnsigned);
}
else if (e->ident == Id::isAssociativeArray)
{
return isTypeX(e, &isTypeAssociativeArray);
}
else if (e->ident == Id::isStaticArray)
{
return isTypeX(e, &isTypeStaticArray);
}
else if (e->ident == Id::isAbstractClass)
{
return isTypeX(e, &isTypeAbstractClass);
}
else if (e->ident == Id::isFinalClass)
{
return isTypeX(e, &isTypeFinalClass);
}
else if (e->ident == Id::isTemplate)
{
return isSymbolX(e, &isTemplate);
}
else if (e->ident == Id::isPOD)
{
if (dim != 1)
goto Ldimerror;
RootObject *o = (*e->args)[0];
Type *t = isType(o);
StructDeclaration *sd;
if (!t)
{
e->error("type expected as second argument of __traits %s instead of %s", e->ident->toChars(), o->toChars());
goto Lfalse;
}
Type *tb = t->baseElemOf();
if (tb->ty == Tstruct
&& ((sd = (StructDeclaration *)(((TypeStruct *)tb)->sym)) != NULL))
{
if (sd->isPOD())
goto Ltrue;
else
goto Lfalse;
}
goto Ltrue;
}
else if (e->ident == Id::isNested)
{
if (dim != 1)
goto Ldimerror;
RootObject *o = (*e->args)[0];
Dsymbol *s = getDsymbol(o);
AggregateDeclaration *a;
FuncDeclaration *f;
if (!s) { }
else if ((a = s->isAggregateDeclaration()) != NULL)
{
if (a->isNested())
goto Ltrue;
else
goto Lfalse;
}
else if ((f = s->isFuncDeclaration()) != NULL)
{
if (f->isNested())
goto Ltrue;
else
goto Lfalse;
}
//.........这里部分代码省略.........
示例2: ErrorInitializer
Initializer *StructInitializer::semantic(Scope *sc, Type *t, NeedInterpret needInterpret)
{
//printf("StructInitializer::semantic(t = %s) %s\n", t->toChars(), toChars());
t = t->toBasetype();
if (t->ty == Tsarray && t->nextOf()->toBasetype()->ty == Tstruct)
t = t->nextOf()->toBasetype();
if (t->ty == Tstruct)
{
StructDeclaration *sd = ((TypeStruct *)t)->sym;
if (sd->ctor)
{
error(loc, "%s %s has constructors, cannot use { initializers }, use %s( initializers ) instead",
sd->kind(), sd->toChars(), sd->toChars());
return new ErrorInitializer();
}
sd->size(loc);
if (sd->sizeok != SIZEOKdone)
return new ErrorInitializer();
size_t nfields = sd->fields.dim - sd->isNested();
//expandTuples for non-identity arguments?
Expressions *elements = new Expressions();
elements->setDim(nfields);
for (size_t i = 0; i < elements->dim; i++)
(*elements)[i] = NULL;
// Run semantic for explicitly given initializers
// TODO: this part is slightly different from StructLiteralExp::semantic.
bool errors = false;
for (size_t fieldi = 0, i = 0; i < field.dim; i++)
{
if (Identifier *id = field[i])
{
Dsymbol *s = sd->search(loc, id);
if (!s)
{
s = sd->search_correct(id);
if (s)
error(loc, "'%s' is not a member of '%s', did you mean '%s %s'?",
id->toChars(), sd->toChars(), s->kind(), s->toChars());
else
error(loc, "'%s' is not a member of '%s'", id->toChars(), sd->toChars());
return new ErrorInitializer();
}
s = s->toAlias();
// Find out which field index it is
for (fieldi = 0; 1; fieldi++)
{
if (fieldi >= nfields)
{
error(loc, "%s.%s is not a per-instance initializable field",
sd->toChars(), s->toChars());
return new ErrorInitializer();
}
if (s == sd->fields[fieldi])
break;
}
}
else if (fieldi >= nfields)
{
error(loc, "too many initializers for %s", sd->toChars());
return new ErrorInitializer();
}
VarDeclaration *vd = sd->fields[fieldi];
if ((*elements)[fieldi])
{
error(loc, "duplicate initializer for field '%s'", vd->toChars());
errors = true;
continue;
}
for (size_t j = 0; j < nfields; j++)
{
VarDeclaration *v2 = sd->fields[j];
bool overlap = (vd->offset < v2->offset + v2->type->size() &&
v2->offset < vd->offset + vd->type->size());
if (overlap && (*elements)[j])
{
error(loc, "overlapping initialization for field %s and %s",
v2->toChars(), vd->toChars());
errors = true;
continue;
}
}
assert(sc);
Initializer *iz = value[i];
iz = iz->semantic(sc, vd->type->addMod(t->mod), needInterpret);
Expression *ex = iz->toExpression();
if (ex->op == TOKerror)
{
errors = true;
continue;
}
value[i] = iz;
(*elements)[fieldi] = ex;
++fieldi;
}
//.........这里部分代码省略.........
示例3: printf
Expression *TraitsExp::semantic(Scope *sc)
{
#if LOGSEMANTIC
printf("TraitsExp::semantic() %s\n", toChars());
#endif
if (ident != Id::compiles && ident != Id::isSame &&
ident != Id::identifier && ident != Id::getProtection)
{
TemplateInstance::semanticTiargs(loc, sc, args, 1);
}
size_t dim = args ? args->dim : 0;
Declaration *d;
if (ident == Id::isArithmetic)
{
return isTypeX(&isTypeArithmetic);
}
else if (ident == Id::isFloating)
{
return isTypeX(&isTypeFloating);
}
else if (ident == Id::isIntegral)
{
return isTypeX(&isTypeIntegral);
}
else if (ident == Id::isScalar)
{
return isTypeX(&isTypeScalar);
}
else if (ident == Id::isUnsigned)
{
return isTypeX(&isTypeUnsigned);
}
else if (ident == Id::isAssociativeArray)
{
return isTypeX(&isTypeAssociativeArray);
}
else if (ident == Id::isStaticArray)
{
return isTypeX(&isTypeStaticArray);
}
else if (ident == Id::isAbstractClass)
{
return isTypeX(&isTypeAbstractClass);
}
else if (ident == Id::isFinalClass)
{
return isTypeX(&isTypeFinalClass);
}
else if (ident == Id::isPOD)
{
if (dim != 1)
goto Ldimerror;
RootObject *o = (*args)[0];
Type *t = isType(o);
StructDeclaration *sd;
if (!t)
{
error("type expected as second argument of __traits %s instead of %s", ident->toChars(), o->toChars());
goto Lfalse;
}
if (t->toBasetype()->ty == Tstruct
&& ((sd = (StructDeclaration *)(((TypeStruct *)t->toBasetype())->sym)) != NULL))
{
if (sd->isPOD())
goto Ltrue;
else
goto Lfalse;
}
goto Ltrue;
}
else if (ident == Id::isNested)
{
if (dim != 1)
goto Ldimerror;
RootObject *o = (*args)[0];
Dsymbol *s = getDsymbol(o);
AggregateDeclaration *a;
FuncDeclaration *f;
if (!s) { }
else if ((a = s->isAggregateDeclaration()) != NULL)
{
if (a->isNested())
goto Ltrue;
else
goto Lfalse;
}
else if ((f = s->isFuncDeclaration()) != NULL)
{
if (f->isNested())
goto Ltrue;
else
goto Lfalse;
}
error("aggregate or function expected instead of '%s'", o->toChars());
goto Lfalse;
}
else if (ident == Id::isAbstractFunction)
//.........这里部分代码省略.........
示例4: if
/***************************************
* This works by transforming a struct initializer into
* a struct literal. In the future, the two should be the
* same thing.
*/
Expression *StructInitializer::toExpression()
{ Expression *e;
size_t offset;
//printf("StructInitializer::toExpression() %s\n", toChars());
if (!ad) // if fwd referenced
{
return NULL;
}
StructDeclaration *sd = ad->isStructDeclaration();
if (!sd)
return NULL;
Expressions *elements = new Expressions();
size_t nfields = ad->fields.dim;
#if DMDV2
if (sd->isnested)
nfields--;
#endif
elements->setDim(nfields);
for (size_t i = 0; i < elements->dim; i++)
{
elements->tdata()[i] = NULL;
}
unsigned fieldi = 0;
for (size_t i = 0; i < value.dim; i++)
{
Identifier *id = field.tdata()[i];
if (id)
{
Dsymbol * s = ad->search(loc, id, 0);
if (!s)
{
error(loc, "'%s' is not a member of '%s'", id->toChars(), sd->toChars());
goto Lno;
}
// Find out which field index it is
for (fieldi = 0; 1; fieldi++)
{
if (fieldi >= nfields)
{
s->error("is not a per-instance initializable field");
goto Lno;
}
if (s == ad->fields.tdata()[fieldi])
break;
}
}
else if (fieldi >= nfields)
{ error(loc, "too many initializers for '%s'", ad->toChars());
goto Lno;
}
Initializer *iz = value.tdata()[i];
if (!iz)
goto Lno;
Expression *ex = iz->toExpression();
if (!ex)
goto Lno;
if (elements->tdata()[fieldi])
{ error(loc, "duplicate initializer for field '%s'",
ad->fields.tdata()[fieldi]->toChars());
goto Lno;
}
elements->tdata()[fieldi] = ex;
++fieldi;
}
// Now, fill in any missing elements with default initializers.
// We also need to validate any anonymous unions
offset = 0;
for (size_t i = 0; i < elements->dim; )
{
VarDeclaration * vd = ad->fields.tdata()[i]->isVarDeclaration();
//printf("test2 [%d] : %s %d %d\n", i, vd->toChars(), (int)offset, (int)vd->offset);
if (vd->offset < offset)
{
// Only the first field of a union can have an initializer
if (elements->tdata()[i])
goto Lno;
}
else
{
if (!elements->tdata()[i])
// Default initialize
elements->tdata()[i] = vd->type->defaultInit();
}
offset = vd->offset + vd->type->size();
i++;
#if 0
int unionSize = ad->numFieldsInUnion(i);
if (unionSize == 1)
{ // Not a union -- default initialize if missing
if (!elements->tdata()[i])
elements->tdata()[i] = vd->type->defaultInit();
}
//.........这里部分代码省略.........
示例5: isStructDeclaration
void AggregateDeclaration::semantic3(Scope *sc)
{
//printf("AggregateDeclaration::semantic3(%s) type = %s, errors = %d\n", toChars(), type->toChars(), errors);
if (!members)
return;
StructDeclaration *sd = isStructDeclaration();
if (!sc) // from runDeferredSemantic3 for TypeInfo generation
{
assert(sd);
sd->semanticTypeInfoMembers();
return;
}
Scope *sc2 = sc->push(this);
sc2->stc &= STCsafe | STCtrusted | STCsystem;
sc2->parent = this;
if (isUnionDeclaration())
sc2->inunion = 1;
sc2->protection = Prot(PROTpublic);
sc2->explicitProtection = 0;
sc2->structalign = STRUCTALIGN_DEFAULT;
sc2->userAttribDecl = NULL;
for (size_t i = 0; i < members->dim; i++)
{
Dsymbol *s = (*members)[i];
s->semantic3(sc2);
}
sc2->pop();
// don't do it for unused deprecated types
// or error types
if (!getRTInfo && Type::rtinfo &&
(!isDeprecated() || global.params.useDeprecated) &&
(type && type->ty != Terror))
{
// we do not want to report deprecated uses of this type during RTInfo
// generation, so we disable reporting deprecation temporarily
// WARNING: Muting messages during analysis of RTInfo might silently instantiate
// templates that use (other) deprecated types. If these template instances
// are used in other parts of the program later, they will be reused without
// ever producing the deprecation message. The implementation here restricts
// muting to the types that RTInfo is currently generated for.
bool wasmuted = mutedeprecation;
mutedeprecation = true;
// Evaluate: RTinfo!type
Objects *tiargs = new Objects();
tiargs->push(type);
TemplateInstance *ti = new TemplateInstance(loc, Type::rtinfo, tiargs);
ti->semantic(sc);
ti->semantic2(sc);
ti->semantic3(sc);
Dsymbol *s = ti->toAlias();
Expression *e = new DsymbolExp(Loc(), s, 0);
Scope *sc3 = ti->tempdecl->scope->startCTFE();
sc3->tinst = sc->tinst;
e = e->semantic(sc3);
sc3->endCTFE();
e = e->ctfeInterpret();
getRTInfo = e;
mutedeprecation = wasmuted;
}
if (sd)
sd->semanticTypeInfoMembers();
}
示例6: toChars
void TypeInfoStructDeclaration::llvmDefine()
{
Logger::println("TypeInfoStructDeclaration::llvmDefine() %s", toChars());
LOG_SCOPE;
// make sure struct is resolved
assert(tinfo->ty == Tstruct);
TypeStruct *tc = static_cast<TypeStruct *>(tinfo);
StructDeclaration *sd = tc->sym;
// can't emit typeinfo for forward declarations
if (sd->sizeok != 1)
{
sd->error("cannot emit TypeInfo for forward declaration");
fatal();
}
sd->codegen(Type::sir);
IrStruct* irstruct = sd->ir.irStruct;
RTTIBuilder b(Type::typeinfostruct);
// char[] name
b.push_string(sd->toPrettyChars());
// void[] init
// never emit a null array, even for zero initialized typeinfo
// the size() method uses this array!
size_t init_size = getTypeStoreSize(tc->irtype->getType());
b.push_void_array(init_size, irstruct->getInitSymbol());
// toX functions ground work
static TypeFunction *tftohash;
static TypeFunction *tftostring;
if (!tftohash)
{
Scope sc;
tftohash = new TypeFunction(NULL, Type::thash_t, 0, LINKd);
#if DMDV2
tftohash ->mod = MODconst;
#endif
tftohash = static_cast<TypeFunction *>(tftohash->semantic(0, &sc));
#if DMDV2
Type *retType = Type::tchar->invariantOf()->arrayOf();
#else
Type *retType = Type::tchar->arrayOf();
#endif
tftostring = new TypeFunction(NULL, retType, 0, LINKd);
tftostring = static_cast<TypeFunction *>(tftostring->semantic(0, &sc));
}
// this one takes a parameter, so we need to build a new one each time
// to get the right type. can we avoid this?
TypeFunction *tfcmpptr;
{
Scope sc;
Parameters *arguments = new Parameters;
#if STRUCTTHISREF
// arg type is ref const T
Parameter *arg = new Parameter(STCref, tc->constOf(), NULL, NULL);
#else
// arg type is const T*
Parameter *arg = new Parameter(STCin, tc->pointerTo(), NULL, NULL);
#endif
arguments->push(arg);
tfcmpptr = new TypeFunction(arguments, Type::tint32, 0, LINKd);
#if DMDV2
tfcmpptr->mod = MODconst;
#endif
tfcmpptr = static_cast<TypeFunction *>(tfcmpptr->semantic(0, &sc));
}
// well use this module for all overload lookups
Module *gm = getModule();
// toHash
FuncDeclaration* fd = find_method_overload(sd, Id::tohash, tftohash, gm);
b.push_funcptr(fd);
// opEquals
#if DMDV2
fd = sd->xeq;
#else
fd = find_method_overload(sd, Id::eq, tfcmpptr, gm);
#endif
b.push_funcptr(fd);
// opCmp
fd = find_method_overload(sd, Id::cmp, tfcmpptr, gm);
b.push_funcptr(fd);
// toString
fd = find_method_overload(sd, Id::tostring, tftostring, gm);
b.push_funcptr(fd);
// uint m_flags;
unsigned hasptrs = tc->hasPointers() ? 1 : 0;
b.push_uint(hasptrs);
//.........这里部分代码省略.........
示例7: dtxoff
void TypeInfoStructDeclaration::toDt(dt_t **pdt)
{
//printf("TypeInfoStructDeclaration::toDt() '%s'\n", toChars());
unsigned offset = Type::typeinfostruct->structsize;
dtxoff(pdt, Type::typeinfostruct->toVtblSymbol(), 0, TYnptr); // vtbl for TypeInfo_Struct
dtdword(pdt, 0); // monitor
assert(tinfo->ty == Tstruct);
TypeStruct *tc = (TypeStruct *)tinfo;
StructDeclaration *sd = tc->sym;
/* Put out:
* char[] name;
* void[] init;
* hash_t function(void*) xtoHash;
* int function(void*,void*) xopEquals;
* int function(void*,void*) xopCmp;
* char[] function(void*) xtoString;
* uint m_flags;
*
* name[]
*/
const char *name = sd->toPrettyChars();
size_t namelen = strlen(name);
dtdword(pdt, namelen);
//dtabytes(pdt, TYnptr, 0, namelen + 1, name);
dtxoff(pdt, toSymbol(), offset, TYnptr);
offset += namelen + 1;
// void[] init;
dtdword(pdt, sd->structsize); // init.length
if (sd->zeroInit)
dtdword(pdt, 0); // NULL for 0 initialization
else
dtxoff(pdt, sd->toInitializer(), 0, TYnptr); // init.ptr
FuncDeclaration *fd;
FuncDeclaration *fdx;
TypeFunction *tf;
Type *ta;
Dsymbol *s;
static TypeFunction *tftohash;
static TypeFunction *tftostring;
if (!tftohash)
{
Scope sc;
tftohash = new TypeFunction(NULL, Type::thash_t, 0, LINKd);
tftohash = (TypeFunction *)tftohash->semantic(0, &sc);
tftostring = new TypeFunction(NULL, Type::tchar->arrayOf(), 0, LINKd);
tftostring = (TypeFunction *)tftostring->semantic(0, &sc);
}
TypeFunction *tfeqptr;
{
Scope sc;
Parameters *arguments = new Parameters;
#if STRUCTTHISREF
// arg type is ref const T
Parameter *arg = new Parameter(STCref, tc->constOf(), NULL, NULL);
#else
// arg type is const T*
Parameter *arg = new Parameter(STCin, tc->pointerTo(), NULL, NULL);
#endif
arguments->push(arg);
tfeqptr = new TypeFunction(arguments, Type::tint32, 0, LINKd);
tfeqptr = (TypeFunction *)tfeqptr->semantic(0, &sc);
}
#if 0
TypeFunction *tfeq;
{
Scope sc;
Array *arguments = new Array;
Parameter *arg = new Parameter(In, tc, NULL, NULL);
arguments->push(arg);
tfeq = new TypeFunction(arguments, Type::tint32, 0, LINKd);
tfeq = (TypeFunction *)tfeq->semantic(0, &sc);
}
#endif
s = search_function(sd, Id::tohash);
fdx = s ? s->isFuncDeclaration() : NULL;
if (fdx)
{ fd = fdx->overloadExactMatch(tftohash);
if (fd)
dtxoff(pdt, fd->toSymbol(), 0, TYnptr);
else
//fdx->error("must be declared as extern (D) uint toHash()");
dtdword(pdt, 0);
}
//.........这里部分代码省略.........
示例8: Loc
FuncDeclaration *StructDeclaration::buildOpAssign(Scope *sc)
{
if (FuncDeclaration *f = hasIdentityOpAssign(sc))
{
hasIdentityAssign = 1;
return f;
}
// Even if non-identity opAssign is defined, built-in identity opAssign
// will be defined.
if (!needOpAssign())
return NULL;
//printf("StructDeclaration::buildOpAssign() %s\n", toChars());
StorageClass stc = STCsafe | STCnothrow | STCpure;
Loc declLoc = this->loc;
Loc loc = Loc(); // internal code should have no loc to prevent coverage
if (dtor || postblit)
{
if (dtor)
stc = mergeFuncAttrs(stc, dtor->storage_class);
}
else
{
for (size_t i = 0; i < fields.dim; i++)
{
Dsymbol *s = fields[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v && v->isField());
if (v->storage_class & STCref)
continue;
Type *tv = v->type->toBasetype();
while (tv->ty == Tsarray)
{ TypeSArray *ta = (TypeSArray *)tv;
tv = tv->nextOf()->toBasetype();
}
if (tv->ty == Tstruct)
{ TypeStruct *ts = (TypeStruct *)tv;
StructDeclaration *sd = ts->sym;
if (FuncDeclaration *f = sd->hasIdentityOpAssign(sc))
stc = mergeFuncAttrs(stc, f->storage_class);
}
}
}
Parameters *fparams = new Parameters;
fparams->push(new Parameter(STCnodtor, type, Id::p, NULL));
Type *tf = new TypeFunction(fparams, handle, 0, LINKd, stc | STCref);
FuncDeclaration *fop = new FuncDeclaration(declLoc, Loc(), Id::assign, stc, tf);
Expression *e = NULL;
if (stc & STCdisable)
{
}
else if (dtor || postblit)
{
/* Do swap this and rhs
* tmp = this; this = s; tmp.dtor();
*/
//printf("\tswap copy\n");
Identifier *idtmp = Lexer::uniqueId("__tmp");
VarDeclaration *tmp;
AssignExp *ec = NULL;
if (dtor)
{
tmp = new VarDeclaration(loc, type, idtmp, new VoidInitializer(loc));
tmp->noscope = 1;
tmp->storage_class |= STCctfe;
e = new DeclarationExp(loc, tmp);
ec = new AssignExp(loc,
new VarExp(loc, tmp),
new ThisExp(loc)
);
ec->op = TOKblit;
e = Expression::combine(e, ec);
}
ec = new AssignExp(loc,
new ThisExp(loc),
new IdentifierExp(loc, Id::p));
ec->op = TOKblit;
e = Expression::combine(e, ec);
if (dtor)
{
/* Instead of running the destructor on s, run it
* on tmp. This avoids needing to copy tmp back in to s.
*/
Expression *ec2 = new DotVarExp(loc, new VarExp(loc, tmp), dtor, 0);
ec2 = new CallExp(loc, ec2);
e = Expression::combine(e, ec2);
}
}
else
{
/* Do memberwise copy
*/
//printf("\tmemberwise copy\n");
for (size_t i = 0; i < fields.dim; i++)
{
//.........这里部分代码省略.........
示例9: visit
void visit(TypeInfoStructDeclaration *d)
{
//printf("TypeInfoStructDeclaration::toDt() '%s'\n", toChars());
if (global.params.is64bit)
verifyStructSize(Type::typeinfostruct, 17 * Target::ptrsize);
else
verifyStructSize(Type::typeinfostruct, 15 * Target::ptrsize);
dtxoff(pdt, Type::typeinfostruct->toVtblSymbol(), 0); // vtbl for TypeInfo_Struct
dtsize_t(pdt, 0); // monitor
assert(d->tinfo->ty == Tstruct);
TypeStruct *tc = (TypeStruct *)d->tinfo;
StructDeclaration *sd = tc->sym;
if (!sd->members)
return;
/* Put out:
* char[] name;
* void[] init;
* hash_t function(in void*) xtoHash;
* bool function(in void*, in void*) xopEquals;
* int function(in void*, in void*) xopCmp;
* string function(const(void)*) xtoString;
* StructFlags m_flags;
* //xgetMembers;
* xdtor;
* xpostblit;
* uint m_align;
* version (X86_64)
* TypeInfo m_arg1;
* TypeInfo m_arg2;
* xgetRTInfo
*/
const char *name = sd->toPrettyChars();
size_t namelen = strlen(name);
dtsize_t(pdt, namelen);
dtabytes(pdt, 0, namelen + 1, name);
// void[] init;
dtsize_t(pdt, sd->structsize); // init.length
if (sd->zeroInit)
dtsize_t(pdt, 0); // NULL for 0 initialization
else
dtxoff(pdt, sd->toInitializer(), 0); // init.ptr
if (FuncDeclaration *fd = search_toHash(sd))
{
dtxoff(pdt, fd->toSymbol(), 0);
TypeFunction *tf = (TypeFunction *)fd->type;
assert(tf->ty == Tfunction);
/* I'm a little unsure this is the right way to do it. Perhaps a better
* way would to automatically add these attributes to any struct member
* function with the name "toHash".
* So I'm leaving this here as an experiment for the moment.
*/
if (!tf->isnothrow || tf->trust == TRUSTsystem /*|| tf->purity == PUREimpure*/)
warning(fd->loc, "toHash() must be declared as extern (D) size_t toHash() const nothrow @safe, not %s", tf->toChars());
}
else
dtsize_t(pdt, 0);
if (sd->xeq)
dtxoff(pdt, sd->xeq->toSymbol(), 0);
else
dtsize_t(pdt, 0);
if (sd->xcmp)
dtxoff(pdt, sd->xcmp->toSymbol(), 0);
else
dtsize_t(pdt, 0);
if (FuncDeclaration *fd = search_toString(sd))
{
dtxoff(pdt, fd->toSymbol(), 0);
}
else
dtsize_t(pdt, 0);
// StructFlags m_flags;
StructFlags::Type m_flags = 0;
if (tc->hasPointers()) m_flags |= StructFlags::hasPointers;
dtsize_t(pdt, m_flags);
#if 0
// xgetMembers
FuncDeclaration *sgetmembers = sd->findGetMembers();
if (sgetmembers)
dtxoff(pdt, sgetmembers->toSymbol(), 0);
else
dtsize_t(pdt, 0); // xgetMembers
#endif
// xdtor
FuncDeclaration *sdtor = sd->dtor;
if (sdtor)
dtxoff(pdt, sdtor->toSymbol(), 0);
//.........这里部分代码省略.........
示例10: visit
void visit(TypeInfoStructDeclaration *decl) override {
IF_LOG Logger::println("TypeInfoStructDeclaration::llvmDefine() %s",
decl->toChars());
LOG_SCOPE;
// make sure struct is resolved
assert(decl->tinfo->ty == Tstruct);
TypeStruct *tc = static_cast<TypeStruct *>(decl->tinfo);
StructDeclaration *sd = tc->sym;
// handle opaque structs
if (!sd->members) {
RTTIBuilder b(Type::typeinfostruct);
b.finalize(getIrGlobal(decl));
return;
}
// can't emit typeinfo for forward declarations
if (sd->sizeok != SIZEOKdone) {
sd->error("cannot emit TypeInfo for forward declaration");
fatal();
}
DtoResolveStruct(sd);
if (TemplateInstance *ti = sd->isInstantiated()) {
if (!ti->needsCodegen()) {
assert(ti->minst || sd->requestTypeInfo);
// We won't emit ti, so emit the special member functions in here.
if (sd->xeq && sd->xeq != StructDeclaration::xerreq) {
Declaration_codegen(sd->xeq);
}
if (sd->xcmp && sd->xcmp != StructDeclaration::xerrcmp) {
Declaration_codegen(sd->xcmp);
}
if (FuncDeclaration *ftostr = search_toString(sd)) {
Declaration_codegen(ftostr);
}
if (sd->xhash) {
Declaration_codegen(sd->xhash);
}
if (sd->postblit) {
Declaration_codegen(sd->postblit);
}
if (sd->dtor) {
Declaration_codegen(sd->dtor);
}
}
}
IrAggr *iraggr = getIrAggr(sd);
RTTIBuilder b(Type::typeinfostruct);
// On x86_64, class TypeInfo_Struct contains 2 additional fields
// (m_arg1/m_arg2) which are used for the X86_64 System V ABI varargs
// implementation. They are not present on any other cpu/os.
unsigned expectedFields = 12;
if (global.params.targetTriple->getArch() == llvm::Triple::x86_64) {
expectedFields += 2;
}
if (Type::typeinfostruct->fields.dim != expectedFields) {
error(Loc(), "Unexpected number of object.TypeInfo_Struct fields; "
"druntime version does not match compiler");
fatal();
}
// char[] name
b.push_string(sd->toPrettyChars());
// void[] init
// The protocol is to write a null pointer for zero-initialized arrays. The
// length field is always needed for tsize().
llvm::Constant *initPtr;
if (tc->isZeroInit(Loc())) {
initPtr = getNullValue(getVoidPtrType());
} else {
initPtr = iraggr->getInitSymbol();
}
b.push_void_array(getTypeStoreSize(DtoType(tc)), initPtr);
// toHash
FuncDeclaration *fd = sd->xhash;
b.push_funcptr(fd);
// opEquals
fd = sd->xeq;
b.push_funcptr(fd);
// opCmp
fd = sd->xcmp;
b.push_funcptr(fd);
// toString
fd = search_toString(sd);
b.push_funcptr(fd);
// uint m_flags;
unsigned hasptrs = tc->hasPointers() ? 1 : 0;
b.push_uint(hasptrs);
//.........这里部分代码省略.........
示例11: visit
void visit(TypeInfoStructDeclaration *decl)
{
IF_LOG Logger::println("TypeInfoStructDeclaration::llvmDefine() %s", decl->toChars());
LOG_SCOPE;
// make sure struct is resolved
assert(decl->tinfo->ty == Tstruct);
TypeStruct *tc = static_cast<TypeStruct *>(decl->tinfo);
StructDeclaration *sd = tc->sym;
// handle opaque structs
if (!sd->members) {
RTTIBuilder b(Type::typeinfostruct);
b.finalize(getIrGlobal(decl));
return;
}
// can't emit typeinfo for forward declarations
if (sd->sizeok != SIZEOKdone)
{
sd->error("cannot emit TypeInfo for forward declaration");
fatal();
}
DtoResolveStruct(sd);
IrAggr* iraggr = getIrAggr(sd);
RTTIBuilder b(Type::typeinfostruct);
// char[] name
b.push_string(sd->toPrettyChars());
// void[] init
// The protocol is to write a null pointer for zero-initialized arrays. The
// length field is always needed for tsize().
llvm::Constant *initPtr;
if (tc->isZeroInit(Loc()))
initPtr = getNullValue(getVoidPtrType());
else
initPtr = iraggr->getInitSymbol();
b.push_void_array(getTypeStoreSize(DtoType(tc)), initPtr);
// well use this module for all overload lookups
// toHash
FuncDeclaration* fd = sd->xhash;
b.push_funcptr(fd);
// opEquals
fd = sd->xeq;
b.push_funcptr(fd);
// opCmp
fd = sd->xcmp;
b.push_funcptr(fd);
// toString
fd = search_toString(sd);
b.push_funcptr(fd);
// uint m_flags;
unsigned hasptrs = tc->hasPointers() ? 1 : 0;
b.push_uint(hasptrs);
// On x86_64, class TypeInfo_Struct contains 2 additional fields
// (m_arg1/m_arg2) which are used for the X86_64 System V ABI varargs
// implementation. They are not present on any other cpu/os.
assert((global.params.targetTriple.getArch() != llvm::Triple::x86_64 && Type::typeinfostruct->fields.dim == 11) ||
(global.params.targetTriple.getArch() == llvm::Triple::x86_64 && Type::typeinfostruct->fields.dim == 13));
//void function(void*) xdtor;
b.push_funcptr(sd->dtor);
//void function(void*) xpostblit;
FuncDeclaration *xpostblit = sd->postblit;
if (xpostblit && sd->postblit->storage_class & STCdisable)
xpostblit = 0;
b.push_funcptr(xpostblit);
//uint m_align;
b.push_uint(tc->alignsize());
if (global.params.is64bit)
{
// TypeInfo m_arg1;
// TypeInfo m_arg2;
Type *t = sd->arg1type;
for (unsigned i = 0; i < 2; i++)
{
if (t)
{
t = t->merge();
b.push_typeinfo(t);
}
else
b.push_null(Type::dtypeinfo->type);
t = sd->arg2type;
}
}
//.........这里部分代码省略.........
示例12: assert
Expression *StructInitializer::fill(Scope *sc, Type *t, NeedInterpret needInterpret)
{
//printf("StructInitializer::fill(sc = %p, '%s')\n", sc, toChars());
assert(t->ty == Tstruct);
StructDeclaration *sd = ((TypeStruct *)t)->sym;
sd->size(loc);
if (sd->sizeok != SIZEOKdone)
return new ErrorExp();
size_t nfields = sd->fields.dim - sd->isNested();
Expressions *elements = new Expressions();
elements->setDim(nfields);
for (size_t i = 0; i < elements->dim; i++)
(*elements)[i] = NULL;
// Run semantic for explicitly given initializers
bool errors = false;
for (size_t fieldi = 0, i = 0; i < field.dim; i++)
{
if (Identifier *id = field[i])
{
Dsymbol *s = sd->search(loc, id, 0);
if (!s)
{
s = sd->search_correct(id);
if (s)
error(loc, "'%s' is not a member of '%s', did you mean '%s %s'?",
id->toChars(), sd->toChars(), s->kind(), s->toChars());
else
error(loc, "'%s' is not a member of '%s'", id->toChars(), sd->toChars());
return new ErrorExp();
}
s = s->toAlias();
// Find out which field index it is
for (fieldi = 0; 1; fieldi++)
{
if (fieldi >= nfields)
{
error(loc, "%s.%s is not a per-instance initializable field",
sd->toChars(), s->toChars());
return new ErrorExp();
}
if (s == sd->fields[fieldi])
break;
}
}
else if (fieldi >= nfields)
{
error(loc, "too many initializers for %s", sd->toChars());
return new ErrorExp();
}
VarDeclaration *vd = sd->fields[fieldi];
if ((*elements)[fieldi])
{
error(loc, "duplicate initializer for field '%s'", vd->toChars());
errors = true;
continue;
}
for (size_t j = 0; j < nfields; j++)
{
VarDeclaration *v2 = sd->fields[j];
bool overlap = (vd->offset < v2->offset + v2->type->size() &&
v2->offset < vd->offset + vd->type->size());
if (overlap && (*elements)[j])
{
error(loc, "overlapping initialization for field %s and %s",
v2->toChars(), vd->toChars());
errors = true;
continue;
}
}
assert(sc);
Initializer *iz = value[i];
iz = iz->semantic(sc, vd->type->addMod(t->mod), needInterpret);
Expression *ex = iz->toExpression();
if (ex->op == TOKerror)
{
errors = true;
continue;
}
value[i] = iz;
(*elements)[fieldi] = ex;
++fieldi;
}
if (errors)
return new ErrorExp();
// Fill in missing any elements with default initializers
for (size_t i = 0; i < elements->dim; i++)
{
if ((*elements)[i])
continue;
VarDeclaration *vd = sd->fields[i];
VarDeclaration *vx = vd;
if (vd->init && vd->init->isVoidInitializer())
vx = NULL;
// Find overlapped fields with the hole [vd->offset .. vd->offset->size()].
//.........这里部分代码省略.........
示例13: error
Initializer *StructInitializer::semantic(Scope *sc, Type *t, NeedInterpret needInterpret)
{
int errors = 0;
//printf("StructInitializer::semantic(t = %s) %s\n", t->toChars(), toChars());
vars.setDim(field.dim);
t = t->toBasetype();
if (t->ty == Tstruct)
{
unsigned fieldi = 0;
TypeStruct *ts = (TypeStruct *)t;
ad = ts->sym;
if (ad->ctor)
error(loc, "%s %s has constructors, cannot use { initializers }, use %s( initializers ) instead",
ad->kind(), ad->toChars(), ad->toChars());
StructDeclaration *sd = ad->isStructDeclaration();
assert(sd);
sd->size(loc);
if (sd->sizeok != SIZEOKdone)
{
error(loc, "struct %s is forward referenced", sd->toChars());
errors = 1;
goto Lerror;
}
size_t nfields = sd->fields.dim;
if (sd->isnested)
nfields--;
for (size_t i = 0; i < field.dim; i++)
{
Identifier *id = field[i];
Initializer *val = value[i];
Dsymbol *s;
VarDeclaration *v;
if (id == NULL)
{
if (fieldi >= nfields)
{ error(loc, "too many initializers for %s", ad->toChars());
errors = 1;
field.remove(i);
i--;
continue;
}
else
{
s = ad->fields[fieldi];
}
}
else
{
//s = ad->symtab->lookup(id);
s = ad->search(loc, id, 0);
if (!s)
{
s = ad->search_correct(id);
if (s)
error(loc, "'%s' is not a member of '%s', did you mean '%s %s'?",
id->toChars(), t->toChars(), s->kind(), s->toChars());
else
error(loc, "'%s' is not a member of '%s'", id->toChars(), t->toChars());
errors = 1;
continue;
}
s = s->toAlias();
// Find out which field index it is
for (fieldi = 0; 1; fieldi++)
{
if (fieldi >= nfields)
{
error(loc, "%s.%s is not a per-instance initializable field",
t->toChars(), s->toChars());
errors = 1;
break;
}
if (s == ad->fields[fieldi])
break;
}
}
if (s && (v = s->isVarDeclaration()) != NULL)
{
val = val->semantic(sc, v->type->addMod(t->mod), needInterpret);
value[i] = val;
vars[i] = v;
}
else
{ error(loc, "%s is not a field of %s", id ? id->toChars() : s->toChars(), ad->toChars());
errors = 1;
}
fieldi++;
}
}
else if (t->ty == Tdelegate && value.dim == 0)
{ /* Rewrite as empty delegate literal { }
*/
Parameters *arguments = new Parameters;
Type *tf = new TypeFunction(arguments, NULL, 0, LINKd);
FuncLiteralDeclaration *fd = new FuncLiteralDeclaration(loc, 0, tf, TOKdelegate, NULL);
fd->fbody = new CompoundStatement(loc, new Statements());
//.........这里部分代码省略.........
示例14: printf
Expression *TraitsExp::semantic(Scope *sc)
{
#if LOGSEMANTIC
printf("TraitsExp::semantic() %s\n", toChars());
#endif
if (ident != Id::compiles && ident != Id::isSame &&
ident != Id::identifier)
{
TemplateInstance::semanticTiargs(loc, sc, args, 1);
}
size_t dim = args ? args->dim : 0;
Declaration *d;
#define ISTYPE(cond) \
for (size_t i = 0; i < dim; i++) \
{ Type *t = getType((*args)[i]); \
if (!t) \
goto Lfalse; \
if (!(cond)) \
goto Lfalse; \
} \
if (!dim) \
goto Lfalse; \
goto Ltrue;
#define ISDSYMBOL(cond) \
for (size_t i = 0; i < dim; i++) \
{ Dsymbol *s = getDsymbol((*args)[i]); \
if (!s) \
goto Lfalse; \
if (!(cond)) \
goto Lfalse; \
} \
if (!dim) \
goto Lfalse; \
goto Ltrue;
if (ident == Id::isArithmetic)
{
ISTYPE(t->isintegral() || t->isfloating())
}
else if (ident == Id::isFloating)
{
ISTYPE(t->isfloating())
}
else if (ident == Id::isIntegral)
{
ISTYPE(t->isintegral())
}
else if (ident == Id::isScalar)
{
ISTYPE(t->isscalar())
}
else if (ident == Id::isUnsigned)
{
ISTYPE(t->isunsigned())
}
else if (ident == Id::isAssociativeArray)
{
ISTYPE(t->toBasetype()->ty == Taarray)
}
else if (ident == Id::isStaticArray)
{
ISTYPE(t->toBasetype()->ty == Tsarray)
}
else if (ident == Id::isAbstractClass)
{
ISTYPE(t->toBasetype()->ty == Tclass && ((TypeClass *)t->toBasetype())->sym->isAbstract())
}
else if (ident == Id::isFinalClass)
{
ISTYPE(t->toBasetype()->ty == Tclass && ((TypeClass *)t->toBasetype())->sym->storage_class & STCfinal)
}
else if (ident == Id::isPOD)
{
if (dim != 1)
goto Ldimerror;
RootObject *o = (*args)[0];
Type *t = isType(o);
StructDeclaration *sd;
if (!t)
{
error("type expected as second argument of __traits %s instead of %s", ident->toChars(), o->toChars());
goto Lfalse;
}
if (t->toBasetype()->ty == Tstruct
&& ((sd = (StructDeclaration *)(((TypeStruct *)t->toBasetype())->sym)) != NULL))
{
if (sd->isPOD())
goto Ltrue;
else
goto Lfalse;
}
goto Ltrue;
}
else if (ident == Id::isNested)
{
if (dim != 1)
//.........这里部分代码省略.........
示例15: visit
void visit(TypeStruct *t)
{
//printf("TypeStruct::toCtype() '%s'\n", t->sym->toChars());
Type *tm = t->mutableOf();
if (tm->ctype)
{
t->ctype = type_alloc(tybasic(tm->ctype->Tty));
t->ctype->Tcount++;
if (t->ctype->Tty == TYstruct)
{
Symbol *s = tm->ctype->Ttag;
t->ctype->Ttag = (Classsym *)s; // structure tag name
}
// Add modifiers
switch (t->mod)
{
case 0:
assert(0);
break;
case MODconst:
case MODwild:
case MODwildconst:
t->ctype->Tty |= mTYconst;
break;
case MODshared:
t->ctype->Tty |= mTYshared;
break;
case MODshared | MODconst:
case MODshared | MODwild:
case MODshared | MODwildconst:
t->ctype->Tty |= mTYshared | mTYconst;
break;
case MODimmutable:
t->ctype->Tty |= mTYimmutable;
break;
default:
assert(0);
}
}
else
{
StructDeclaration *sym = t->sym;
if (sym->ident == Id::__c_long_double)
{
t->ctype = type_fake(TYdouble);
t->ctype->Tcount++;
return;
}
t->ctype = type_struct_class(sym->toPrettyChars(true), sym->alignsize, sym->structsize,
sym->arg1type ? Type_toCtype(sym->arg1type) : NULL,
sym->arg2type ? Type_toCtype(sym->arg2type) : NULL,
sym->isUnionDeclaration() != 0,
false,
sym->isPOD() != 0);
tm->ctype = t->ctype;
/* Add in fields of the struct
* (after setting ctype to avoid infinite recursion)
*/
if (global.params.symdebug)
{
for (size_t i = 0; i < sym->fields.dim; i++)
{
VarDeclaration *v = sym->fields[i];
symbol_struct_addField(t->ctype->Ttag, v->ident->toChars(), Type_toCtype(v->type), v->offset);
}
}
}
//printf("t = %p, Tflags = x%x\n", ctype, ctype->Tflags);
}