MCPcopy Create free account
hub / github.com/GaijinEntertainment/daScript / evalAndFold

Method evalAndFold

src/ast/ast_const_folding.cpp:353–419  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

351 }
352
353 ExpressionPtr FoldingVisitor::evalAndFold ( Expression * expr ) {
354 if ( expr->type->baseType == Type::tString ) return evalAndFoldString(expr);
355 if ( expr->rtti_isConstant() ) return expr->clone();
356 bool failed;
357 vec4f value = eval(expr, failed);
358 if ( !failed ) {
359 if ( expr->type->isEnumT() ) {
360 int64_t ival = 0;
361 switch ( expr->type->enumType->baseType ) {
362 case Type::tInt8: ival = cast<int8_t>::to(value); break;
363 case Type::tUInt8: ival = cast<uint8_t>::to(value); break;
364 case Type::tInt16: ival = cast<int16_t>::to(value); break;
365 case Type::tUInt16: ival = cast<uint16_t>::to(value); break;
366 case Type::tInt: ival = cast<int32_t>::to(value); break;
367 case Type::tUInt: ival = cast<uint32_t>::to(value); break;
368 case Type::tBitfield: ival = cast<uint32_t>::to(value); break;
369 case Type::tBitfield8: ival = cast<uint8_t>::to(value); break;
370 case Type::tBitfield16: ival = cast<uint16_t>::to(value); break;
371 case Type::tBitfield64: ival = cast<uint64_t>::to(value); break;
372 case Type::tInt64: ival = cast<int64_t>::to(value); break;
373 case Type::tUInt64: ival = cast<uint64_t>::to(value); break;
374 default: DAS_ASSERTF(0,"we should not be here. unsupported enum type");
375 }
376 auto cef = expr->type->enumType->find(ival, "");
377 if ( cef.empty() ) return expr; // it folded to unsupported value
378 auto sim = new ExprConstEnumeration(expr->at, cef, expr->type);
379 sim->type = expr->type->enumType->makeEnumType();
380 sim->constexpression = true;
381 sim->at = encloseAt(expr);
382 sim->value = value;
383 sim->foldedNonConst = !expr->type->constant;
384 reportFolding();
385 return sim;
386 } else if ( expr->type->isBitfield() ) {
387 uint64_t ival = 0;
388 switch ( expr->type->baseType ) {
389 case Type::tBitfield8: ival = cast<uint8_t>::to(value); break;
390 case Type::tBitfield16: ival = cast<uint16_t>::to(value); break;
391 case Type::tBitfield: ival = cast<uint32_t>::to(value); break;
392 case Type::tBitfield64: ival = cast<uint64_t>::to(value); break;
393 default: DAS_ASSERTF(0,"we should not be here. unsupported bitfield type");
394 }
395 auto sim = new ExprConstBitfield(expr->at, ival);
396 sim->type = new TypeDecl(*expr->type);
397 sim->constexpression = true;
398 sim->at = encloseAt(expr);
399 sim->foldedNonConst = !expr->type->constant;
400 sim->baseType = expr->type->baseType;
401 sim->bitfieldType = new TypeDecl(*expr->type);
402 reportFolding();
403 return sim;
404 } else {
405 auto wasRef = expr->type->ref;
406 expr->type->ref = false;
407 auto sim = Program::makeConst(expr->at, expr->type, value);
408 expr->type->ref = wasRef;
409 sim->type = new TypeDecl(*expr->type);
410 sim->constexpression = true;

Callers

nothing calls this directly

Calls 8

toFunction · 0.85
encloseAtFunction · 0.85
isEnumTMethod · 0.80
isBitfieldMethod · 0.80
cloneMethod · 0.45
findMethod · 0.45
emptyMethod · 0.45
makeEnumTypeMethod · 0.45

Tested by

no test coverage detected