本文整理汇总了C++中StringExp::length方法的典型用法代码示例。如果您正苦于以下问题:C++ StringExp::length方法的具体用法?C++ StringExp::length怎么用?C++ StringExp::length使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringExp
的用法示例。
在下文中一共展示了StringExp::length方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ErrorInitializer
Initializer *ExpInitializer::semantic(Scope *sc, Type *t, NeedInterpret needInterpret)
{
//printf("ExpInitializer::semantic(%s), type = %s\n", exp->toChars(), t->toChars());
if (needInterpret) sc = sc->startCTFE();
exp = exp->semantic(sc);
exp = resolveProperties(sc, exp);
if (needInterpret) sc = sc->endCTFE();
if (exp->op == TOKerror)
return new ErrorInitializer();
unsigned int olderrors = global.errors;
if (needInterpret)
{
// If the result will be implicitly cast, move the cast into CTFE
// to avoid premature truncation of polysemous types.
// eg real [] x = [1.1, 2.2]; should use real precision.
if (exp->implicitConvTo(t))
{
exp = exp->implicitCastTo(sc, t);
}
exp = exp->ctfeInterpret();
}
else
{
exp = exp->optimize(WANTvalue);
}
if (!global.gag && olderrors != global.errors)
return this; // Failed, suppress duplicate error messages
if (exp->type->ty == Ttuple && ((TypeTuple *)exp->type)->arguments->dim == 0)
{
Type *et = exp->type;
exp = new TupleExp(exp->loc, new Expressions());
exp->type = et;
}
if (exp->op == TOKtype)
{
exp->error("initializer must be an expression, not '%s'", exp->toChars());
return new ErrorInitializer();
}
// Make sure all pointers are constants
if (needInterpret && hasNonConstPointers(exp))
{
exp->error("cannot use non-constant CTFE pointer in an initializer '%s'", exp->toChars());
return new ErrorInitializer();
}
Type *tb = t->toBasetype();
Type *ti = exp->type->toBasetype();
if (exp->op == TOKtuple && expandTuples && !exp->implicitConvTo(t))
return new ExpInitializer(loc, exp);
/* Look for case of initializing a static array with a too-short
* string literal, such as:
* char[5] foo = "abc";
* Allow this by doing an explicit cast, which will lengthen the string
* literal.
*/
if (exp->op == TOKstring && tb->ty == Tsarray)
{
StringExp *se = (StringExp *)exp;
Type *typeb = se->type->toBasetype();
TY tynto = tb->nextOf()->ty;
if (!se->committed &&
(typeb->ty == Tarray || typeb->ty == Tsarray) &&
(tynto == Tchar || tynto == Twchar || tynto == Tdchar) &&
se->length((int)tb->nextOf()->size()) < ((TypeSArray *)tb)->dim->toInteger())
{
exp = se->castTo(sc, t);
goto L1;
}
}
// Look for implicit constructor call
if (tb->ty == Tstruct &&
!(ti->ty == Tstruct && tb->toDsymbol(sc) == ti->toDsymbol(sc)) &&
!exp->implicitConvTo(t))
{
StructDeclaration *sd = ((TypeStruct *)tb)->sym;
if (sd->ctor)
{
// Rewrite as S().ctor(exp)
Expression *e;
e = new StructLiteralExp(loc, sd, NULL);
e = new DotIdExp(loc, e, Id::ctor);
e = new CallExp(loc, e, exp);
e = e->semantic(sc);
if (needInterpret)
exp = e->ctfeInterpret();
else
exp = e->optimize(WANTvalue);
}
}
// Look for the case of statically initializing an array
// with a single member.
if (tb->ty == Tsarray &&
!tb->nextOf()->equals(ti->toBasetype()->nextOf()) &&
//.........这里部分代码省略.........
示例2: ErrorExp
//.........这里部分代码省略.........
td = td->overroot; // then get the start
Expression *ex = new TemplateExp(e->loc, td, f);
ex = ex->semantic(sc);
return ex;
}
if (FuncLiteralDeclaration *fld = f->isFuncLiteralDeclaration())
{
// Directly translate to VarExp instead of FuncExp
Expression *ex = new VarExp(e->loc, fld, 1);
return ex->semantic(sc);
}
}
return (new DsymbolExp(e->loc, s))->semantic(sc);
}
else if (e->ident == Id::hasMember ||
e->ident == Id::getMember ||
e->ident == Id::getOverloads ||
e->ident == Id::getVirtualMethods ||
e->ident == Id::getVirtualFunctions)
{
if (dim != 2)
goto Ldimerror;
RootObject *o = (*e->args)[0];
Expression *ex = isExpression((*e->args)[1]);
if (!ex)
{
e->error("expression expected as second argument of __traits %s", e->ident->toChars());
goto Lfalse;
}
ex = ex->ctfeInterpret();
StringExp *se = ex->toStringExp();
if (!se || se->length() == 0)
{
e->error("string expected as second argument of __traits %s instead of %s", e->ident->toChars(), ex->toChars());
goto Lfalse;
}
se = se->toUTF8(sc);
if (se->sz != 1)
{
e->error("string must be chars");
goto Lfalse;
}
Identifier *id = Lexer::idPool((char *)se->string);
/* Prefer dsymbol, because it might need some runtime contexts.
*/
Dsymbol *sym = getDsymbol(o);
if (sym)
{
ex = new DsymbolExp(e->loc, sym);
ex = new DotIdExp(e->loc, ex, id);
}
else if (Type *t = isType(o))
ex = typeDotIdExp(e->loc, t, id);
else if (Expression *ex2 = isExpression(o))
ex = new DotIdExp(e->loc, ex2, id);
else
{
e->error("invalid first argument");
goto Lfalse;
}
if (e->ident == Id::hasMember)
{
示例3: fit
/***************************************
* Fit elements[] to the corresponding type of field[].
* Input:
* loc
* sc
* elements The explicit arguments that given to construct object.
* stype The constructed object type.
* Returns false if any errors occur.
* Otherwise, returns true and elements[] are rewritten for the output.
*/
bool StructDeclaration::fit(Loc loc, Scope *sc, Expressions *elements, Type *stype)
{
if (!elements)
return true;
size_t nfields = fields.dim - isNested();
size_t offset = 0;
for (size_t i = 0; i < elements->dim; i++)
{
Expression *e = (*elements)[i];
if (!e)
continue;
e = resolveProperties(sc, e);
if (i >= nfields)
{
if (i == fields.dim - 1 && isNested() && e->op == TOKnull)
{
// CTFE sometimes creates null as hidden pointer; we'll allow this.
continue;
}
::error(loc, "more initializers than fields (%d) of %s", nfields, toChars());
return false;
}
VarDeclaration *v = fields[i];
if (v->offset < offset)
{
::error(loc, "overlapping initialization for %s", v->toChars());
return false;
}
offset = (unsigned)(v->offset + v->type->size());
Type *t = v->type;
if (stype)
t = t->addMod(stype->mod);
Type *origType = t;
Type *tb = t->toBasetype();
/* Look for case of initializing a static array with a too-short
* string literal, such as:
* char[5] foo = "abc";
* Allow this by doing an explicit cast, which will lengthen the string
* literal.
*/
if (e->op == TOKstring && tb->ty == Tsarray)
{
StringExp *se = (StringExp *)e;
Type *typeb = se->type->toBasetype();
TY tynto = tb->nextOf()->ty;
if (!se->committed &&
(typeb->ty == Tarray || typeb->ty == Tsarray) &&
(tynto == Tchar || tynto == Twchar || tynto == Tdchar) &&
se->length((int)tb->nextOf()->size()) < ((TypeSArray *)tb)->dim->toInteger())
{
e = se->castTo(sc, t);
goto L1;
}
}
while (!e->implicitConvTo(t) && tb->ty == Tsarray)
{
/* Static array initialization, as in:
* T[3][5] = e;
*/
t = tb->nextOf();
tb = t->toBasetype();
}
if (!e->implicitConvTo(t))
t = origType; // restore type for better diagnostic
e = e->implicitCastTo(sc, t);
L1:
if (e->op == TOKerror)
return false;
(*elements)[i] = e->isLvalue() ? callCpCtor(sc, e) : valueNoDtor(e);
}
return true;
}