pdecl.pas 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Does declaration parsing for Free Pascal
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pdecl;
  19. interface
  20. uses
  21. globals,symtable;
  22. var
  23. { pointer to the last read type symbol, (for "forward" }
  24. { types) }
  25. lasttypesym : ptypesym;
  26. { hack, which allows to use the current parsed }
  27. { object type as function argument type }
  28. testcurobject : byte;
  29. curobjectname : stringid;
  30. { reads a string type with optional length }
  31. { and returns a pointer to the string }
  32. { definition }
  33. function stringtype : pdef;
  34. { reads a string, file type or a type id and returns a name and }
  35. { pdef }
  36. function single_type(var s : string) : pdef;
  37. { reads the declaration blocks }
  38. procedure read_declarations(islibrary : boolean);
  39. { reads declarations in the interface part of a unit }
  40. procedure read_interface_declarations;
  41. implementation
  42. uses
  43. cobjects,scanner,aasm,tree,pass_1,
  44. files,types,hcodegen,verbose,systems
  45. {$ifdef GDB}
  46. ,gdb
  47. {$endif GDB}
  48. { parser specific stuff }
  49. ,pbase,ptconst,pexpr,psub,pexports
  50. { processor specific stuff }
  51. {$ifdef i386}
  52. ,i386
  53. {$endif}
  54. {$ifdef m68k}
  55. ,m68k
  56. {$endif}
  57. ;
  58. function read_type(const name : stringid) : pdef;forward;
  59. procedure const_dec;
  60. var
  61. name : stringid;
  62. p : ptree;
  63. def : pdef;
  64. sym : psym;
  65. ps : pconstset;
  66. pd : pdouble;
  67. begin
  68. consume(_CONST);
  69. repeat
  70. name:=pattern;
  71. consume(ID);
  72. case token of
  73. EQUAL:
  74. begin
  75. consume(EQUAL);
  76. p:=comp_expr(true);
  77. do_firstpass(p);
  78. case p^.treetype of
  79. ordconstn:
  80. begin
  81. if is_constintnode(p) then
  82. symtablestack^.insert(new(pconstsym,init(name,constint,p^.value,nil)))
  83. else if is_constcharnode(p) then
  84. symtablestack^.insert(new(pconstsym,init(name,constchar,p^.value,nil)))
  85. else if is_constboolnode(p) then
  86. symtablestack^.insert(new(pconstsym,init(name,constbool,p^.value,nil)))
  87. else if p^.resulttype^.deftype=enumdef then
  88. symtablestack^.insert(new(pconstsym,init(name,constord,p^.value,p^.resulttype)))
  89. else internalerror(111);
  90. end;
  91. stringconstn:
  92. {values is disposed with p so I need a copy !}
  93. symtablestack^.insert(new(pconstsym,init(name,conststring,longint(stringdup(p^.values^)),nil)));
  94. realconstn : begin
  95. new(pd);
  96. pd^:=p^.valued;
  97. symtablestack^.insert(new(pconstsym,init(name,constreal,longint(pd),nil)));
  98. end;
  99. setconstrn : begin
  100. new(ps);
  101. ps^:=p^.constset^;
  102. symtablestack^.insert(new(pconstsym,init(name,
  103. constseta,longint(ps),p^.resulttype)));
  104. end;
  105. else Message(cg_e_illegal_expression);
  106. end;
  107. consume(SEMICOLON);
  108. end;
  109. COLON:
  110. begin
  111. { this was missed, so const s : ^string = nil gives an
  112. error (FK)
  113. }
  114. block_type:=bt_type;
  115. consume(COLON);
  116. ignore_equal:=true;
  117. def:=read_type('');
  118. block_type:=bt_general;
  119. ignore_equal:=false;
  120. sym:=new(ptypedconstsym,init(name,def));
  121. symtablestack^.insert(sym);
  122. consume(EQUAL);
  123. readtypedconst(def,ptypedconstsym(sym));
  124. consume(SEMICOLON);
  125. end;
  126. else consume(EQUAL);
  127. end;
  128. until token<>ID;
  129. end;
  130. procedure label_dec;
  131. var
  132. hl : plabel;
  133. begin
  134. consume(_LABEL);
  135. if not(cs_support_goto in aktswitches) then
  136. Message(sym_e_goto_and_label_not_supported);
  137. repeat
  138. if not(token in [ID,INTCONST]) then
  139. consume(ID)
  140. else
  141. begin
  142. getlabel(hl);
  143. symtablestack^.insert(new(plabelsym,init(pattern,hl)));
  144. consume(token);
  145. end;
  146. if token<>SEMICOLON then consume(COMMA);
  147. until not(token in [ID,INTCONST]);
  148. consume(SEMICOLON);
  149. end;
  150. procedure read_var_decs(is_record,is_object:boolean);
  151. { reads the filed of a record into a }
  152. { symtablestack, if record=false }
  153. { variants are forbidden, so this procedure }
  154. { can be used to read object fields }
  155. { if absolute is true, ABSOLUTE and file }
  156. { types are allowed }
  157. { => the procedure is also used to read }
  158. { a sequence of variable declaration }
  159. var
  160. sc : pstringcontainer;
  161. s : stringid;
  162. old_block_type : tblock_type;
  163. filepos : tfileposinfo;
  164. symdone : boolean;
  165. { to handle absolute }
  166. abssym : pabsolutesym;
  167. {$ifdef i386}
  168. l : longint;
  169. code : word;
  170. {$endif i386}
  171. { c var }
  172. Csym : pvarsym;
  173. is_cdecl,extern_Csym,export_Csym : boolean;
  174. C_name : string;
  175. { case }
  176. p,casedef : pdef;
  177. { maxsize contains the max. size of a variant }
  178. { startvarrec contains the start of the variant part of a record }
  179. maxsize,startvarrec : longint;
  180. pt : ptree;
  181. begin
  182. old_block_type:=block_type;
  183. block_type:=bt_type;
  184. { Force an expected ID error message }
  185. if not (token in [ID,_CASE,_END]) then
  186. consume(ID);
  187. { read vars }
  188. while (token=ID) and
  189. not(is_object and
  190. ((pattern='PUBLIC') or (pattern='PRIVATE') or
  191. (pattern='PUBLISHED') or (pattern='PROTECTED'))) do
  192. begin
  193. C_name:=orgpattern;
  194. sc:=idlist;
  195. consume(COLON);
  196. p:=read_type('');
  197. symdone:=false;
  198. { check for absolute }
  199. if (token=ID) and (pattern='ABSOLUTE') and not(is_record or is_object) then
  200. begin
  201. consume(ID);
  202. { only allowed for one var }
  203. s:=sc^.get_with_tokeninfo(filepos);
  204. if not sc^.empty then
  205. Message(parser_e_absolute_only_one_var);
  206. dispose(sc,done);
  207. { parse the rest }
  208. if token=ID then
  209. begin
  210. getsym(pattern,true);
  211. consume(ID);
  212. { we should check the result type of srsym }
  213. if not (srsym^.typ in [varsym,typedconstsym]) then
  214. Message(parser_e_absolute_only_to_var_or_const);
  215. abssym:=new(pabsolutesym,init(s,p));
  216. abssym^.typ:=absolutesym;
  217. abssym^.abstyp:=tovar;
  218. abssym^.ref:=srsym;
  219. abssym^.line_no:=filepos.line;
  220. symtablestack^.insert(abssym);
  221. end
  222. else
  223. if token=CSTRING then
  224. begin
  225. abssym:=new(pabsolutesym,init(s,p));
  226. s:=pattern;
  227. consume(CSTRING);
  228. abssym^.typ:=absolutesym;
  229. abssym^.abstyp:=toasm;
  230. abssym^.asmname:=stringdup(s);
  231. abssym^.line_no:=filepos.line;
  232. symtablestack^.insert(abssym);
  233. end
  234. else
  235. { absolute address ?!? }
  236. if token=INTCONST then
  237. begin
  238. {$ifdef i386}
  239. if (target_info.target=target_GO32V2) then
  240. begin
  241. abssym:=new(pabsolutesym,init(s,p));
  242. abssym^.typ:=absolutesym;
  243. abssym^.abstyp:=toaddr;
  244. abssym^.absseg:=false;
  245. abssym^.line_no:=filepos.line;
  246. s:=pattern;
  247. consume(INTCONST);
  248. val(s,abssym^.address,code);
  249. if token=COLON then
  250. begin
  251. consume(token);
  252. s:=pattern;
  253. consume(INTCONST);
  254. val(s,l,code);
  255. abssym^.address:=abssym^.address shl 4+l;
  256. abssym^.absseg:=true;
  257. end;
  258. symtablestack^.insert(abssym);
  259. end
  260. else
  261. {$endif i386}
  262. Message(parser_e_absolute_only_to_var_or_const);
  263. end
  264. else
  265. Message(parser_e_absolute_only_to_var_or_const);
  266. symdone:=true;
  267. end;
  268. { for a record there doesn't need to be a ; before the END or ) }
  269. if not((is_record or is_object) and (token in [_END,RKLAMMER])) then
  270. consume(SEMICOLON);
  271. { Check for variable directives }
  272. if (token=ID) then
  273. begin
  274. { Check for C Variable declarations }
  275. if support_c_var and
  276. not(is_record or is_object) and
  277. ((pattern='EXPORT') or
  278. (pattern='EXTERNAL') or
  279. (pattern='CDECL')) then
  280. begin
  281. { only allowed for one var }
  282. s:=sc^.get_with_tokeninfo(filepos);
  283. if not sc^.empty then
  284. Message(parser_e_absolute_only_one_var);
  285. dispose(sc,done);
  286. { defaults }
  287. is_cdecl:=false;
  288. extern_csym:=false;
  289. export_Csym:=false;
  290. { cdecl }
  291. if pattern='CDECL' then
  292. begin
  293. consume(ID);
  294. consume(SEMICOLON);
  295. is_cdecl:=true;
  296. end;
  297. { external }
  298. if pattern='EXTERNAL' then
  299. begin
  300. consume(ID);
  301. extern_csym:=true;
  302. end;
  303. { export }
  304. if pattern='EXPORT' then
  305. begin
  306. consume(ID);
  307. if extern_csym then
  308. Comment(V_Error,'can''t use both EXPORT and EXTERNAL')
  309. else
  310. export_Csym:=true;
  311. end;
  312. { external and export need a name after when no cdecl is used }
  313. if not is_cdecl then
  314. begin
  315. if (token=ID) and (pattern='NAME') then
  316. consume(ID)
  317. else
  318. Comment(V_Error,'NAME keyword expected');
  319. C_name:=pattern;
  320. { allow also char }
  321. if token=CCHAR then
  322. consume(CCHAR)
  323. else
  324. consume(CSTRING);
  325. end;
  326. { consume the ; when export or external is used }
  327. if extern_csym or export_csym then
  328. consume(SEMICOLON);
  329. { insert in the symtable }
  330. Csym:=new(pvarsym,init_C(s,C_name,p));
  331. if extern_Csym then
  332. begin
  333. Csym^.var_options:=Csym^.var_options or vo_is_external;
  334. { correct type ?? }
  335. externals^.concat(new(pai_external,init(Csym^.mangledname,EXT_NEAR)));
  336. end;
  337. symtablestack^.insert(Csym);
  338. symdone:=true;
  339. end
  340. else
  341. if (is_object) and (cs_static_keyword in aktswitches) and (pattern='STATIC') then
  342. begin
  343. current_object_option:=current_object_option or sp_static;
  344. insert_syms(symtablestack,sc,p);
  345. current_object_option:=current_object_option - sp_static;
  346. consume(ID);
  347. consume(SEMICOLON);
  348. symdone:=true;
  349. end;
  350. end;
  351. { insert it in the symtable, if not done yet }
  352. if not symdone then
  353. insert_syms(symtablestack,sc,p);
  354. end;
  355. { Check for Case }
  356. if is_record and (token=_CASE) then
  357. begin
  358. maxsize:=0;
  359. consume(_CASE);
  360. s:=pattern;
  361. getsym(s,false);
  362. { may be only a type: }
  363. if assigned(srsym) and (srsym^.typ in [typesym,unitsym]) then
  364. casedef:=read_type('')
  365. else
  366. begin
  367. consume(ID);
  368. consume(COLON);
  369. casedef:=read_type('');
  370. symtablestack^.insert(new(pvarsym,init(s,casedef)));
  371. end;
  372. if not is_ordinal(casedef) then
  373. Message(parser_e_ordinal_expected);
  374. consume(_OF);
  375. startvarrec:=symtablestack^.datasize;
  376. repeat
  377. repeat
  378. pt:=comp_expr(true);
  379. do_firstpass(pt);
  380. if not(pt^.treetype=ordconstn) then
  381. Message(cg_e_illegal_expression);
  382. disposetree(pt);
  383. if token=COMMA then
  384. consume(COMMA)
  385. else
  386. break;
  387. until false;
  388. consume(COLON);
  389. { read the vars }
  390. consume(LKLAMMER);
  391. if token<>RKLAMMER then
  392. read_var_decs(true,false);
  393. consume(RKLAMMER);
  394. { calculates maximal variant size }
  395. maxsize:=max(maxsize,symtablestack^.datasize);
  396. { the items of the next variant are overlayed }
  397. symtablestack^.datasize:=startvarrec;
  398. if token<>_END then
  399. consume(SEMICOLON);
  400. until token=_END;
  401. { at last set the record size to that of the biggest variant }
  402. symtablestack^.datasize:=maxsize;
  403. end;
  404. block_type:=old_block_type;
  405. end;
  406. function stringtype : pdef;
  407. { reads a string type with optional length }
  408. { and returns a pointer to the string }
  409. { definition }
  410. var
  411. p : ptree;
  412. d : pdef;
  413. begin
  414. consume(_STRING);
  415. if token=LECKKLAMMER then
  416. begin
  417. consume(LECKKLAMMER);
  418. p:=comp_expr(true);
  419. do_firstpass(p);
  420. if not is_constintnode(p) then
  421. Message(cg_e_illegal_expression);
  422. {$ifndef UseAnsiString}
  423. if (p^.value<1) or (p^.value>255) then
  424. begin
  425. Message(parser_e_string_too_long);
  426. p^.value:=255;
  427. end;
  428. consume(RECKKLAMMER);
  429. if p^.value<>255 then
  430. d:=new(pstringdef,init(p^.value))
  431. {$ifndef GDB}
  432. else d:=new(pstringdef,init(255));
  433. {$else GDB}
  434. else d:=globaldef('STRING');
  435. {$endif GDB}
  436. {$else UseAnsiString}
  437. if p^.value>255 then
  438. d:=new(pstringdef,ansiinit(p^.value))
  439. else if p^.value<>255 then
  440. d:=new(pstringdef,init(p^.value))
  441. {$ifndef GDB}
  442. else d:=new(pstringdef,init(255));
  443. {$else GDB}
  444. else d:=globaldef('STRING');
  445. {$endif GDB}
  446. consume(RECKKLAMMER);
  447. {$endif UseAnsiString}
  448. disposetree(p);
  449. end
  450. { should string without suffix be an ansistring also
  451. in ansistring mode ?? (PM) }
  452. {$ifndef GDB}
  453. else d:=new(pstringdef,init(255));
  454. {$else GDB}
  455. else d:=globaldef('STRING');
  456. {$endif GDB}
  457. stringtype:=d;
  458. end;
  459. function id_type(var s : string) : pdef;
  460. { reads a type definition and returns a pointer }
  461. { to a appropriating pdef, s gets the name of }
  462. { the type to allow name mangling }
  463. begin
  464. s:=pattern;
  465. consume(ID);
  466. { classes can be used also in classes }
  467. if (curobjectname=pattern) and aktobjectdef^.isclass then
  468. begin
  469. id_type:=aktobjectdef;
  470. exit;
  471. end;
  472. { objects can be parameters }
  473. if (testcurobject=2) and (curobjectname=pattern) then
  474. begin
  475. id_type:=aktobjectdef;
  476. exit;
  477. end;
  478. getsym(s,true);
  479. if assigned(srsym) then
  480. begin
  481. if srsym^.typ=unitsym then
  482. begin
  483. consume(POINT);
  484. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  485. s:=pattern;
  486. consume(ID);
  487. end;
  488. if srsym^.typ<>typesym then
  489. begin
  490. Message(sym_e_type_id_expected);
  491. lasttypesym:=ptypesym(srsym);
  492. id_type:=generrordef;
  493. exit;
  494. end;
  495. end;
  496. lasttypesym:=ptypesym(srsym);
  497. id_type:=ptypesym(srsym)^.definition;
  498. end;
  499. function single_type(var s : string) : pdef;
  500. { reads a string, file type or a type id and returns a name and }
  501. { pdef }
  502. var
  503. hs : string;
  504. begin
  505. case token of
  506. _STRING:
  507. begin
  508. single_type:=stringtype;
  509. s:='STRING';
  510. lasttypesym:=nil;
  511. end;
  512. _FILE:
  513. begin
  514. consume(_FILE);
  515. if token=_OF then
  516. begin
  517. consume(_OF);
  518. single_type:=new(pfiledef,init(ft_typed,single_type(hs)));
  519. s:='FILE$OF$'+hs;
  520. end
  521. else
  522. begin
  523. { single_type:=new(pfiledef,init(ft_untyped,nil));}
  524. single_type:=cfiledef;
  525. s:='FILE';
  526. end;
  527. lasttypesym:=nil;
  528. end;
  529. else single_type:=id_type(s);
  530. end;
  531. end;
  532. function object_dec(const n : stringid;fd : pobjectdef) : pdef;
  533. { this function parses an object or class declaration }
  534. var
  535. actmembertype : symprop;
  536. there_is_a_destructor : boolean;
  537. is_a_class : boolean;
  538. childof : pobjectdef;
  539. aktclass : pobjectdef;
  540. procedure constructor_head;
  541. begin
  542. consume(_CONSTRUCTOR);
  543. { must be at same level as in implementation }
  544. _proc_head(poconstructor);
  545. if (cs_checkconsname in aktswitches) and (aktprocsym^.name<>'INIT') then
  546. Message(parser_e_constructorname_must_be_init);
  547. consume(SEMICOLON);
  548. begin
  549. if (aktclass^.options and oois_class)<>0 then
  550. begin
  551. { CLASS constructors return the created instance }
  552. aktprocsym^.definition^.retdef:=aktclass;
  553. end
  554. else
  555. begin
  556. { OBJECT constructors return a boolean }
  557. {$IfDef GDB}
  558. {GDB doesn't like unnamed types !}
  559. aktprocsym^.definition^.retdef:=
  560. globaldef('boolean');
  561. {$Else GDB}
  562. aktprocsym^.definition^.retdef:=
  563. new(porddef,init(bool8bit,0,1));
  564. {$Endif GDB}
  565. end;
  566. end;
  567. end;
  568. procedure property_dec;
  569. var
  570. sym : psym;
  571. propertyparas : pdefcoll;
  572. { returns the matching procedure to access a property }
  573. function get_procdef : pprocdef;
  574. var
  575. p : pprocdef;
  576. begin
  577. p:=pprocsym(sym)^.definition;
  578. get_procdef:=nil;
  579. while assigned(p) do
  580. begin
  581. if equal_paras(p^.para1,propertyparas,true) then
  582. break;
  583. p:=p^.nextoverloaded;
  584. end;
  585. get_procdef:=p;
  586. end;
  587. var
  588. hp2,datacoll : pdefcoll;
  589. p,p2 : ppropertysym;
  590. overriden : psym;
  591. hs : string;
  592. code : word;
  593. varspez : tvarspez;
  594. sc : pstringcontainer;
  595. hp : pdef;
  596. s : string;
  597. filepos : tfileposinfo;
  598. pp : pprocdef;
  599. begin
  600. { check for a class }
  601. if (aktclass^.options and oois_class=0) then
  602. Message(parser_e_syntax_error);
  603. consume(_PROPERTY);
  604. if token=ID then
  605. begin
  606. p:=new(ppropertysym,init(pattern));
  607. consume(ID);
  608. propertyparas:=nil;
  609. datacoll:=nil;
  610. { property parameters ? }
  611. if token=LECKKLAMMER then
  612. begin
  613. { create a list of the parameters in propertyparas }
  614. consume(LECKKLAMMER);
  615. inc(testcurobject);
  616. repeat
  617. if token=_VAR then
  618. begin
  619. consume(_VAR);
  620. varspez:=vs_var;
  621. end
  622. else if token=_CONST then
  623. begin
  624. consume(_CONST);
  625. varspez:=vs_const;
  626. end
  627. else varspez:=vs_value;
  628. sc:=idlist;
  629. if token=COLON then
  630. begin
  631. consume(COLON);
  632. if token=_ARRAY then
  633. begin
  634. {
  635. if (varspez<>vs_const) and
  636. (varspez<>vs_var) then
  637. begin
  638. varspez:=vs_const;
  639. Message(parser_e_illegal_open_parameter);
  640. end;
  641. }
  642. consume(_ARRAY);
  643. consume(_OF);
  644. { define range and type of range }
  645. hp:=new(parraydef,init(0,-1,s32bitdef));
  646. { define field type }
  647. parraydef(hp)^.definition:=single_type(s);
  648. end
  649. else
  650. hp:=single_type(s);
  651. end
  652. else
  653. hp:=new(pformaldef,init);
  654. s:=sc^.get_with_tokeninfo(filepos);
  655. while s<>'' do
  656. begin
  657. new(hp2);
  658. hp2^.paratyp:=varspez;
  659. hp2^.data:=hp;
  660. hp2^.next:=propertyparas;
  661. propertyparas:=hp2;
  662. s:=sc^.get_with_tokeninfo(filepos);
  663. end;
  664. dispose(sc,done);
  665. if token=SEMICOLON then consume(SEMICOLON)
  666. else break;
  667. until false;
  668. dec(testcurobject);
  669. consume(RECKKLAMMER);
  670. end;
  671. { overriden property ? }
  672. { force property interface, if there is a property parameter }
  673. if (token=COLON) or assigned(propertyparas) then
  674. begin
  675. consume(COLON);
  676. p^.proptype:=single_type(hs);
  677. if (token=ID) and (pattern='INDEX') then
  678. begin
  679. consume(ID);
  680. p^.options:=p^.options or ppo_indexed;
  681. if token=INTCONST then
  682. val(pattern,p^.index,code);
  683. consume(INTCONST);
  684. { concat a longint to the para template }
  685. new(hp2);
  686. hp2^.paratyp:=vs_value;
  687. hp2^.data:=s32bitdef;
  688. hp2^.next:=propertyparas;
  689. propertyparas:=hp2;
  690. end;
  691. end
  692. else
  693. begin
  694. { do an property override }
  695. overriden:=search_class_member(aktclass,pattern);
  696. if assigned(overriden) and (overriden^.typ=propertysym) then
  697. begin
  698. { take the whole info: }
  699. p^.options:=ppropertysym(overriden)^.options;
  700. p^.index:=ppropertysym(overriden)^.index;
  701. p^.proptype:=ppropertysym(overriden)^.proptype;
  702. p^.writeaccesssym:=ppropertysym(overriden)^.writeaccesssym;
  703. p^.readaccesssym:=ppropertysym(overriden)^.readaccesssym;
  704. p^.writeaccessdef:=ppropertysym(overriden)^.writeaccessdef;
  705. p^.readaccessdef:=ppropertysym(overriden)^.readaccessdef;
  706. end
  707. else
  708. begin
  709. p^.proptype:=generrordef;
  710. message(parser_e_no_property_found_to_override);
  711. end;
  712. end;
  713. { create data defcoll to allow correct parameter checks }
  714. new(datacoll);
  715. datacoll^.paratyp:=vs_value;
  716. datacoll^.data:=p^.proptype;
  717. datacoll^.next:=nil;
  718. if (token=ID) and (pattern='READ') then
  719. begin
  720. consume(ID);
  721. sym:=search_class_member(aktclass,pattern);
  722. if not(assigned(sym)) then
  723. Message1(sym_e_unknown_id,pattern)
  724. else
  725. begin
  726. { varsym aren't allowed for an indexed property
  727. or an property with parameters }
  728. if ((sym^.typ=varsym) and
  729. { not necessary, an index forces propertyparas
  730. to be assigned
  731. }
  732. { (((p^.options and ppo_indexed)<>0) or }
  733. assigned(propertyparas)) or
  734. not(sym^.typ in [varsym,procsym]) then
  735. Message(parser_e_ill_property_access_sym);
  736. { search the matching definition }
  737. if sym^.typ=procsym then
  738. begin
  739. pp:=get_procdef;
  740. if not(assigned(pp)) or
  741. not(is_equal(pp^.retdef,p^.proptype)) then
  742. Message(parser_e_ill_property_access_sym);
  743. p^.readaccessdef:=pp;
  744. end
  745. else if sym^.typ=varsym then
  746. begin
  747. if not(is_equal(pvarsym(sym)^.definition,
  748. p^.proptype)) then
  749. Message(parser_e_ill_property_access_sym);
  750. end;
  751. p^.readaccesssym:=sym;
  752. end;
  753. consume(ID);
  754. end;
  755. if (token=ID) and (pattern='WRITE') then
  756. begin
  757. consume(ID);
  758. sym:=search_class_member(aktclass,pattern);
  759. if not(assigned(sym)) then
  760. Message1(sym_e_unknown_id,pattern)
  761. else
  762. begin
  763. if ((sym^.typ=varsym) and
  764. assigned(propertyparas)) or
  765. not(sym^.typ in [varsym,procsym]) then
  766. Message(parser_e_ill_property_access_sym);
  767. { search the matching definition }
  768. if sym^.typ=procsym then
  769. begin
  770. { insert data entry to check access method }
  771. datacoll^.next:=propertyparas;
  772. propertyparas:=datacoll;
  773. pp:=get_procdef;
  774. { ... and remove it }
  775. propertyparas:=propertyparas^.next;
  776. if not(assigned(pp)) then
  777. Message(parser_e_ill_property_access_sym);
  778. p^.writeaccessdef:=pp;
  779. end
  780. else if sym^.typ=varsym then
  781. begin
  782. if not(is_equal(pvarsym(sym)^.definition,
  783. p^.proptype)) then
  784. Message(parser_e_ill_property_access_sym);
  785. end;
  786. p^.writeaccesssym:=sym;
  787. end;
  788. consume(ID);
  789. end;
  790. if (token=ID) and (pattern='STORED') then
  791. begin
  792. consume(ID);
  793. { !!!!!!!! }
  794. end;
  795. if (token=ID) and (pattern='DEFAULT') then
  796. begin
  797. consume(ID);
  798. { !!!!!!! storage }
  799. consume(SEMICOLON);
  800. end
  801. else if (token=ID) and (pattern='NODEFAULT') then
  802. begin
  803. consume(ID);
  804. { !!!!!!!! }
  805. end;
  806. symtablestack^.insert(p);
  807. { default property ? }
  808. consume(SEMICOLON);
  809. if (token=ID) and (pattern='DEFAULT') then
  810. begin
  811. consume(ID);
  812. p2:=search_default_property(aktclass);
  813. if assigned(p2) then
  814. message1(parser_e_only_one_default_property,
  815. pobjectdef(p2^.owner^.defowner)^.name^)
  816. else
  817. begin
  818. p^.options:=p^.options or ppo_defaultproperty;
  819. if not(assigned(propertyparas)) then
  820. message(parser_e_property_need_paras);
  821. end;
  822. consume(SEMICOLON);
  823. end;
  824. { clean up }
  825. if assigned(datacoll) then
  826. dispose(datacoll);
  827. end
  828. else
  829. begin
  830. consume(ID);
  831. consume(SEMICOLON);
  832. end;
  833. end;
  834. procedure destructor_head;
  835. begin
  836. consume(_DESTRUCTOR);
  837. _proc_head(podestructor);
  838. if (cs_checkconsname in aktswitches) and (aktprocsym^.name<>'DONE') then
  839. Message(parser_e_destructorname_must_be_done);
  840. consume(SEMICOLON);
  841. if assigned(aktprocsym^.definition^.para1) then
  842. Message(parser_e_no_paras_for_destructor);
  843. { no return value }
  844. aktprocsym^.definition^.retdef:=voiddef;
  845. end;
  846. var
  847. hs : string;
  848. pcrd : pclassrefdef;
  849. hp1 : pdef;
  850. oldprocsym : Pprocsym;
  851. oldparse_only : boolean;
  852. begin
  853. {Nowadays aktprocsym may already have a value, so we need to save
  854. it.}
  855. oldprocsym:=aktprocsym;
  856. { forward is resolved }
  857. if assigned(fd) then
  858. fd^.options:=fd^.options and not(oo_isforward);
  859. there_is_a_destructor:=false;
  860. actmembertype:=sp_public;
  861. { objects and class types can't be declared local }
  862. if (symtablestack^.symtabletype<>globalsymtable) and
  863. (symtablestack^.symtabletype<>staticsymtable) then
  864. Message(parser_e_no_local_objects);
  865. { distinguish classes and objects }
  866. if token=_OBJECT then
  867. begin
  868. is_a_class:=false;
  869. consume(_OBJECT)
  870. end
  871. else
  872. begin
  873. is_a_class:=true;
  874. consume(_CLASS);
  875. if not(assigned(fd)) and (token=_OF) then
  876. begin
  877. { a hack, but it's easy to handle }
  878. { class reference type }
  879. consume(_OF);
  880. if typecanbeforward then
  881. forwardsallowed:=true;
  882. hp1:=single_type(hs);
  883. { accept hp1, if is a forward def ...}
  884. if ((lasttypesym<>nil)
  885. and ((lasttypesym^.properties and sp_forwarddef)<>0)) or
  886. { or a class
  887. (if the foward defined type is a class is checked, when
  888. the forward is resolved)
  889. }
  890. ((hp1^.deftype=objectdef) and (
  891. (pobjectdef(hp1)^.options and oois_class)<>0)) then
  892. begin
  893. pcrd:=new(pclassrefdef,init(hp1));
  894. object_dec:=pcrd;
  895. {I add big troubles here
  896. with var p : ^byte in graph.putimage
  897. because a save_forward was called and
  898. no resolve forward
  899. => so the definition was rewritten after
  900. having been disposed !!
  901. Strange problems appeared !!!!}
  902. {Anyhow forwards should only be allowed
  903. inside a type statement ??
  904. don't you think so }
  905. if (lasttypesym<>nil)
  906. and ((lasttypesym^.properties and sp_forwarddef)<>0) then
  907. lasttypesym^.forwardpointer:=ppointerdef(pcrd);
  908. forwardsallowed:=false;
  909. end
  910. else
  911. begin
  912. Message(parser_e_class_type_expected);
  913. object_dec:=new(perrordef,init);
  914. end;
  915. exit;
  916. end
  917. { forward class }
  918. else if not(assigned(fd)) and (token=SEMICOLON) then
  919. begin
  920. { also anonym objects aren't allow (o : object a : longint; end;) }
  921. if n='' then
  922. Message(parser_e_no_anonym_objects);
  923. if n='TOBJECT' then
  924. begin
  925. aktclass:=new(pobjectdef,init(n,nil));
  926. class_tobject:=aktclass;
  927. end
  928. else
  929. aktclass:=new(pobjectdef,init(n,class_tobject));
  930. aktclass^.options:=aktclass^.options or oois_class or oo_isforward;
  931. object_dec:=aktclass;
  932. exit;
  933. end;
  934. end;
  935. { also anonym objects aren't allow (o : object a : longint; end;) }
  936. if n='' then
  937. Message(parser_e_no_anonym_objects);
  938. { read the parent class }
  939. if token=LKLAMMER then
  940. begin
  941. consume(LKLAMMER);
  942. { does not allow objects.tobject !! }
  943. {if token<>ID then
  944. consume(ID);
  945. getsym(pattern,true);}
  946. childof:=pobjectdef(id_type(pattern));
  947. if (childof^.deftype<>objectdef) then
  948. begin
  949. Message(parser_e_class_type_expected);
  950. childof:=nil;
  951. end;
  952. { a mix of class and object isn't allowed }
  953. if (((childof^.options and oois_class)<>0) and not is_a_class) or
  954. (((childof^.options and oois_class)=0) and is_a_class) then
  955. Message(parser_e_mix_of_classes_and_objects);
  956. if assigned(fd) then
  957. begin
  958. { the forward of the child must be resolved to get
  959. correct field addresses
  960. }
  961. if (childof^.options and oo_isforward)<>0 then
  962. Message1(parser_forward_declaration_must_be_resolved,childof^.name^);
  963. fd^.childof:=childof;
  964. aktclass:=fd;
  965. { ajust the size, because the child could be also
  966. forward defined
  967. }
  968. aktclass^.publicsyms^.datasize:=
  969. aktclass^.publicsyms^.datasize-4+childof^.publicsyms^.datasize;
  970. end
  971. else
  972. aktclass:=new(pobjectdef,init(n,childof));
  973. consume(RKLAMMER);
  974. end
  975. { if no parent class, then a class get tobject as parent }
  976. else if is_a_class then
  977. begin
  978. { is the current class tobject? }
  979. { so you could define your own tobject }
  980. if n='TOBJECT' then
  981. begin
  982. if assigned(fd) then
  983. aktclass:=fd
  984. else
  985. aktclass:=new(pobjectdef,init(n,nil));
  986. class_tobject:=aktclass;
  987. end
  988. else
  989. begin
  990. childof:=class_tobject;
  991. if assigned(fd) then
  992. begin
  993. { the forward of the child must be resolved to get
  994. correct field addresses
  995. }
  996. if (childof^.options and oo_isforward)<>0 then
  997. Message1(parser_forward_declaration_must_be_resolved,childof^.name^);
  998. aktclass:=fd;
  999. aktclass^.childof:=childof;
  1000. { ajust the size, because the child could be also
  1001. forward defined
  1002. }
  1003. aktclass^.publicsyms^.datasize:=
  1004. aktclass^.publicsyms^.datasize-4+childof^.publicsyms^.datasize;
  1005. end
  1006. else
  1007. aktclass:=new(pobjectdef,init(n,childof));
  1008. end;
  1009. end
  1010. else aktclass:=new(pobjectdef,init(n,nil));
  1011. { set the class attribute }
  1012. if is_a_class then
  1013. begin
  1014. aktclass^.options:=aktclass^.options or oois_class;
  1015. if (cs_generate_rtti in aktswitches) or
  1016. (assigned(aktclass^.childof) and
  1017. ((aktclass^.childof^.options and oo_can_have_published)<>0)
  1018. ) then
  1019. aktclass^.options:=aktclass^.options or oo_can_have_published;
  1020. end;
  1021. aktobjectdef:=aktclass;
  1022. { default access is public }
  1023. actmembertype:=sp_public;
  1024. aktclass^.publicsyms^.next:=symtablestack;
  1025. symtablestack:=aktclass^.publicsyms;
  1026. procinfo._class:=aktclass;
  1027. testcurobject:=1;
  1028. curobjectname:=n;
  1029. { short class declaration ? }
  1030. if (not is_a_class) or (token<>SEMICOLON) then
  1031. begin
  1032. { Parse componenten }
  1033. repeat
  1034. if actmembertype=sp_private then
  1035. aktclass^.options:=aktclass^.options or oo_hasprivate;
  1036. if actmembertype=sp_protected then
  1037. aktclass^.options:=aktclass^.options or oo_hasprotected;
  1038. case token of
  1039. ID : begin
  1040. if (pattern='PRIVATE') then
  1041. begin
  1042. consume(ID);
  1043. actmembertype:=sp_private;
  1044. current_object_option:=sp_private;
  1045. end
  1046. else
  1047. if (pattern='PROTECTED') then
  1048. begin
  1049. consume(ID);
  1050. current_object_option:=sp_protected;
  1051. actmembertype:=sp_protected;
  1052. end
  1053. else
  1054. if (pattern='PUBLIC') then
  1055. begin
  1056. consume(ID);
  1057. current_object_option:=sp_public;
  1058. actmembertype:=sp_public;
  1059. end
  1060. else
  1061. if (pattern='PUBLISHED') then
  1062. begin
  1063. if (aktclass^.options and oo_can_have_published)=0 then
  1064. Message(parser_e_cant_have_published);
  1065. consume(ID);
  1066. current_object_option:=sp_published;
  1067. actmembertype:=sp_published;
  1068. end
  1069. else
  1070. read_var_decs(false,true);
  1071. end;
  1072. _PROPERTY : property_dec;
  1073. _PROCEDURE,
  1074. _FUNCTION,
  1075. _CLASS : begin
  1076. oldparse_only:=parse_only;
  1077. parse_only:=true;
  1078. proc_head;
  1079. parse_only:=oldparse_only;
  1080. if (token=ID) then
  1081. begin
  1082. if (pattern='VIRTUAL') or (pattern='DYNAMIC') then
  1083. begin
  1084. if actmembertype=sp_private then
  1085. Message(parser_w_priv_meth_not_virtual);
  1086. consume(ID);
  1087. consume(SEMICOLON);
  1088. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or povirtualmethod;
  1089. aktclass^.options:=aktclass^.options or oo_hasvirtual;
  1090. end
  1091. else
  1092. if (pattern='OVERRIDE') then
  1093. begin
  1094. consume(ID);
  1095. consume(SEMICOLON);
  1096. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or
  1097. pooverridingmethod or povirtualmethod;
  1098. end;
  1099. { Delphi II extension }
  1100. if (pattern='ABSTRACT') then
  1101. begin
  1102. consume(ID);
  1103. consume(SEMICOLON);
  1104. if (aktprocsym^.definition^.options and povirtualmethod)<>0 then
  1105. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poabstractmethod
  1106. else
  1107. Message(parser_e_only_virtual_methods_abstract);
  1108. { the method is defined }
  1109. aktprocsym^.definition^.forwarddef:=false;
  1110. end;
  1111. if (cs_static_keyword in aktswitches) and (pattern='STATIC') then
  1112. begin
  1113. consume(ID);
  1114. consume(SEMICOLON);
  1115. aktprocsym^.properties:=aktprocsym^.properties or sp_static;
  1116. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or postaticmethod;
  1117. end;
  1118. end;
  1119. end;
  1120. _CONSTRUCTOR : begin
  1121. if actmembertype<>sp_public then
  1122. Message(parser_w_constructor_should_be_public);
  1123. oldparse_only:=parse_only;
  1124. parse_only:=true;
  1125. constructor_head;
  1126. parse_only:=oldparse_only;
  1127. if (token=ID) then
  1128. begin
  1129. if (pattern='VIRTUAL') or (pattern='DYNAMIC') then
  1130. begin
  1131. consume(ID);
  1132. consume(SEMICOLON);
  1133. if not(aktclass^.isclass) then
  1134. Message(parser_e_constructor_cannot_be_not_virtual)
  1135. else
  1136. begin
  1137. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or povirtualmethod;
  1138. aktclass^.options:=aktclass^.options or oo_hasvirtual;
  1139. end;
  1140. end
  1141. else
  1142. if (pattern='OVERRIDE') then
  1143. begin
  1144. consume(ID);
  1145. consume(SEMICOLON);
  1146. if (aktclass^.options and oois_class=0) then
  1147. Message(parser_e_constructor_cannot_be_not_virtual)
  1148. else
  1149. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or
  1150. pooverridingmethod or povirtualmethod;
  1151. end;
  1152. end;
  1153. end;
  1154. _DESTRUCTOR : begin
  1155. if there_is_a_destructor then
  1156. Message(parser_n_only_one_destructor);
  1157. there_is_a_destructor:=true;
  1158. if actmembertype<>sp_public then
  1159. Message(parser_w_destructor_should_be_public);
  1160. oldparse_only:=parse_only;
  1161. parse_only:=true;
  1162. destructor_head;
  1163. parse_only:=oldparse_only;
  1164. if (token=ID) then
  1165. begin
  1166. if (pattern='VIRTUAL') or (pattern='DYNAMIC') then
  1167. begin
  1168. consume(ID);
  1169. consume(SEMICOLON);
  1170. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or povirtualmethod;
  1171. end
  1172. else
  1173. if (pattern='OVERRIDE') then
  1174. begin
  1175. consume(ID);
  1176. consume(SEMICOLON);
  1177. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or
  1178. pooverridingmethod or povirtualmethod;
  1179. end;
  1180. end;
  1181. end;
  1182. _END : begin
  1183. consume(_END);
  1184. break;
  1185. end;
  1186. else
  1187. consume(ID); { Give a ident expected message, like tp7 }
  1188. end;
  1189. until false;
  1190. current_object_option:=sp_public;
  1191. end;
  1192. testcurobject:=0;
  1193. curobjectname:='';
  1194. if (cs_smartlink in aktswitches) then
  1195. datasegment^.concat(new(pai_cut,init));
  1196. {$ifdef GDB}
  1197. { generate the VMT }
  1198. if cs_debuginfo in aktswitches then
  1199. begin
  1200. do_count_dbx:=true;
  1201. if assigned(aktclass^.owner) and assigned(aktclass^.owner^.name) then
  1202. datasegment^.concat(new(pai_stabs,init(strpnew('"vmt_'+aktclass^.owner^.name^+n+':S'+
  1203. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+aktclass^.vmt_mangledname))));
  1204. end;
  1205. {$endif GDB}
  1206. datasegment^.concat(new(pai_symbol,init_global(aktclass^.vmt_mangledname)));
  1207. { determine the size with publicsyms^.datasize, because }
  1208. { size gives back 4 for CLASSes }
  1209. datasegment^.concat(new(pai_const,init_32bit(aktclass^.publicsyms^.datasize)));
  1210. datasegment^.concat(new(pai_const,init_32bit(-aktclass^.publicsyms^.datasize)));
  1211. { write pointer to parent VMT, this isn't implemented in TP }
  1212. { but this is not used in FPC ? (PM) }
  1213. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1214. if assigned(aktclass^.childof) then
  1215. begin
  1216. datasegment^.concat(new(pai_const,init_symbol(strpnew(aktclass^.childof^.vmt_mangledname))));
  1217. if aktclass^.childof^.owner^.symtabletype=unitsymtable then
  1218. concat_external(aktclass^.childof^.vmt_mangledname,EXT_NEAR);
  1219. end
  1220. else
  1221. datasegment^.concat(new(pai_const,init_32bit(0)));
  1222. { this generates the entries }
  1223. genvmt(aktclass);
  1224. { restore old state }
  1225. symtablestack:=symtablestack^.next;
  1226. procinfo._class:=nil;
  1227. {Restore the aktprocsym.}
  1228. aktprocsym:=oldprocsym;
  1229. object_dec:=aktclass;
  1230. end;
  1231. { reads a record declaration }
  1232. function record_dec : pdef;
  1233. var
  1234. symtable : psymtable;
  1235. begin
  1236. symtable:=new(psymtable,init(recordsymtable));
  1237. symtable^.next:=symtablestack;
  1238. symtablestack:=symtable;
  1239. consume(_RECORD);
  1240. read_var_decs(true,false);
  1241. { may be scale record size to a size of n*4 ? }
  1242. if ((symtablestack^.datasize mod aktpackrecords)<>0) then
  1243. inc(symtablestack^.datasize,aktpackrecords-(symtablestack^.datasize mod aktpackrecords));
  1244. consume(_END);
  1245. symtablestack:=symtable^.next;
  1246. record_dec:=new(precdef,init(symtable));
  1247. end;
  1248. { search in symtablestack used, but not defined type }
  1249. procedure testforward_types(p : psym);{$ifndef FPC}far;{$endif}
  1250. begin
  1251. if (p^.typ=typesym) and ((p^.properties and sp_forwarddef)<>0) then
  1252. Message(sym_e_type_id_not_defined);
  1253. end;
  1254. { reads a type definition and returns a pointer to it }
  1255. function read_type(const name : stringid) : pdef;
  1256. function handle_procvar:Pprocvardef;
  1257. var
  1258. sc : pstringcontainer;
  1259. s : string;
  1260. p : pdef;
  1261. varspez : tvarspez;
  1262. procvardef : pprocvardef;
  1263. begin
  1264. procvardef:=new(pprocvardef,init);
  1265. if token=LKLAMMER then
  1266. begin
  1267. consume(LKLAMMER);
  1268. inc(testcurobject);
  1269. repeat
  1270. if token=_VAR then
  1271. begin
  1272. consume(_VAR);
  1273. varspez:=vs_var;
  1274. end
  1275. else if token=_CONST then
  1276. begin
  1277. consume(_CONST);
  1278. varspez:=vs_const;
  1279. end
  1280. else varspez:=vs_value;
  1281. sc:=idlist;
  1282. if token=COLON then
  1283. begin
  1284. consume(COLON);
  1285. if token=_ARRAY then
  1286. begin
  1287. {
  1288. if (varspez<>vs_const) and
  1289. (varspez<>vs_var) then
  1290. begin
  1291. varspez:=vs_const;
  1292. Message(parser_e_illegal_open_parameter);
  1293. end;
  1294. }
  1295. consume(_ARRAY);
  1296. consume(_OF);
  1297. { define range and type of range }
  1298. p:=new(parraydef,init(0,-1,s32bitdef));
  1299. { define field type }
  1300. parraydef(p)^.definition:=single_type(s);
  1301. end
  1302. else
  1303. p:=single_type(s);
  1304. end
  1305. else
  1306. p:=new(pformaldef,init);
  1307. s:=sc^.get;
  1308. while s<>'' do
  1309. begin
  1310. procvardef^.concatdef(p,varspez);
  1311. s:=sc^.get;
  1312. end;
  1313. dispose(sc,done);
  1314. if token=SEMICOLON then consume(SEMICOLON)
  1315. else break;
  1316. until false;
  1317. dec(testcurobject);
  1318. consume(RKLAMMER);
  1319. end;
  1320. handle_procvar:=procvardef;
  1321. end;
  1322. var
  1323. hp1,p : pdef;
  1324. aufdef : penumdef;
  1325. aufsym : penumsym;
  1326. ap : parraydef;
  1327. s : stringid;
  1328. l,v,oldaktpackrecords : longint;
  1329. hs : string;
  1330. procedure expr_type;
  1331. var
  1332. pt1,pt2 : ptree;
  1333. begin
  1334. { use of current parsed object ? }
  1335. if (token=ID) and (testcurobject=2) and (curobjectname=pattern) then
  1336. begin
  1337. consume(ID);
  1338. p:=aktobjectdef;
  1339. exit;
  1340. end;
  1341. { we can't accept a equal in type }
  1342. pt1:=comp_expr(not(ignore_equal));
  1343. if (pt1^.treetype=typen) and (token<>POINTPOINT) then
  1344. begin
  1345. { a simple type renaming }
  1346. p:=pt1^.resulttype;
  1347. end
  1348. else
  1349. begin
  1350. { range type }
  1351. consume(POINTPOINT);
  1352. { range type declaration }
  1353. do_firstpass(pt1);
  1354. pt2:=comp_expr(not(ignore_equal));
  1355. do_firstpass(pt2);
  1356. { valid expression ? }
  1357. if (pt1^.treetype<>ordconstn) or
  1358. (pt2^.treetype<>ordconstn) then
  1359. Begin
  1360. Message(sym_e_error_in_type_def);
  1361. { Here we create a node type with a range of 0 }
  1362. { To make sure that no crashes will occur later }
  1363. { on in the compiler. }
  1364. p:=new(porddef,init(uauto,0,0));
  1365. end
  1366. else
  1367. p:=new(porddef,init(uauto,pt1^.value,pt2^.value));
  1368. disposetree(pt2);
  1369. end;
  1370. disposetree(pt1);
  1371. end;
  1372. var
  1373. pt : ptree;
  1374. procedure array_dec;
  1375. begin
  1376. consume(_ARRAY);
  1377. consume(LECKKLAMMER);
  1378. p:=nil;
  1379. repeat
  1380. { read the expression and check it }
  1381. pt:=expr;
  1382. if pt^.treetype=typen then
  1383. begin
  1384. if pt^.resulttype^.deftype=enumdef then
  1385. begin
  1386. if p=nil then
  1387. begin
  1388. ap:=new(parraydef,
  1389. init(0,penumdef(pt^.resulttype)^.max,pt^.resulttype));
  1390. p:=ap;
  1391. end
  1392. else
  1393. begin
  1394. ap^.definition:=new(parraydef,
  1395. init(0,penumdef(pt^.resulttype)^.max,pt^.resulttype));
  1396. ap:=parraydef(ap^.definition);
  1397. end;
  1398. end
  1399. else if pt^.resulttype^.deftype=orddef then
  1400. begin
  1401. case porddef(pt^.resulttype)^.typ of
  1402. s8bit,u8bit,s16bit,u16bit,s32bit :
  1403. begin
  1404. if p=nil then
  1405. begin
  1406. ap:=new(parraydef,init(porddef(pt^.resulttype)^.low,
  1407. porddef(pt^.resulttype)^.high,pt^.resulttype));
  1408. p:=ap;
  1409. end
  1410. else
  1411. begin
  1412. ap^.definition:=new(parraydef,init(porddef(pt^.resulttype)^.low,
  1413. porddef(pt^.resulttype)^.high,pt^.resulttype));
  1414. ap:=parraydef(ap^.definition);
  1415. end;
  1416. end;
  1417. bool8bit:
  1418. begin
  1419. if p=nil then
  1420. begin
  1421. ap:=new(parraydef,init(0,1,pt^.resulttype));
  1422. p:=ap;
  1423. end
  1424. else
  1425. begin
  1426. ap^.definition:=new(parraydef,init(0,1,pt^.resulttype));
  1427. ap:=parraydef(ap^.definition);
  1428. end;
  1429. end;
  1430. uchar:
  1431. begin
  1432. if p=nil then
  1433. begin
  1434. ap:=new(parraydef,init(0,255,pt^.resulttype));
  1435. p:=ap;
  1436. end
  1437. else
  1438. begin
  1439. ap^.definition:=new(parraydef,init(0,255,pt^.resulttype));
  1440. ap:=parraydef(ap^.definition);
  1441. end;
  1442. end;
  1443. else Message(sym_e_error_in_type_def);
  1444. end;
  1445. end
  1446. else Message(sym_e_error_in_type_def);
  1447. end
  1448. else
  1449. begin
  1450. do_firstpass(pt);
  1451. if (pt^.treetype<>rangen) or
  1452. (pt^.left^.treetype<>ordconstn) then
  1453. Message(sym_e_error_in_type_def);
  1454. { force the registration of the ranges }
  1455. {$ifndef GDB}
  1456. if pt^.right^.resulttype=pdef(s32bitdef) then
  1457. pt^.right^.resulttype:=new(porddef,init(
  1458. s32bit,$80000000,$7fffffff));
  1459. {$endif GDB}
  1460. if p=nil then
  1461. begin
  1462. ap:=new(parraydef,init(pt^.left^.value,pt^.right^.value,pt^.right^.resulttype));
  1463. p:=ap;
  1464. end
  1465. else
  1466. begin
  1467. ap^.definition:=new(parraydef,init(pt^.left^.value,pt^.right^.value,pt^.right^.resulttype));
  1468. ap:=parraydef(ap^.definition);
  1469. end;
  1470. end;
  1471. disposetree(pt);
  1472. if token=COMMA then consume(COMMA)
  1473. else break;
  1474. until false;
  1475. consume(RECKKLAMMER);
  1476. consume(_OF);
  1477. hp1:=read_type('');
  1478. { if no error, set element type }
  1479. if assigned(ap) then
  1480. ap^.definition:=hp1;
  1481. end;
  1482. begin
  1483. case token of
  1484. _STRING,_FILE:
  1485. p:=single_type(hs);
  1486. LKLAMMER:
  1487. begin
  1488. consume(LKLAMMER);
  1489. l:=-1;
  1490. aufsym := Nil;
  1491. aufdef:=new(penumdef,init);
  1492. repeat
  1493. s:=pattern;
  1494. consume(ID);
  1495. if token=ASSIGNMENT then
  1496. begin
  1497. consume(ASSIGNMENT);
  1498. v:=get_intconst;
  1499. { please leave that a note, allows type save }
  1500. { declarations in the win32 units ! }
  1501. if v<=l then
  1502. Message(parser_n_duplicate_enum);
  1503. l:=v;
  1504. end
  1505. else
  1506. inc(l);
  1507. constsymtable^.insert(new(penumsym,init(s,aufdef,l)));
  1508. if token=COMMA then
  1509. consume(COMMA)
  1510. else
  1511. break;
  1512. until false;
  1513. aufdef^.max:=l;
  1514. p:=aufdef;
  1515. consume(RKLAMMER);
  1516. end;
  1517. _ARRAY:
  1518. array_dec;
  1519. _SET:
  1520. begin
  1521. consume(_SET);
  1522. consume(_OF);
  1523. hp1:=read_type('');
  1524. case hp1^.deftype of
  1525. enumdef : p:=new(psetdef,init(hp1,penumdef(hp1)^.max));
  1526. orddef : begin
  1527. case porddef(hp1)^.typ of
  1528. uchar : p:=new(psetdef,init(hp1,255));
  1529. u8bit,s8bit,u16bit,s16bit,s32bit :
  1530. begin
  1531. if (porddef(hp1)^.low>=0) then
  1532. p:=new(psetdef,init(hp1,porddef(hp1)^.high))
  1533. else Message(sym_e_ill_type_decl_set);
  1534. end;
  1535. else Message(sym_e_ill_type_decl_set);
  1536. end;
  1537. end;
  1538. else Message(sym_e_ill_type_decl_set);
  1539. end;
  1540. end;
  1541. CARET:
  1542. begin
  1543. consume(CARET);
  1544. { forwards allowed only inside TYPE statements }
  1545. if typecanbeforward then
  1546. forwardsallowed:=true;
  1547. hp1:=single_type(hs);
  1548. p:=new(ppointerdef,init(hp1));
  1549. {I add big troubles here
  1550. with var p : ^byte in graph.putimage
  1551. because a save_forward was called and
  1552. no resolve forward
  1553. => so the definition was rewritten after
  1554. having been disposed !!
  1555. Strange problems appeared !!!!}
  1556. {Anyhow forwards should only be allowed
  1557. inside a type statement ??
  1558. don't you think so }
  1559. if (lasttypesym<>nil)
  1560. and ((lasttypesym^.properties and sp_forwarddef)<>0) then
  1561. lasttypesym^.forwardpointer:=ppointerdef(p);
  1562. forwardsallowed:=false;
  1563. end;
  1564. _RECORD:
  1565. p:=record_dec;
  1566. _PACKED:
  1567. begin
  1568. consume(_PACKED);
  1569. if token=_ARRAY then
  1570. array_dec
  1571. else
  1572. begin
  1573. oldaktpackrecords:=aktpackrecords;
  1574. aktpackrecords:=1;
  1575. if token in [_CLASS,_OBJECT] then
  1576. p:=object_dec(name,nil)
  1577. else
  1578. p:=record_dec;
  1579. aktpackrecords:=oldaktpackrecords;
  1580. end;
  1581. end;
  1582. _CLASS,
  1583. _OBJECT:
  1584. p:=object_dec(name,nil);
  1585. _PROCEDURE:
  1586. begin
  1587. consume(_PROCEDURE);
  1588. p:=handle_procvar;
  1589. if token=_OF then
  1590. begin
  1591. consume(_OF);
  1592. consume(_OBJECT);
  1593. pprocvardef(p)^.options:=pprocvardef(p)^.options or pomethodpointer;
  1594. end;
  1595. end;
  1596. _FUNCTION:
  1597. begin
  1598. consume(_FUNCTION);
  1599. p:=handle_procvar;
  1600. consume(COLON);
  1601. pprocvardef(p)^.retdef:=single_type(hs);
  1602. if token=_OF then
  1603. begin
  1604. consume(_OF);
  1605. consume(_OBJECT);
  1606. pprocvardef(p)^.options:=pprocvardef(p)^.options or pomethodpointer;
  1607. end;
  1608. end;
  1609. else
  1610. expr_type;
  1611. end;
  1612. read_type:=p;
  1613. end;
  1614. { reads a type declaration to the symbol table }
  1615. procedure type_dec;
  1616. var
  1617. typename : stringid;
  1618. newtype : ptypesym;
  1619. {$ifdef dummy}
  1620. olddef,newdef : pdef;
  1621. s : string;
  1622. {$endif dummy}
  1623. begin
  1624. block_type:=bt_type;
  1625. consume(_TYPE);
  1626. typecanbeforward:=true;
  1627. repeat
  1628. typename:=pattern;
  1629. consume(ID);
  1630. consume(EQUAL);
  1631. { here you loose the strictness of pascal
  1632. for which a redefinition like
  1633. childtype = parenttype;
  1634. child2type = parenttype;
  1635. does not make the two child types equal !!
  1636. here all vars from childtype and child2type
  1637. get the definition of parenttype !! }
  1638. {$ifdef testequaltype}
  1639. if (token = ID) or (token=_FILE) or (token=_STRING) then
  1640. begin
  1641. olddef := single_type(s);
  1642. { make a clone of olddef }
  1643. { is that ok ??? }
  1644. getmem(newdef,SizeOf(olddef));
  1645. move(olddef^,newdef^,SizeOf(olddef));
  1646. newtype:=new(ptypesym,init(typename,newdef));
  1647. symtablestack^.insert(newtype);
  1648. end
  1649. else
  1650. {$endif testequaltype}
  1651. begin
  1652. getsym(typename,false);
  1653. { check if it is the definition of a forward defined class }
  1654. if assigned(srsym) and (token=_CLASS) and
  1655. (srsym^.typ=typesym) and
  1656. (ptypesym(srsym)^.definition^.deftype=objectdef) and
  1657. ((pobjectdef(ptypesym(srsym)^.definition)^.options and oo_isforward)<>0) and
  1658. ((pobjectdef(ptypesym(srsym)^.definition)^.options and oois_class)<>0) then
  1659. begin
  1660. { we can ignore the result }
  1661. { the definition is modified }
  1662. object_dec(typename,pobjectdef(ptypesym(srsym)^.definition));
  1663. newtype:=ptypesym(srsym);
  1664. end
  1665. else
  1666. begin
  1667. newtype:=new(ptypesym,init(typename,read_type(typename)));
  1668. symtablestack^.insert(newtype);
  1669. end;
  1670. end;
  1671. consume(SEMICOLON);
  1672. if assigned(newtype^.definition) and (newtype^.definition^.deftype=procvardef) then
  1673. parse_var_proc_directives(newtype);
  1674. until token<>ID;
  1675. typecanbeforward:=false;
  1676. {$ifdef tp}
  1677. symtablestack^.foreach(testforward_types);
  1678. {$else}
  1679. symtablestack^.foreach(@testforward_types);
  1680. {$endif}
  1681. resolve_forwards;
  1682. block_type:=bt_general;
  1683. end;
  1684. procedure var_dec;
  1685. { parses varaible declarations and inserts them in }
  1686. { the top symbol table of symtablestack }
  1687. begin
  1688. consume(_VAR);
  1689. read_var_decs(false,false);
  1690. end;
  1691. procedure Not_supported_for_inline(t : ttoken);
  1692. begin
  1693. if assigned(aktprocsym) and
  1694. ((aktprocsym^.definition^.options and poinline)<>0) then
  1695. Begin
  1696. Comment(V_Warning,tokenstring(t)+' not yet supported inside inline procedure/function ');
  1697. Comment(V_Warning,'inlining disabled');
  1698. aktprocsym^.definition^.options:= aktprocsym^.definition^.options and not poinline;
  1699. End;
  1700. end;
  1701. procedure read_declarations(islibrary : boolean);
  1702. begin
  1703. repeat
  1704. case token of
  1705. _LABEL:
  1706. begin
  1707. Not_supported_for_inline(token);
  1708. label_dec;
  1709. end;
  1710. _CONST:
  1711. begin
  1712. Not_supported_for_inline(token);
  1713. const_dec;
  1714. end;
  1715. _TYPE:
  1716. begin
  1717. Not_supported_for_inline(token);
  1718. type_dec;
  1719. end;
  1720. _VAR:
  1721. var_dec;
  1722. _CONSTRUCTOR,_DESTRUCTOR,
  1723. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  1724. begin
  1725. Not_supported_for_inline(token);
  1726. unter_dec;
  1727. end;
  1728. _EXPORTS:
  1729. begin
  1730. { here we should be at lexlevel 1, no ? PM }
  1731. Not_supported_for_inline(token);
  1732. if islibrary then
  1733. read_exports
  1734. else
  1735. break;
  1736. end
  1737. else break;
  1738. end;
  1739. until false;
  1740. end;
  1741. procedure read_interface_declarations;
  1742. begin
  1743. {Since the body is now parsed at lexlevel 1, and the declarations
  1744. must be parsed at the same lexlevel we increase the lexlevel.}
  1745. inc(lexlevel);
  1746. repeat
  1747. case token of
  1748. _CONST : const_dec;
  1749. _TYPE : type_dec;
  1750. _VAR : var_dec;
  1751. _FUNCTION,
  1752. _PROCEDURE,
  1753. _OPERATOR : unter_dec;
  1754. else
  1755. break;
  1756. end;
  1757. until false;
  1758. dec(lexlevel);
  1759. end;
  1760. end.
  1761. {
  1762. $Log$
  1763. Revision 1.29 1998-06-25 14:04:21 peter
  1764. + internal inc/dec
  1765. Revision 1.28 1998/06/24 12:26:45 peter
  1766. * stricter var parsing like tp7 and some optimizes with directive
  1767. parsing
  1768. Revision 1.27 1998/06/12 16:15:34 pierre
  1769. * external name 'C_var';
  1770. export name 'intern_C_var';
  1771. cdecl;
  1772. cdecl;external;
  1773. are now supported only with -Sv switch
  1774. Revision 1.25 1998/06/09 16:01:45 pierre
  1775. + added procedure directive parsing for procvars
  1776. (accepted are popstack cdecl and pascal)
  1777. + added C vars with the following syntax
  1778. var C calias 'true_c_name';(can be followed by external)
  1779. reason is that you must add the Cprefix
  1780. which is target dependent
  1781. Revision 1.24 1998/06/05 14:37:32 pierre
  1782. * fixes for inline for operators
  1783. * inline procedure more correctly restricted
  1784. Revision 1.23 1998/06/04 23:51:50 peter
  1785. * m68k compiles
  1786. + .def file creation moved to gendef.pas so it could also be used
  1787. for win32
  1788. Revision 1.22 1998/06/03 22:48:59 peter
  1789. + wordbool,longbool
  1790. * rename bis,von -> high,low
  1791. * moved some systemunit loading/creating to psystem.pas
  1792. Revision 1.21 1998/06/03 22:14:19 florian
  1793. * problem with sizes of classes fixed (if the anchestor was declared
  1794. forward, the compiler doesn't update the child classes size)
  1795. Revision 1.20 1998/05/28 14:35:54 peter
  1796. * nicer error message when no id is used after var
  1797. Revision 1.19 1998/05/23 01:21:19 peter
  1798. + aktasmmode, aktoptprocessor, aktoutputformat
  1799. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1800. + $LIBNAME to set the library name where the unit will be put in
  1801. * splitted cgi386 a bit (codeseg to large for bp7)
  1802. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1803. Revision 1.18 1998/05/20 09:42:35 pierre
  1804. + UseTokenInfo now default
  1805. * unit in interface uses and implementation uses gives error now
  1806. * only one error for unknown symbol (uses lastsymknown boolean)
  1807. the problem came from the label code !
  1808. + first inlined procedures and function work
  1809. (warning there might be allowed cases were the result is still wrong !!)
  1810. * UseBrower updated gives a global list of all position of all used symbols
  1811. with switch -gb
  1812. Revision 1.17 1998/05/11 13:07:55 peter
  1813. + $ifdef NEWPPU for the new ppuformat
  1814. + $define GDB not longer required
  1815. * removed all warnings and stripped some log comments
  1816. * no findfirst/findnext anymore to remove smartlink *.o files
  1817. Revision 1.16 1998/05/05 12:05:42 florian
  1818. * problems with properties fixed
  1819. * crash fixed: i:=l when i and l are undefined, was a problem with
  1820. implementation of private/protected
  1821. Revision 1.15 1998/05/01 09:01:23 florian
  1822. + correct semantics of private and protected
  1823. * small fix in variable scope:
  1824. a id can be used in a parameter list of a method, even it is used in
  1825. an anchestor class as field id
  1826. Revision 1.14 1998/05/01 07:43:56 florian
  1827. + basics for rtti implemented
  1828. + switch $m (generate rtti for published sections)
  1829. Revision 1.13 1998/04/30 15:59:41 pierre
  1830. * GDB works again better :
  1831. correct type info in one pass
  1832. + UseTokenInfo for better source position
  1833. * fixed one remaining bug in scanner for line counts
  1834. * several little fixes
  1835. Revision 1.12 1998/04/29 10:33:57 pierre
  1836. + added some code for ansistring (not complete nor working yet)
  1837. * corrected operator overloading
  1838. * corrected nasm output
  1839. + started inline procedures
  1840. + added starstarn : use ** for exponentiation (^ gave problems)
  1841. + started UseTokenInfo cond to get accurate positions
  1842. Revision 1.11 1998/04/28 11:45:52 florian
  1843. * make it compilable with TP
  1844. + small COM problems solved to compile classes.pp
  1845. Revision 1.10 1998/04/27 23:10:28 peter
  1846. + new scanner
  1847. * $makelib -> if smartlink
  1848. * small filename fixes pmodule.setfilename
  1849. * moved import from files.pas -> import.pas
  1850. Revision 1.9 1998/04/10 21:36:56 florian
  1851. + some stuff to support method pointers (procedure of object) added
  1852. (declaration, parameter handling)
  1853. Revision 1.8 1998/04/10 15:39:48 florian
  1854. * more fixes to get classes.pas compiled
  1855. Revision 1.7 1998/04/09 23:02:15 florian
  1856. * small problems solved to get remake3 work
  1857. Revision 1.6 1998/04/09 22:16:35 florian
  1858. * problem with previous REGALLOC solved
  1859. * improved property support
  1860. Revision 1.5 1998/04/08 14:59:20 florian
  1861. * problem with new expr_type solved
  1862. Revision 1.4 1998/04/08 10:26:09 florian
  1863. * correct error handling of virtual constructors
  1864. * problem with new type declaration handling fixed
  1865. Revision 1.3 1998/04/07 22:45:05 florian
  1866. * bug0092, bug0115 and bug0121 fixed
  1867. + packed object/class/array
  1868. Revision 1.2 1998/04/05 13:58:35 peter
  1869. * fixed the -Ss bug
  1870. + warning for Virtual constructors
  1871. * helppages updated with -TGO32V1
  1872. }