exprnode.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. #include "std.h"
  2. #include "nodes.h"
  3. #include <math.h>
  4. #include <float.h>
  5. //////////////////////////////////
  6. // Cast an expression to a type //
  7. //////////////////////////////////
  8. ExprNode *ExprNode::castTo( Type *ty,Environ *e ){
  9. if( !sem_type->canCastTo( ty ) ){
  10. ex( "Illegal type conversion" );
  11. }
  12. ExprNode *cast=d_new CastNode( this,ty );
  13. return cast->semant( e );
  14. }
  15. ExprNode *CastNode::semant( Environ *e ){
  16. if( !expr->sem_type ){
  17. expr=expr->semant( e );
  18. }
  19. if( ConstNode *c=expr->constNode() ){
  20. ExprNode *e;
  21. if( type==Type::int_type ) e=d_new IntConstNode( c->intValue() );
  22. else if( type==Type::float_type ) e=d_new FloatConstNode( c->floatValue() );
  23. else e=d_new StringConstNode( c->stringValue() );
  24. delete this;
  25. return e;
  26. }
  27. sem_type=type;
  28. return this;
  29. }
  30. //////////////////////////////////
  31. // Cast an expression to a type //
  32. //////////////////////////////////
  33. TNode *CastNode::translate( Codegen *g ){
  34. TNode *t=expr->translate( g );
  35. if( expr->sem_type==Type::float_type && sem_type==Type::int_type ){
  36. //float->int
  37. return d_new TNode( IR_CAST,t,0 );
  38. }
  39. if( expr->sem_type==Type::int_type && sem_type==Type::float_type ){
  40. //int->float
  41. return d_new TNode( IR_FCAST,t,0 );
  42. }
  43. if( expr->sem_type==Type::string_type && sem_type==Type::int_type ){
  44. //str->int
  45. return call( "__bbStrToInt",t );
  46. }
  47. if( expr->sem_type==Type::int_type && sem_type==Type::string_type ){
  48. //int->str
  49. return call( "__bbStrFromInt",t );
  50. }
  51. if( expr->sem_type==Type::string_type && sem_type==Type::float_type ){
  52. //str->float
  53. return fcall( "__bbStrToFloat",t );
  54. }
  55. if( expr->sem_type==Type::float_type && sem_type==Type::string_type ){
  56. //float->str
  57. return call( "__bbStrFromFloat",t );
  58. }
  59. if( expr->sem_type->structType() && sem_type==Type::string_type ){
  60. //obj->str
  61. return call( "__bbObjToStr",t );
  62. }
  63. return t;
  64. }
  65. /////////////////////////////
  66. // Sequence of Expressions //
  67. /////////////////////////////
  68. void ExprSeqNode::semant( Environ *e ){
  69. for( int k=0;k<exprs.size();++k ){
  70. if( exprs[k] ) exprs[k]=exprs[k]->semant( e );
  71. }
  72. }
  73. TNode *ExprSeqNode::translate( Codegen *g,bool cfunc ){
  74. TNode *t=0,*l=0;
  75. for( int k=0;k<exprs.size();++k ){
  76. TNode *q=exprs[k]->translate(g);
  77. if( cfunc ){
  78. Type *ty=exprs[k]->sem_type;
  79. if( ty->stringType() ){
  80. q=call( "__bbStrToCStr",q );
  81. }else if( ty->structType() ){
  82. q=d_new TNode( IR_MEM,q );
  83. }else if( ty==Type::void_type ){
  84. q=d_new TNode( IR_MEM,add(q,iconst(4)) );
  85. }
  86. }
  87. TNode *p;
  88. p=d_new TNode( IR_ARG,0,0,k*4 );
  89. p=d_new TNode( IR_MEM,p,0 );
  90. p=d_new TNode( IR_MOVE,q,p );
  91. p=d_new TNode( IR_SEQ,p,0 );
  92. if( l ) l->r=p;
  93. else t=p;
  94. l=p;
  95. }
  96. return t;
  97. }
  98. void ExprSeqNode::castTo( DeclSeq *decls,Environ *e,bool cfunc ){
  99. if( exprs.size()>decls->size() ) ex( "Too many parameters" );
  100. for( int k=0;k<decls->size();++k ){
  101. Decl *d=decls->decls[k];
  102. if( k<exprs.size() && exprs[k] ){
  103. if( cfunc && d->type->structType() ){
  104. if( exprs[k]->sem_type->structType() ){
  105. }else if( exprs[k]->sem_type->intType() ){
  106. exprs[k]->sem_type=Type::void_type;
  107. }else{
  108. ex( "Illegal type conversion" );
  109. }
  110. continue;
  111. }
  112. exprs[k]=exprs[k]->castTo( d->type,e );
  113. }else{
  114. if( !d->defType ) ex( "Not enough parameters" );
  115. ExprNode *expr=constValue( d->defType );
  116. if( k<exprs.size() ) exprs[k]=expr;
  117. else exprs.push_back( expr );
  118. }
  119. }
  120. }
  121. void ExprSeqNode::castTo( Type *t,Environ *e ){
  122. for( int k=0;k<exprs.size();++k ){
  123. exprs[k]=exprs[k]->castTo( t,e );
  124. }
  125. }
  126. ///////////////////
  127. // Function call //
  128. ///////////////////
  129. ExprNode *CallNode::semant( Environ *e ){
  130. Type *t=e->findType( tag );
  131. sem_decl=e->findFunc( ident );
  132. if( !sem_decl || !(sem_decl->kind & DECL_FUNC) ) ex( "Function '"+ident+"' not found" );
  133. FuncType *f=sem_decl->type->funcType();
  134. if( t && f->returnType!=t ) ex( "incorrect function return type" );
  135. exprs->semant( e );
  136. exprs->castTo( f->params,e,f->cfunc );
  137. sem_type=f->returnType;
  138. return this;
  139. }
  140. TNode *CallNode::translate( Codegen *g ){
  141. FuncType *f=sem_decl->type->funcType();
  142. TNode *t;
  143. TNode *l=global( "_f"+ident );
  144. TNode *r=exprs->translate( g,f->cfunc );
  145. if( f->userlib ){
  146. l=d_new TNode( IR_MEM,l );
  147. usedfuncs.insert( ident );
  148. }
  149. if( sem_type==Type::float_type ){
  150. t=d_new TNode( IR_FCALL,l,r,exprs->size()*4 );
  151. }else{
  152. t=d_new TNode( IR_CALL,l,r,exprs->size()*4 );
  153. }
  154. if( f->returnType->stringType() ){
  155. if( f->cfunc ){
  156. t=call( "__bbCStrToStr",t );
  157. }
  158. }
  159. return t;
  160. }
  161. /////////////////////////
  162. // Variable expression //
  163. /////////////////////////
  164. ExprNode *VarExprNode::semant( Environ *e ){
  165. var->semant( e );
  166. sem_type=var->sem_type;
  167. ConstType *c=sem_type->constType();
  168. if( !c ) return this;
  169. ExprNode *expr=constValue( c );
  170. delete this;return expr;
  171. }
  172. TNode *VarExprNode::translate( Codegen *g ){
  173. return var->load( g );
  174. }
  175. //////////////////////
  176. // Integer constant //
  177. //////////////////////
  178. IntConstNode::IntConstNode( int n ):value(n){
  179. sem_type=Type::int_type;
  180. }
  181. TNode *IntConstNode::translate( Codegen *g ){
  182. return d_new TNode( IR_CONST,0,0,value );
  183. }
  184. int IntConstNode::intValue(){
  185. return value;
  186. }
  187. float IntConstNode::floatValue(){
  188. return value;
  189. }
  190. string IntConstNode::stringValue(){
  191. return itoa( value );
  192. }
  193. ////////////////////
  194. // Float constant //
  195. ////////////////////
  196. FloatConstNode::FloatConstNode( float f ):value(f){
  197. sem_type=Type::float_type;
  198. }
  199. TNode *FloatConstNode::translate( Codegen *g ){
  200. return d_new TNode( IR_CONST,0,0,*(int*)&value );
  201. }
  202. int FloatConstNode::intValue(){
  203. float flt=value;
  204. int temp;
  205. _control87( _RC_NEAR|_PC_24|_EM_INVALID|_EM_ZERODIVIDE|_EM_OVERFLOW|_EM_UNDERFLOW|_EM_INEXACT|_EM_DENORMAL,0xfffff );
  206. _asm{
  207. fld [flt];
  208. fistp [temp];
  209. }
  210. _control87( _CW_DEFAULT,0xfffff );
  211. return temp;
  212. }
  213. float FloatConstNode::floatValue(){
  214. return value;
  215. }
  216. string FloatConstNode::stringValue(){
  217. return ftoa( value );
  218. }
  219. /////////////////////
  220. // String constant //
  221. /////////////////////
  222. StringConstNode::StringConstNode( const string &s ):value(s){
  223. sem_type=Type::string_type;
  224. }
  225. TNode *StringConstNode::translate( Codegen *g ){
  226. string lab=genLabel();
  227. g->s_data( value,lab );
  228. return call( "__bbStrConst",global( lab ) );
  229. }
  230. int StringConstNode::intValue(){
  231. return atoi( value );
  232. }
  233. float StringConstNode::floatValue(){
  234. return (float)atof( value );
  235. }
  236. string StringConstNode::stringValue(){
  237. return value;
  238. }
  239. ////////////////////
  240. // Unary operator //
  241. ////////////////////
  242. ExprNode *UniExprNode::semant( Environ *e ){
  243. expr=expr->semant( e );
  244. sem_type=expr->sem_type;
  245. if( sem_type!=Type::int_type && sem_type!=Type::float_type ) ex( "Illegal operator for type" );
  246. if( ConstNode *c=expr->constNode() ){
  247. ExprNode *e;
  248. if( sem_type==Type::int_type ){
  249. switch( op ){
  250. case '+':e=d_new IntConstNode( +c->intValue() );break;
  251. case '-':e=d_new IntConstNode( -c->intValue() );break;
  252. case ABS:e=d_new IntConstNode( c->intValue()>=0 ? c->intValue() : -c->intValue() );break;
  253. case SGN:e=d_new IntConstNode( c->intValue()>0 ? 1 : (c->intValue()<0 ? -1 : 0) );break;
  254. }
  255. }else{
  256. switch( op ){
  257. case '+':e=d_new FloatConstNode( +c->floatValue() );break;
  258. case '-':e=d_new FloatConstNode( -c->floatValue() );break;
  259. case ABS:e=d_new FloatConstNode( c->floatValue()>=0 ? c->floatValue() : -c->floatValue() );break;
  260. case SGN:e=d_new FloatConstNode( c->floatValue()>0 ? 1 : (c->floatValue()<0 ? -1 : 0) );break;
  261. }
  262. }
  263. delete this;
  264. return e;
  265. }
  266. return this;
  267. }
  268. TNode *UniExprNode::translate( Codegen *g ){
  269. int n=0;
  270. TNode *l=expr->translate( g );
  271. if( sem_type==Type::int_type ){
  272. switch( op ){
  273. case '+':return l;
  274. case '-':n=IR_NEG;break;
  275. case ABS:return call( "__bbAbs",l );
  276. case SGN:return call( "__bbSgn",l );
  277. }
  278. }else{
  279. switch( op ){
  280. case '+':return l;
  281. case '-':n=IR_FNEG;break;
  282. case ABS:return fcall( "__bbFAbs",l );
  283. case SGN:return fcall( "__bbFSgn",l );
  284. }
  285. }
  286. return d_new TNode( n,l,0 );
  287. }
  288. /////////////////////////////////////////////////////
  289. // boolean expression - accepts ints, returns ints //
  290. /////////////////////////////////////////////////////
  291. ExprNode *BinExprNode::semant( Environ *e ){
  292. lhs=lhs->semant(e);lhs=lhs->castTo( Type::int_type,e );
  293. rhs=rhs->semant(e);rhs=rhs->castTo( Type::int_type,e );
  294. ConstNode *lc=lhs->constNode(),*rc=rhs->constNode();
  295. if( lc && rc ){
  296. ExprNode *expr;
  297. switch( op ){
  298. case AND:expr=d_new IntConstNode( lc->intValue() & rc->intValue() );break;
  299. case OR: expr=d_new IntConstNode( lc->intValue() | rc->intValue() );break;
  300. case XOR:expr=d_new IntConstNode( lc->intValue() ^ rc->intValue() );break;
  301. case SHL:expr=d_new IntConstNode( lc->intValue()<< rc->intValue() );break;
  302. case SHR:expr=d_new IntConstNode( (unsigned)lc->intValue()>>rc->intValue() );break;
  303. case SAR:expr=d_new IntConstNode( lc->intValue()>> rc->intValue() );break;
  304. }
  305. delete this;
  306. return expr;
  307. }
  308. sem_type=Type::int_type;
  309. return this;
  310. }
  311. TNode *BinExprNode::translate( Codegen *g ){
  312. TNode *l=lhs->translate( g );
  313. TNode *r=rhs->translate( g );
  314. int n=0;
  315. switch( op ){
  316. case AND:n=IR_AND;break;case OR:n=IR_OR;break;case XOR:n=IR_XOR;break;
  317. case SHL:n=IR_SHL;break;case SHR:n=IR_SHR;break;case SAR:n=IR_SAR;break;
  318. }
  319. return d_new TNode( n,l,r );
  320. }
  321. ///////////////////////////
  322. // arithmetic expression //
  323. ///////////////////////////
  324. ExprNode *ArithExprNode::semant( Environ *e ){
  325. lhs=lhs->semant(e);
  326. rhs=rhs->semant(e);
  327. if( lhs->sem_type->structType() || rhs->sem_type->structType() ){
  328. ex( "Arithmetic operator cannot be applied to custom type objects" );
  329. }
  330. if( lhs->sem_type==Type::string_type || rhs->sem_type==Type::string_type ){
  331. //one side is a string - only + operator...
  332. if( op!='+' ) ex( "Operator cannot be applied to strings" );
  333. sem_type=Type::string_type;
  334. }else if( op=='^' || lhs->sem_type==Type::float_type || rhs->sem_type==Type::float_type ){
  335. //It's ^, or one side is a float
  336. sem_type=Type::float_type;
  337. }else{
  338. //must be 2 ints
  339. sem_type=Type::int_type;
  340. }
  341. lhs=lhs->castTo( sem_type,e );
  342. rhs=rhs->castTo( sem_type,e );
  343. ConstNode *lc=lhs->constNode(),*rc=rhs->constNode();
  344. if( rc && (op=='/' || op==MOD) ){
  345. if( (sem_type==Type::int_type && !rc->intValue()) || (sem_type==Type::float_type && !rc->floatValue()) ){
  346. ex( "Division by zero" );
  347. }
  348. }
  349. if( lc && rc ){
  350. ExprNode *expr;
  351. if( sem_type==Type::string_type ){
  352. expr=d_new StringConstNode( lc->stringValue()+rc->stringValue() );
  353. }else if( sem_type==Type::int_type ){
  354. switch( op ){
  355. case '+':expr=d_new IntConstNode( lc->intValue()+rc->intValue() );break;
  356. case '-':expr=d_new IntConstNode( lc->intValue()-rc->intValue() );break;
  357. case '*':expr=d_new IntConstNode( lc->intValue()*rc->intValue() );break;
  358. case '/':expr=d_new IntConstNode( lc->intValue()/rc->intValue() );break;
  359. case MOD:expr=d_new IntConstNode( lc->intValue()%rc->intValue() );break;
  360. }
  361. }else{
  362. switch( op ){
  363. case '+':expr=d_new FloatConstNode( lc->floatValue()+rc->floatValue() );break;
  364. case '-':expr=d_new FloatConstNode( lc->floatValue()-rc->floatValue() );break;
  365. case '*':expr=d_new FloatConstNode( lc->floatValue()*rc->floatValue() );break;
  366. case '/':expr=d_new FloatConstNode( lc->floatValue()/rc->floatValue() );break;
  367. case MOD:expr=d_new FloatConstNode( fmod( lc->floatValue(),rc->floatValue() ) );break;
  368. case '^':expr=d_new FloatConstNode( pow( lc->floatValue(),rc->floatValue() ) );break;
  369. }
  370. }
  371. delete this;
  372. return expr;
  373. }
  374. return this;
  375. }
  376. TNode *ArithExprNode::translate( Codegen *g ){
  377. TNode *l=lhs->translate( g );
  378. TNode *r=rhs->translate( g );
  379. if( sem_type==Type::string_type ){
  380. return call( "__bbStrConcat",l,r );
  381. }
  382. int n=0;
  383. if( sem_type==Type::int_type ){
  384. switch( op ){
  385. case '+':n=IR_ADD;break;case '-':n=IR_SUB;break;
  386. case '*':n=IR_MUL;break;case '/':n=IR_DIV;break;
  387. case MOD:return call( "__bbMod",l,r );
  388. }
  389. }else{
  390. switch( op ){
  391. case '+':n=IR_FADD;break;case '-':n=IR_FSUB;break;
  392. case '*':n=IR_FMUL;break;case '/':n=IR_FDIV;break;
  393. case MOD:return fcall( "__bbFMod",l,r );
  394. case '^':return fcall( "__bbFPow",l,r );
  395. }
  396. }
  397. return d_new TNode( n,l,r );
  398. }
  399. /////////////////////////
  400. // relation expression //
  401. /////////////////////////
  402. ExprNode *RelExprNode::semant( Environ *e ){
  403. lhs=lhs->semant(e);
  404. rhs=rhs->semant(e);
  405. if( lhs->sem_type->structType() || rhs->sem_type->structType() ){
  406. if( op!='=' && op!=NE ) ex( "Illegal operator for custom type objects" );
  407. opType=lhs->sem_type!=Type::null_type ? lhs->sem_type : rhs->sem_type;
  408. }else if( lhs->sem_type==Type::string_type || rhs->sem_type==Type::string_type ){
  409. opType=Type::string_type;
  410. }else if( lhs->sem_type==Type::float_type || rhs->sem_type==Type::float_type ){
  411. opType=Type::float_type;
  412. }else{
  413. opType=Type::int_type;
  414. }
  415. sem_type=Type::int_type;
  416. lhs=lhs->castTo( opType,e );
  417. rhs=rhs->castTo( opType,e );
  418. ConstNode *lc=lhs->constNode(),*rc=rhs->constNode();
  419. if( lc && rc ){
  420. ExprNode *expr;
  421. if( opType==Type::string_type ){
  422. switch( op ){
  423. case '<':expr=d_new IntConstNode( lc->stringValue()< rc->stringValue() );break;
  424. case '=':expr=d_new IntConstNode( lc->stringValue()==rc->stringValue() );break;
  425. case '>':expr=d_new IntConstNode( lc->stringValue()> rc->stringValue() );break;
  426. case LE: expr=d_new IntConstNode( lc->stringValue()<=rc->stringValue() );break;
  427. case NE: expr=d_new IntConstNode( lc->stringValue()!=rc->stringValue() );break;
  428. case GE: expr=d_new IntConstNode( lc->stringValue()>=rc->stringValue() );break;
  429. }
  430. }else if( opType==Type::float_type ){
  431. switch( op ){
  432. case '<':expr=d_new IntConstNode( lc->floatValue()< rc->floatValue() );break;
  433. case '=':expr=d_new IntConstNode( lc->floatValue()==rc->floatValue() );break;
  434. case '>':expr=d_new IntConstNode( lc->floatValue()> rc->floatValue() );break;
  435. case LE: expr=d_new IntConstNode( lc->floatValue()<=rc->floatValue() );break;
  436. case NE: expr=d_new IntConstNode( lc->floatValue()!=rc->floatValue() );break;
  437. case GE: expr=d_new IntConstNode( lc->floatValue()>=rc->floatValue() );break;
  438. }
  439. }else{
  440. switch( op ){
  441. case '<':expr=d_new IntConstNode( lc->intValue()< rc->intValue() );break;
  442. case '=':expr=d_new IntConstNode( lc->intValue()==rc->intValue() );break;
  443. case '>':expr=d_new IntConstNode( lc->intValue()> rc->intValue() );break;
  444. case LE: expr=d_new IntConstNode( lc->intValue()<=rc->intValue() );break;
  445. case NE: expr=d_new IntConstNode( lc->intValue()!=rc->intValue() );break;
  446. case GE: expr=d_new IntConstNode( lc->intValue()>=rc->intValue() );break;
  447. }
  448. }
  449. delete this;
  450. return expr;
  451. }
  452. return this;
  453. }
  454. TNode *RelExprNode::translate( Codegen *g ){
  455. TNode *l=lhs->translate( g );
  456. TNode *r=rhs->translate( g );
  457. return compare( op,l,r,opType );
  458. }
  459. ////////////////////
  460. // New expression //
  461. ////////////////////
  462. ExprNode *NewNode::semant( Environ *e ){
  463. sem_type=e->findType( ident );
  464. if( !sem_type ) ex( "custom type name not found" );
  465. if( sem_type->structType()==0 ) ex( "type is not a custom type" );
  466. return this;
  467. }
  468. TNode *NewNode::translate( Codegen *g ){
  469. return call( "__bbObjNew",global( "_t"+ident ) );
  470. }
  471. ////////////////////
  472. // First of class //
  473. ////////////////////
  474. ExprNode *FirstNode::semant( Environ *e ){
  475. sem_type=e->findType( ident );
  476. if( !sem_type ) ex( "custom type name name not found" );
  477. return this;
  478. }
  479. TNode *FirstNode::translate( Codegen *g ){
  480. return call( "__bbObjFirst",global( "_t"+ident ) );
  481. }
  482. ///////////////////
  483. // Last of class //
  484. ///////////////////
  485. ExprNode *LastNode::semant( Environ *e ){
  486. sem_type=e->findType( ident );
  487. if( !sem_type ) ex( "custom type name not found" );
  488. return this;
  489. }
  490. TNode *LastNode::translate( Codegen *g ){
  491. return call( "__bbObjLast",global( "_t"+ident ) );
  492. }
  493. ////////////////////
  494. // Next of object //
  495. ////////////////////
  496. ExprNode *AfterNode::semant( Environ *e ){
  497. expr=expr->semant( e );
  498. if( expr->sem_type==Type::null_type ) ex( "'After' cannot be used on 'Null'" );
  499. if( expr->sem_type->structType()==0 ) ex( "'After' must be used with a custom type object" );
  500. sem_type=expr->sem_type;
  501. return this;
  502. }
  503. TNode *AfterNode::translate( Codegen *g ){
  504. TNode *t=expr->translate( g );
  505. if( g->debug ) t=jumpf( t,"__bbNullObjEx" );
  506. return call( "__bbObjNext",t );
  507. }
  508. ////////////////////
  509. // Prev of object //
  510. ////////////////////
  511. ExprNode *BeforeNode::semant( Environ *e ){
  512. expr=expr->semant( e );
  513. if( expr->sem_type==Type::null_type ) ex( "'Before' cannot be used with 'Null'" );
  514. if( expr->sem_type->structType()==0 ) ex( "'Before' must be used with a custom type object" );
  515. sem_type=expr->sem_type;
  516. return this;
  517. }
  518. TNode *BeforeNode::translate( Codegen *g ){
  519. TNode *t=expr->translate( g );
  520. if( g->debug ) t=jumpf( t,"__bbNullObjEx" );
  521. return call( "__bbObjPrev",t );
  522. }
  523. /////////////////
  524. // Null object //
  525. /////////////////
  526. ExprNode *NullNode::semant( Environ *e ){
  527. sem_type=Type::null_type;
  528. return this;
  529. }
  530. TNode *NullNode::translate( Codegen *g ){
  531. return d_new TNode( IR_CONST,0,0,0 );
  532. }
  533. /////////////////
  534. // Object cast //
  535. /////////////////
  536. ExprNode *ObjectCastNode::semant( Environ *e ){
  537. expr=expr->semant( e );
  538. expr=expr->castTo( Type::int_type,e );
  539. sem_type=e->findType( type_ident );
  540. if( !sem_type ) ex( "custom type name not found" );
  541. if( !sem_type->structType() ) ex( "type is not a custom type" );
  542. return this;
  543. }
  544. TNode *ObjectCastNode::translate( Codegen *g ){
  545. TNode *t=expr->translate( g );
  546. t=call( "__bbObjFromHandle",t,global( "_t"+sem_type->structType()->ident ) );
  547. return t;
  548. }
  549. ///////////////////
  550. // Object Handle //
  551. ///////////////////
  552. ExprNode *ObjectHandleNode::semant( Environ *e ){
  553. expr=expr->semant( e );
  554. if( !expr->sem_type->structType() ) ex( "'ObjectHandle' must be used with an object" );
  555. sem_type=Type::int_type;
  556. return this;
  557. }
  558. TNode *ObjectHandleNode::translate( Codegen *g ){
  559. TNode *t=expr->translate( g );
  560. return call( "__bbObjToHandle",t );
  561. }