psub.pas 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Florian Klaempfl, Daniel Mantione
  4. Does the parsing of the procedures/functions
  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 psub;
  19. interface
  20. uses
  21. cobjects,
  22. symconst,tokens,symtable;
  23. const
  24. pd_global = $1; { directive must be global }
  25. pd_body = $2; { directive needs a body }
  26. pd_implemen = $4; { directive can be used implementation section }
  27. pd_interface = $8; { directive can be used interface section }
  28. pd_object = $10; { directive can be used object declaration }
  29. pd_procvar = $20; { directive can be used procvar declaration }
  30. pd_notobject = $40;{ directive can not be used object declaration }
  31. procedure compile_proc_body(const proc_names:Tstringcontainer;
  32. make_global,parent_has_class:boolean);
  33. procedure parse_proc_head(options:tproctypeoption);
  34. procedure parse_proc_dec;
  35. function is_proc_directive(tok:ttoken):boolean;
  36. procedure parse_var_proc_directives(var sym : psym);
  37. procedure parse_object_proc_directives(var sym : pprocsym);
  38. procedure read_proc;
  39. implementation
  40. uses
  41. globtype,systems,
  42. strings,globals,verbose,files,
  43. scanner,aasm,tree,types,
  44. import,gendef,
  45. {$ifdef newcg}
  46. cgbase,
  47. {$else newcg}
  48. hcodegen,
  49. {$endif newcg}
  50. temp_gen,pass_1,cpubase,cpuasm
  51. {$ifndef NOPASS2}
  52. ,pass_2
  53. {$endif}
  54. {$ifdef GDB}
  55. ,gdb
  56. {$endif GDB}
  57. {$ifdef newcg}
  58. {$ifndef NOOPT}
  59. ,aopt
  60. {$endif}
  61. {$else}
  62. {$ifdef i386}
  63. ,tgeni386
  64. ,cgai386
  65. {$ifndef NOOPT}
  66. ,aopt386
  67. {$endif}
  68. {$endif}
  69. {$ifdef m68k}
  70. ,tgen68k,cga68k
  71. {$endif}
  72. {$endif newcg}
  73. { parser specific stuff }
  74. ,pbase,ptype,pdecl,pexpr,pstatmnt
  75. {$ifdef newcg}
  76. ,tgcpu,convtree,cgobj,tgeni386 { for the new code generator tgeni386 is only a dummy }
  77. {$endif newcg}
  78. ;
  79. var
  80. realname:string; { contains the real name of a procedure as it's typed }
  81. procedure parse_proc_head(options:tproctypeoption);
  82. var sp:stringid;
  83. pd:Pprocdef;
  84. paramoffset:longint;
  85. sym:Psym;
  86. hs:string;
  87. st : psymtable;
  88. overloaded_level:word;
  89. storepos,procstartfilepos : tfileposinfo;
  90. begin
  91. { Save the position where this procedure really starts and set col to 1 which
  92. looks nicer }
  93. procstartfilepos:=tokenpos;
  94. { procstartfilepos.column:=1; I do not agree here !!
  95. lets keep excat position PM }
  96. if (options=potype_operator) then
  97. begin
  98. sp:=overloaded_names[optoken];
  99. realname:=sp;
  100. end
  101. else
  102. begin
  103. sp:=pattern;
  104. realname:=orgpattern;
  105. consume(_ID);
  106. end;
  107. { method ? }
  108. if not(parse_only) and try_to_consume(_POINT) then
  109. begin
  110. storepos:=tokenpos;
  111. tokenpos:=procstartfilepos;
  112. getsym(sp,true);
  113. sym:=srsym;
  114. tokenpos:=storepos;
  115. { load proc name }
  116. sp:=pattern;
  117. realname:=orgpattern;
  118. procstartfilepos:=tokenpos;
  119. { qualifier is class name ? }
  120. if (sym^.typ<>typesym) or
  121. (ptypesym(sym)^.definition^.deftype<>objectdef) then
  122. begin
  123. Message(parser_e_class_id_expected);
  124. aktprocsym:=nil;
  125. consume(_ID);
  126. end
  127. else
  128. begin
  129. { used to allow private syms to be seen }
  130. aktobjectdef:=pobjectdef(ptypesym(sym)^.definition);
  131. procinfo^._class:=pobjectdef(ptypesym(sym)^.definition);
  132. aktprocsym:=pprocsym(procinfo^._class^.symtable^.search(sp));
  133. consume(_ID);
  134. {The procedure has been found. So it is
  135. a global one. Set the flags to mark this.}
  136. procinfo^.flags:=procinfo^.flags or pi_is_global;
  137. aktobjectdef:=nil;
  138. { we solve this below }
  139. if not(assigned(aktprocsym)) then
  140. Message(parser_e_methode_id_expected);
  141. end;
  142. end
  143. else
  144. begin
  145. { check for constructor/destructor which is not allowed here }
  146. if (not parse_only) and
  147. (options in [potype_constructor,potype_destructor]) then
  148. Message(parser_e_constructors_always_objects);
  149. tokenpos:=procstartfilepos;
  150. aktprocsym:=pprocsym(symtablestack^.search(sp));
  151. if not(parse_only) then
  152. begin
  153. {The procedure we prepare for is in the implementation
  154. part of the unit we compile. It is also possible that we
  155. are compiling a program, which is also some kind of
  156. implementaion part.
  157. We need to find out if the procedure is global. If it is
  158. global, it is in the global symtable.}
  159. if not assigned(aktprocsym) and
  160. (symtablestack^.symtabletype=staticsymtable) then
  161. begin
  162. {Search the procedure in the global symtable.}
  163. aktprocsym:=Pprocsym(search_a_symtable(sp,globalsymtable));
  164. if assigned(aktprocsym) then
  165. begin
  166. {Check if it is a procedure.}
  167. if aktprocsym^.typ<>procsym then
  168. DuplicateSym(aktprocsym);
  169. {The procedure has been found. So it is
  170. a global one. Set the flags to mark this.}
  171. procinfo^.flags:=procinfo^.flags or pi_is_global;
  172. end;
  173. end;
  174. end;
  175. end;
  176. { Create the mangledname }
  177. {$ifndef UseNiceNames}
  178. if assigned(procinfo^._class) then
  179. begin
  180. if (pos('_$$_',procprefix)=0) then
  181. hs:=procprefix+'_$$_'+procinfo^._class^.objname^+'_$$_'+sp
  182. else
  183. hs:=procprefix+'_$'+sp;
  184. end
  185. else
  186. begin
  187. if lexlevel=normal_function_level then
  188. hs:=procprefix+'_'+sp
  189. else
  190. hs:=procprefix+'_$'+sp;
  191. end;
  192. {$else UseNiceNames}
  193. if assigned(procinfo^._class) then
  194. begin
  195. if (pos('_5Class_',procprefix)=0) then
  196. hs:=procprefix+'_5Class_'+procinfo^._class^.name^+'_'+tostr(length(sp))+sp
  197. else
  198. hs:=procprefix+'_'+tostr(length(sp))+sp;
  199. end
  200. else
  201. begin
  202. if lexlevel=normal_function_level then
  203. hs:=procprefix+'_'+tostr(length(sp))+sp
  204. else
  205. hs:=lowercase(procprefix)+'_'+tostr(length(sp))+sp;
  206. end;
  207. {$endif UseNiceNames}
  208. if assigned(aktprocsym) then
  209. begin
  210. { Check if overloading is enabled }
  211. if not(m_fpc in aktmodeswitches) then
  212. begin
  213. if aktprocsym^.typ<>procsym then
  214. begin
  215. DuplicateSym(aktprocsym);
  216. { try to recover by creating a new aktprocsym }
  217. tokenpos:=procstartfilepos;
  218. aktprocsym:=new(pprocsym,init(sp));
  219. end
  220. else
  221. begin
  222. if not(aktprocsym^.definition^.forwarddef) then
  223. Message(parser_e_procedure_overloading_is_off);
  224. end;
  225. end
  226. else
  227. begin
  228. { Check if the overloaded sym is realy a procsym }
  229. if aktprocsym^.typ<>procsym then
  230. begin
  231. Message1(parser_e_overloaded_no_procedure,aktprocsym^.name);
  232. { try to recover by creating a new aktprocsym }
  233. tokenpos:=procstartfilepos;
  234. aktprocsym:=new(pprocsym,init(sp));
  235. end;
  236. end;
  237. end
  238. else
  239. begin
  240. { create a new procsym and set the real filepos }
  241. tokenpos:=procstartfilepos;
  242. aktprocsym:=new(pprocsym,init(sp));
  243. { for operator we have only one definition for each overloaded
  244. operation }
  245. if (options=potype_operator) then
  246. begin
  247. { the only problem is that nextoverloaded might not be in a unit
  248. known for the unit itself }
  249. if assigned(overloaded_operators[optoken]) then
  250. aktprocsym^.definition:=overloaded_operators[optoken]^.definition;
  251. end;
  252. symtablestack^.insert(aktprocsym);
  253. end;
  254. st:=symtablestack;
  255. pd:=new(pprocdef,init);
  256. pd^.symtablelevel:=symtablestack^.symtablelevel;
  257. if assigned(procinfo^._class) then
  258. pd^._class := procinfo^._class;
  259. { set the options from the caller (podestructor or poconstructor) }
  260. pd^.proctypeoption:=options;
  261. { calculate the offset of the parameters }
  262. paramoffset:=8;
  263. { calculate frame pointer offset }
  264. if lexlevel>normal_function_level then
  265. begin
  266. procinfo^.framepointer_offset:=paramoffset;
  267. inc(paramoffset,target_os.size_of_pointer);
  268. { this is needed to get correct framepointer push for local
  269. forward functions !! }
  270. pd^.parast^.symtablelevel:=lexlevel;
  271. end;
  272. if assigned (procinfo^._Class) and
  273. not(procinfo^._Class^.is_class) and
  274. (pd^.proctypeoption in [potype_constructor,potype_destructor]) then
  275. inc(paramoffset,target_os.size_of_pointer);
  276. { self pointer offset }
  277. { self isn't pushed in nested procedure of methods }
  278. if assigned(procinfo^._class) and (lexlevel=normal_function_level) then
  279. begin
  280. procinfo^.selfpointer_offset:=paramoffset;
  281. if assigned(aktprocsym^.definition) and
  282. not(po_containsself in aktprocsym^.definition^.procoptions) then
  283. inc(paramoffset,target_os.size_of_pointer);
  284. end;
  285. { destructor flag ? }
  286. if assigned (procinfo^._Class) and
  287. procinfo^._class^.is_class and
  288. (pd^.proctypeoption=potype_destructor) then
  289. inc(paramoffset,target_os.size_of_pointer);
  290. procinfo^.call_offset:=paramoffset;
  291. pd^.parast^.datasize:=0;
  292. pd^.nextoverloaded:=aktprocsym^.definition;
  293. aktprocsym^.definition:=pd;
  294. { this is probably obsolete now PM }
  295. aktprocsym^.definition^.fileinfo:=procstartfilepos;
  296. aktprocsym^.definition^.setmangledname(hs);
  297. aktprocsym^.definition^.procsym:=aktprocsym;
  298. if not parse_only then
  299. begin
  300. overloaded_level:=0;
  301. { we need another procprefix !!! }
  302. { count, but only those in the same unit !!}
  303. while assigned(pd) and
  304. (pd^.owner^.symtabletype in [globalsymtable,staticsymtable]) do
  305. begin
  306. { only count already implemented functions }
  307. if not(pd^.forwarddef) then
  308. inc(overloaded_level);
  309. pd:=pd^.nextoverloaded;
  310. end;
  311. if overloaded_level>0 then
  312. procprefix:=hs+'$'+tostr(overloaded_level)+'$'
  313. else
  314. procprefix:=hs+'$';
  315. end;
  316. { this must also be inserted in the right symtable !! PM }
  317. { otherwise we get subbtle problems with
  318. definitions of args defs in staticsymtable for
  319. implementation of a global method }
  320. if token=_LKLAMMER then
  321. parameter_dec(aktprocsym^.definition);
  322. { so we only restore the symtable now }
  323. symtablestack:=st;
  324. if (options=potype_operator) then
  325. overloaded_operators[optoken]:=aktprocsym;
  326. end;
  327. procedure parse_proc_dec;
  328. var
  329. hs : string;
  330. isclassmethod : boolean;
  331. begin
  332. inc(lexlevel);
  333. { read class method }
  334. if token=_CLASS then
  335. begin
  336. consume(_CLASS);
  337. isclassmethod:=true;
  338. end
  339. else
  340. isclassmethod:=false;
  341. case token of
  342. _FUNCTION : begin
  343. consume(_FUNCTION);
  344. parse_proc_head(potype_none);
  345. if token<>_COLON then
  346. begin
  347. if not(aktprocsym^.definition^.forwarddef) or
  348. (m_repeat_forward in aktmodeswitches) then
  349. begin
  350. consume(_COLON);
  351. consume_all_until(_SEMICOLON);
  352. end;
  353. end
  354. else
  355. begin
  356. consume(_COLON);
  357. inc(testcurobject);
  358. aktprocsym^.definition^.retdef:=single_type(hs,false);
  359. aktprocsym^.definition^.test_if_fpu_result;
  360. dec(testcurobject);
  361. end;
  362. end;
  363. _PROCEDURE : begin
  364. consume(_PROCEDURE);
  365. parse_proc_head(potype_none);
  366. aktprocsym^.definition^.retdef:=voiddef;
  367. end;
  368. _CONSTRUCTOR : begin
  369. consume(_CONSTRUCTOR);
  370. parse_proc_head(potype_constructor);
  371. if assigned(procinfo^._class) and
  372. procinfo^._class^.is_class then
  373. begin
  374. { CLASS constructors return the created instance }
  375. aktprocsym^.definition^.retdef:=procinfo^._class;
  376. end
  377. else
  378. begin
  379. { OBJECT constructors return a boolean }
  380. {$IfDef GDB}
  381. { GDB doesn't like unnamed types !}
  382. aktprocsym^.definition^.retdef:=globaldef('boolean');
  383. {$else GDB}
  384. aktprocsym^.definition^.retdef:=new(porddef,init(bool8bit,0,1));
  385. {$Endif GDB}
  386. end;
  387. end;
  388. _DESTRUCTOR : begin
  389. consume(_DESTRUCTOR);
  390. parse_proc_head(potype_destructor);
  391. aktprocsym^.definition^.retdef:=voiddef;
  392. end;
  393. _OPERATOR : begin
  394. if lexlevel>normal_function_level then
  395. Message(parser_e_no_local_operator);
  396. consume(_OPERATOR);
  397. if not(token in [_PLUS..last_overloaded]) then
  398. Message(parser_e_overload_operator_failed);
  399. optoken:=token;
  400. consume(Token);
  401. procinfo^.flags:=procinfo^.flags or pi_operator;
  402. parse_proc_head(potype_operator);
  403. if token<>_ID then
  404. begin
  405. opsym:=nil;
  406. if not(m_result in aktmodeswitches) then
  407. consume(_ID);
  408. end
  409. else
  410. begin
  411. opsym:=new(pvarsym,init(pattern,voiddef));
  412. consume(_ID);
  413. end;
  414. if not try_to_consume(_COLON) then
  415. begin
  416. consume(_COLON);
  417. aktprocsym^.definition^.retdef:=generrordef;
  418. consume_all_until(_SEMICOLON);
  419. end
  420. else
  421. begin
  422. aktprocsym^.definition^.retdef:=
  423. single_type(hs,false);
  424. aktprocsym^.definition^.test_if_fpu_result;
  425. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  426. ((aktprocsym^.definition^.retdef^.deftype<>
  427. orddef) or (porddef(aktprocsym^.definition^.
  428. retdef)^.typ<>bool8bit)) then
  429. Message(parser_e_comparative_operator_return_boolean);
  430. if assigned(opsym) then
  431. opsym^.definition:=aktprocsym^.definition^.retdef;
  432. { We need to add the retrun type in the mangledname
  433. to allow overloading with just different results !! (PM) }
  434. aktprocsym^.definition^.setmangledname(
  435. aktprocsym^.definition^.mangledname+'$$'+hs);
  436. end;
  437. end;
  438. end;
  439. if isclassmethod and
  440. assigned(aktprocsym) then
  441. {$ifdef INCLUDEOK}
  442. include(aktprocsym^.definition^.procoptions,po_classmethod);
  443. {$else}
  444. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_classmethod];
  445. {$endif}
  446. consume(_SEMICOLON);
  447. dec(lexlevel);
  448. end;
  449. {****************************************************************************
  450. Procedure directive handlers
  451. ****************************************************************************}
  452. {$ifdef tp}
  453. {$F+}
  454. {$endif}
  455. procedure pd_far(const procnames:Tstringcontainer);
  456. begin
  457. Message(parser_w_proc_far_ignored);
  458. end;
  459. procedure pd_near(const procnames:Tstringcontainer);
  460. begin
  461. Message(parser_w_proc_near_ignored);
  462. end;
  463. procedure pd_export(const procnames:Tstringcontainer);
  464. begin
  465. if assigned(procinfo^._class) then
  466. Message(parser_e_methods_dont_be_export);
  467. if lexlevel<>normal_function_level then
  468. Message(parser_e_dont_nest_export);
  469. { only os/2 needs this }
  470. if target_info.target=target_i386_os2 then
  471. begin
  472. procnames.insert(realname);
  473. procinfo^.exported:=true;
  474. if cs_link_deffile in aktglobalswitches then
  475. deffile.AddExport(aktprocsym^.definition^.mangledname);
  476. end;
  477. end;
  478. procedure pd_inline(const procnames:Tstringcontainer);
  479. begin
  480. if not(cs_support_inline in aktmoduleswitches) then
  481. Message(parser_e_proc_inline_not_supported);
  482. end;
  483. procedure pd_forward(const procnames:Tstringcontainer);
  484. begin
  485. aktprocsym^.definition^.forwarddef:=true;
  486. end;
  487. procedure pd_stdcall(const procnames:Tstringcontainer);
  488. begin
  489. end;
  490. procedure pd_safecall(const procnames:Tstringcontainer);
  491. begin
  492. end;
  493. procedure pd_alias(const procnames:Tstringcontainer);
  494. begin
  495. consume(_COLON);
  496. procnames.insert(get_stringconst);
  497. end;
  498. procedure pd_asmname(const procnames:Tstringcontainer);
  499. begin
  500. aktprocsym^.definition^.setmangledname(target_os.Cprefix+pattern);
  501. if token=_CCHAR then
  502. consume(_CCHAR)
  503. else
  504. consume(_CSTRING);
  505. { we don't need anything else }
  506. aktprocsym^.definition^.forwarddef:=false;
  507. end;
  508. procedure pd_intern(const procnames:Tstringcontainer);
  509. begin
  510. consume(_COLON);
  511. aktprocsym^.definition^.extnumber:=get_intconst;
  512. end;
  513. procedure pd_system(const procnames:Tstringcontainer);
  514. begin
  515. aktprocsym^.definition^.setmangledname(realname);
  516. end;
  517. procedure pd_abstract(const procnames:Tstringcontainer);
  518. begin
  519. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  520. {$ifdef INCLUDEOK}
  521. include(aktprocsym^.definition^.procoptions,po_abstractmethod)
  522. {$else}
  523. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_abstractmethod]
  524. {$endif}
  525. else
  526. Message(parser_e_only_virtual_methods_abstract);
  527. { the method is defined }
  528. aktprocsym^.definition^.forwarddef:=false;
  529. end;
  530. procedure pd_virtual(const procnames:Tstringcontainer);
  531. begin
  532. if (aktprocsym^.definition^.proctypeoption=potype_constructor) and
  533. not(aktprocsym^.definition^._class^.is_class) then
  534. Message(parser_e_constructor_cannot_be_not_virtual);
  535. end;
  536. procedure pd_static(const procnames:Tstringcontainer);
  537. begin
  538. if (cs_static_keyword in aktmoduleswitches) then
  539. begin
  540. {$ifdef INCLUDEOK}
  541. include(aktprocsym^.symoptions,sp_static);
  542. include(aktprocsym^.definition^.procoptions,po_staticmethod);
  543. {$else}
  544. aktprocsym^.symoptions:=aktprocsym^.symoptions+[sp_static];
  545. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_staticmethod];
  546. {$endif}
  547. end;
  548. end;
  549. procedure pd_override(const procnames:Tstringcontainer);
  550. begin
  551. if not(aktprocsym^.definition^._class^.is_class) then
  552. Message(parser_e_no_object_override);
  553. end;
  554. procedure pd_message(const procnames:Tstringcontainer);
  555. var
  556. pt : ptree;
  557. begin
  558. { check parameter type }
  559. if not(po_containsself in aktprocsym^.definition^.procoptions) and
  560. ((aktprocsym^.definition^.para^.count<>1) or
  561. (pparaitem(aktprocsym^.definition^.para^.first)^.paratyp<>vs_var)) then
  562. Message(parser_e_ill_msg_param);
  563. pt:=comp_expr(true);
  564. do_firstpass(pt);
  565. if pt^.treetype=stringconstn then
  566. begin
  567. {$ifdef INCLUDEOK}
  568. include(aktprocsym^.definition^.procoptions,po_msgstr);
  569. {$else}
  570. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_msgstr];
  571. {$endif}
  572. aktprocsym^.definition^.messageinf.str:=strnew(pt^.value_str);
  573. end
  574. else
  575. if is_constintnode(pt) then
  576. begin
  577. {$ifdef INCLUDEOK}
  578. include(aktprocsym^.definition^.procoptions,po_msgint);
  579. {$else}
  580. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_msgint];
  581. {$endif}
  582. aktprocsym^.definition^.messageinf.i:=pt^.value;
  583. end
  584. else
  585. Message(parser_e_ill_msg_expr);
  586. disposetree(pt);
  587. end;
  588. procedure pd_cdecl(const procnames:Tstringcontainer);
  589. begin
  590. if aktprocsym^.definition^.deftype<>procvardef then
  591. aktprocsym^.definition^.setmangledname(target_os.Cprefix+realname);
  592. end;
  593. procedure pd_register(const procnames:Tstringcontainer);
  594. begin
  595. Message(parser_w_proc_register_ignored);
  596. end;
  597. procedure pd_syscall(const procnames:Tstringcontainer);
  598. begin
  599. aktprocsym^.definition^.forwarddef:=false;
  600. aktprocsym^.definition^.extnumber:=get_intconst;
  601. end;
  602. procedure pd_external(const procnames:Tstringcontainer);
  603. {
  604. If import_dll=nil the procedure is assumed to be in another
  605. object file. In that object file it should have the name to
  606. which import_name is pointing to. Otherwise, the procedure is
  607. assumed to be in the DLL to which import_dll is pointing to. In
  608. that case either import_nr<>0 or import_name<>nil is true, so
  609. the procedure is either imported by number or by name. (DM)
  610. }
  611. var
  612. import_dll,
  613. import_name : string;
  614. import_nr : word;
  615. begin
  616. aktprocsym^.definition^.forwarddef:=false;
  617. { If the procedure should be imported from a DLL, a constant string follows.
  618. This isn't really correct, an contant string expression follows
  619. so we check if an semicolon follows, else a string constant have to
  620. follow (FK) }
  621. import_nr:=0;
  622. import_name:='';
  623. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  624. begin
  625. import_dll:=get_stringconst;
  626. if (idtoken=_NAME) then
  627. begin
  628. consume(_NAME);
  629. import_name:=get_stringconst;
  630. end;
  631. if (idtoken=_INDEX) then
  632. begin
  633. {After the word index follows the index number in the DLL.}
  634. consume(_INDEX);
  635. import_nr:=get_intconst;
  636. end;
  637. if (import_nr=0) and (import_name='') then
  638. {if (aktprocsym^.definition^.options and pocdecl)<>0 then
  639. import_name:=aktprocsym^.definition^.mangledname
  640. else
  641. Message(parser_w_empty_import_name);}
  642. { this should work both for win32 and Linux !! PM }
  643. import_name:=realname;
  644. if not(current_module^.uses_imports) then
  645. begin
  646. current_module^.uses_imports:=true;
  647. importlib^.preparelib(current_module^.modulename^);
  648. end;
  649. if not(m_repeat_forward in aktmodeswitches) then
  650. begin
  651. { we can only have one overloaded here ! }
  652. if assigned(aktprocsym^.definition^.nextoverloaded) then
  653. importlib^.importprocedure(aktprocsym^.definition^.nextoverloaded^.mangledname,
  654. import_dll,import_nr,import_name)
  655. else
  656. importlib^.importprocedure(aktprocsym^.mangledname,import_dll,import_nr,import_name);
  657. end
  658. else
  659. importlib^.importprocedure(aktprocsym^.mangledname,import_dll,import_nr,import_name);
  660. end
  661. else
  662. begin
  663. if (idtoken=_NAME) then
  664. begin
  665. consume(_NAME);
  666. import_name:=get_stringconst;
  667. aktprocsym^.definition^.setmangledname(import_name);
  668. end
  669. else
  670. begin
  671. { external shouldn't override the cdecl/system name }
  672. if not (pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  673. aktprocsym^.definition^.setmangledname(aktprocsym^.name);
  674. end;
  675. end;
  676. end;
  677. {$ifdef TP}
  678. {$F-}
  679. {$endif}
  680. const
  681. namelength=15;
  682. type
  683. pd_handler=procedure(const procnames:Tstringcontainer);
  684. proc_dir_rec=record
  685. idtok : ttoken;
  686. pd_flags : longint;
  687. handler : pd_handler;
  688. pocall : tproccalloptions;
  689. pooption : tprocoptions;
  690. mutexclpocall : tproccalloptions;
  691. mutexclpotype : tproctypeoptions;
  692. mutexclpo : tprocoptions;
  693. end;
  694. const
  695. {Should contain the number of procedure directives we support.}
  696. num_proc_directives=29;
  697. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  698. (
  699. (
  700. idtok:_ABSTRACT;
  701. pd_flags : pd_interface+pd_object;
  702. handler : {$ifndef TP}@{$endif}pd_abstract;
  703. pocall : [];
  704. pooption : [po_abstractmethod];
  705. mutexclpocall : [pocall_internproc,pocall_inline];
  706. mutexclpotype : [potype_constructor,potype_destructor];
  707. mutexclpo : [po_exports,po_interrupt,po_external]
  708. ),(
  709. idtok:_ALIAS;
  710. pd_flags : pd_implemen+pd_body;
  711. handler : {$ifndef TP}@{$endif}pd_alias;
  712. pocall : [];
  713. pooption : [];
  714. mutexclpocall : [pocall_inline];
  715. mutexclpotype : [];
  716. mutexclpo : [po_external]
  717. ),(
  718. idtok:_ASMNAME;
  719. pd_flags : pd_interface+pd_implemen;
  720. handler : {$ifndef TP}@{$endif}pd_asmname;
  721. pocall : [pocall_cdecl,pocall_clearstack];
  722. pooption : [po_external];
  723. mutexclpocall : [pocall_internproc];
  724. mutexclpotype : [];
  725. mutexclpo : [po_external]
  726. ),(
  727. idtok:_ASSEMBLER;
  728. pd_flags : pd_implemen+pd_body;
  729. handler : nil;
  730. pocall : [];
  731. pooption : [po_assembler];
  732. mutexclpocall : [];
  733. mutexclpotype : [];
  734. mutexclpo : [po_external]
  735. ),(
  736. idtok:_CDECL;
  737. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  738. handler : {$ifndef TP}@{$endif}pd_cdecl;
  739. pocall : [pocall_cdecl,pocall_clearstack];
  740. pooption : [po_savestdregs];
  741. mutexclpocall : [pocall_internproc,pocall_leftright,pocall_inline];
  742. mutexclpotype : [];
  743. mutexclpo : [po_assembler,po_external]
  744. ),(
  745. idtok:_DYNAMIC;
  746. pd_flags : pd_interface+pd_object;
  747. handler : {$ifndef TP}@{$endif}pd_virtual;
  748. pocall : [];
  749. pooption : [po_virtualmethod];
  750. mutexclpocall : [pocall_internproc,pocall_inline];
  751. mutexclpotype : [];
  752. mutexclpo : [po_exports,po_interrupt,po_external]
  753. ),(
  754. idtok:_EXPORT;
  755. pd_flags : pd_body+pd_global+pd_interface+pd_implemen{??};
  756. handler : {$ifndef TP}@{$endif}pd_export;
  757. pocall : [];
  758. pooption : [po_exports];
  759. mutexclpocall : [pocall_internproc,pocall_inline];
  760. mutexclpotype : [];
  761. mutexclpo : [po_external,po_interrupt]
  762. ),(
  763. idtok:_EXTERNAL;
  764. pd_flags : pd_implemen+pd_interface;
  765. handler : {$ifndef TP}@{$endif}pd_external;
  766. pocall : [];
  767. pooption : [po_external];
  768. mutexclpocall : [pocall_internproc,pocall_inline,pocall_palmossyscall];
  769. mutexclpotype : [];
  770. mutexclpo : [po_exports,po_interrupt,po_assembler]
  771. ),(
  772. idtok:_FAR;
  773. pd_flags : pd_implemen+pd_body+pd_interface+pd_procvar;
  774. handler : {$ifndef TP}@{$endif}pd_far;
  775. pocall : [];
  776. pooption : [];
  777. mutexclpocall : [pocall_internproc,pocall_inline];
  778. mutexclpotype : [];
  779. mutexclpo : []
  780. ),(
  781. idtok:_FORWARD;
  782. pd_flags : pd_implemen;
  783. handler : {$ifndef TP}@{$endif}pd_forward;
  784. pocall : [];
  785. pooption : [];
  786. mutexclpocall : [pocall_internproc,pocall_inline];
  787. mutexclpotype : [];
  788. mutexclpo : [po_external]
  789. ),(
  790. idtok:_INLINE;
  791. pd_flags : pd_implemen+pd_body;
  792. handler : {$ifndef TP}@{$endif}pd_inline;
  793. pocall : [pocall_inline];
  794. pooption : [];
  795. mutexclpocall : [pocall_internproc];
  796. mutexclpotype : [potype_constructor,potype_destructor];
  797. mutexclpo : [po_exports,po_external,po_interrupt]
  798. ),(
  799. idtok:_INTERNCONST;
  800. pd_flags : pd_implemen+pd_body;
  801. handler : {$ifndef TP}@{$endif}pd_intern;
  802. pocall : [pocall_internconst];
  803. pooption : [];
  804. mutexclpocall : [];
  805. mutexclpotype : [potype_operator];
  806. mutexclpo : []
  807. ),(
  808. idtok:_INTERNPROC;
  809. pd_flags : pd_implemen;
  810. handler : {$ifndef TP}@{$endif}pd_intern;
  811. pocall : [pocall_internproc];
  812. pooption : [];
  813. mutexclpocall : [pocall_inline,pocall_clearstack,pocall_leftright,pocall_cdecl];
  814. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  815. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck]
  816. ),(
  817. idtok:_INTERRUPT;
  818. pd_flags : pd_implemen+pd_body;
  819. handler : nil;
  820. pocall : [];
  821. pooption : [po_interrupt];
  822. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_clearstack,pocall_leftright,pocall_inline];
  823. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  824. mutexclpo : [po_external]
  825. ),(
  826. idtok:_IOCHECK;
  827. pd_flags : pd_implemen+pd_body;
  828. handler : nil;
  829. pocall : [];
  830. pooption : [po_iocheck];
  831. mutexclpocall : [pocall_internproc];
  832. mutexclpotype : [];
  833. mutexclpo : [po_external]
  834. ),(
  835. idtok:_MESSAGE;
  836. pd_flags : pd_interface+pd_object;
  837. handler : {$ifndef TP}@{$endif}pd_message;
  838. pocall : [];
  839. pooption : []; { can be po_msgstr or po_msgint }
  840. mutexclpocall : [pocall_inline,pocall_internproc];
  841. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  842. mutexclpo : [po_interrupt,po_external]
  843. ),(
  844. idtok:_NEAR;
  845. pd_flags : pd_implemen+pd_body+pd_procvar;
  846. handler : {$ifndef TP}@{$endif}pd_near;
  847. pocall : [];
  848. pooption : [];
  849. mutexclpocall : [pocall_internproc];
  850. mutexclpotype : [];
  851. mutexclpo : []
  852. ),(
  853. idtok:_OVERRIDE;
  854. pd_flags : pd_interface+pd_object;
  855. handler : {$ifndef TP}@{$endif}pd_override;
  856. pocall : [];
  857. pooption : [po_overridingmethod,po_virtualmethod];
  858. mutexclpocall : [pocall_inline,pocall_internproc];
  859. mutexclpotype : [];
  860. mutexclpo : [po_exports,po_external,po_interrupt]
  861. ),(
  862. idtok:_PASCAL;
  863. pd_flags : pd_implemen+pd_body+pd_procvar;
  864. handler : nil;
  865. pocall : [pocall_leftright];
  866. pooption : [];
  867. mutexclpocall : [pocall_internproc];
  868. mutexclpotype : [];
  869. mutexclpo : [po_external]
  870. ),(
  871. idtok:_POPSTACK;
  872. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  873. handler : nil;
  874. pocall : [pocall_clearstack];
  875. pooption : [];
  876. mutexclpocall : [pocall_inline,pocall_internproc];
  877. mutexclpotype : [];
  878. mutexclpo : [po_assembler,po_external]
  879. ),(
  880. idtok:_PUBLIC;
  881. pd_flags : pd_implemen+pd_body+pd_global+pd_notobject;
  882. handler : nil;
  883. pocall : [];
  884. pooption : [];
  885. mutexclpocall : [pocall_internproc,pocall_inline];
  886. mutexclpotype : [];
  887. mutexclpo : [po_external]
  888. ),(
  889. idtok:_REGISTER;
  890. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  891. handler : {$ifndef TP}@{$endif}pd_register;
  892. pocall : [pocall_register];
  893. pooption : [];
  894. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_internproc];
  895. mutexclpotype : [];
  896. mutexclpo : [po_external]
  897. ),(
  898. idtok:_SAFECALL;
  899. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  900. handler : {$ifndef TP}@{$endif}pd_safecall;
  901. pocall : [pocall_safecall];
  902. pooption : [po_savestdregs];
  903. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_internproc,pocall_inline];
  904. mutexclpotype : [];
  905. mutexclpo : [po_external]
  906. ),(
  907. idtok:_SAVEREGISTERS;
  908. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  909. handler : nil;
  910. pocall : [];
  911. pooption : [po_saveregisters];
  912. mutexclpocall : [pocall_internproc];
  913. mutexclpotype : [];
  914. mutexclpo : [po_external]
  915. ),(
  916. idtok:_STATIC;
  917. pd_flags : pd_interface+pd_object;
  918. handler : {$ifndef TP}@{$endif}pd_static;
  919. pocall : [];
  920. pooption : [po_staticmethod];
  921. mutexclpocall : [pocall_inline,pocall_internproc];
  922. mutexclpotype : [potype_constructor,potype_destructor];
  923. mutexclpo : [po_external,po_interrupt,po_exports]
  924. ),(
  925. idtok:_STDCALL;
  926. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  927. handler : {$ifndef TP}@{$endif}pd_stdcall;
  928. pocall : [pocall_stdcall];
  929. pooption : [po_savestdregs];
  930. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_inline,pocall_internproc];
  931. mutexclpotype : [];
  932. mutexclpo : [po_external]
  933. ),(
  934. idtok:_SYSCALL;
  935. pd_flags : pd_interface;
  936. handler : {$ifndef TP}@{$endif}pd_syscall;
  937. pocall : [pocall_palmossyscall];
  938. pooption : [];
  939. mutexclpocall : [pocall_cdecl,pocall_inline,pocall_internproc];
  940. mutexclpotype : [];
  941. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  942. ),(
  943. idtok:_SYSTEM;
  944. pd_flags : pd_implemen;
  945. handler : {$ifndef TP}@{$endif}pd_system;
  946. pocall : [pocall_clearstack];
  947. pooption : [];
  948. mutexclpocall : [pocall_leftright,pocall_inline,pocall_internproc];
  949. mutexclpotype : [];
  950. mutexclpo : [po_external,po_assembler,po_interrupt]
  951. ),(
  952. idtok:_VIRTUAL;
  953. pd_flags : pd_interface+pd_object;
  954. handler : {$ifndef TP}@{$endif}pd_virtual;
  955. pocall : [];
  956. pooption : [po_virtualmethod];
  957. mutexclpocall : [pocall_inline,pocall_internproc];
  958. mutexclpotype : [];
  959. mutexclpo : [po_external,po_interrupt,po_exports]
  960. )
  961. );
  962. function is_proc_directive(tok:ttoken):boolean;
  963. var
  964. i : longint;
  965. begin
  966. is_proc_directive:=false;
  967. for i:=1 to num_proc_directives do
  968. if proc_direcdata[i].idtok=idtoken then
  969. begin
  970. is_proc_directive:=true;
  971. exit;
  972. end;
  973. end;
  974. function parse_proc_direc(const proc_names:Tstringcontainer;var pdflags:word):boolean;
  975. {
  976. Parse the procedure directive, returns true if a correct directive is found
  977. }
  978. var
  979. p : longint;
  980. found : boolean;
  981. name : string;
  982. begin
  983. parse_proc_direc:=false;
  984. name:=pattern;
  985. found:=false;
  986. for p:=1 to num_proc_directives do
  987. if proc_direcdata[p].idtok=idtoken then
  988. begin
  989. found:=true;
  990. break;
  991. end;
  992. { Check if the procedure directive is known }
  993. if not found then
  994. begin
  995. { parsing a procvar type the name can be any
  996. next variable !! }
  997. if (pdflags and (pd_procvar or pd_object))=0 then
  998. Message1(parser_w_unknown_proc_directive_ignored,name);
  999. exit;
  1000. end;
  1001. { static needs a special treatment }
  1002. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1003. exit;
  1004. { Conflicts between directives ? }
  1005. if (aktprocsym^.definition^.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1006. ((aktprocsym^.definition^.proccalloptions*proc_direcdata[p].mutexclpocall)<>[]) or
  1007. ((aktprocsym^.definition^.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1008. begin
  1009. Message1(parser_e_proc_dir_conflict,name);
  1010. exit;
  1011. end;
  1012. { Check if the directive is only for objects }
  1013. if ((proc_direcdata[p].pd_flags and pd_object)<>0) and
  1014. not assigned(aktprocsym^.definition^._class) then
  1015. begin
  1016. exit;
  1017. end;
  1018. { check if method and directive not for object public }
  1019. if ((proc_direcdata[p].pd_flags and pd_notobject)<>0) and
  1020. assigned(aktprocsym^.definition^._class) then
  1021. begin
  1022. exit;
  1023. end;
  1024. { consume directive, and turn flag on }
  1025. consume(token);
  1026. parse_proc_direc:=true;
  1027. { Check the pd_flags if the directive should be allowed }
  1028. if ((pdflags and pd_interface)<>0) and
  1029. ((proc_direcdata[p].pd_flags and pd_interface)=0) then
  1030. begin
  1031. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1032. exit;
  1033. end;
  1034. if ((pdflags and pd_implemen)<>0) and
  1035. ((proc_direcdata[p].pd_flags and pd_implemen)=0) then
  1036. begin
  1037. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1038. exit;
  1039. end;
  1040. if ((pdflags and pd_procvar)<>0) and
  1041. ((proc_direcdata[p].pd_flags and pd_procvar)=0) then
  1042. begin
  1043. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1044. exit;
  1045. end;
  1046. { Return the new pd_flags }
  1047. if (proc_direcdata[p].pd_flags and pd_body)=0 then
  1048. pdflags:=pdflags and (not pd_body);
  1049. if (proc_direcdata[p].pd_flags and pd_global)<>0 then
  1050. pdflags:=pdflags or pd_global;
  1051. { Add the correct flag }
  1052. aktprocsym^.definition^.proccalloptions:=aktprocsym^.definition^.proccalloptions+proc_direcdata[p].pocall;
  1053. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+proc_direcdata[p].pooption;
  1054. { Adjust positions of args for cdecl or stdcall }
  1055. if (aktprocsym^.definition^.deftype=procdef) and
  1056. (([pocall_cdecl,pocall_stdcall]*aktprocsym^.definition^.proccalloptions)<>[]) then
  1057. aktprocsym^.definition^.parast^.set_alignment(target_os.size_of_longint);
  1058. { Call the handler }
  1059. if pointer({$ifndef FPC}@{$endif}proc_direcdata[p].handler)<>nil then
  1060. proc_direcdata[p].handler(proc_names);
  1061. end;
  1062. {***************************************************************************}
  1063. function check_identical(var p : pprocdef) : boolean;
  1064. {
  1065. Search for idendical definitions,
  1066. if there is a forward, then kill this.
  1067. Returns the result of the forward check.
  1068. Removed from unter_dec to keep the source readable
  1069. }
  1070. var
  1071. hd,pd : Pprocdef;
  1072. storeparast : psymtable;
  1073. ad,fd : psym;
  1074. s : string;
  1075. begin
  1076. check_identical:=false;
  1077. p:=nil;
  1078. pd:=aktprocsym^.definition;
  1079. if assigned(pd) then
  1080. begin
  1081. { Is there an overload/forward ? }
  1082. if assigned(pd^.nextoverloaded) then
  1083. begin
  1084. { walk the procdef list }
  1085. while (assigned(pd)) and (assigned(pd^.nextoverloaded)) do
  1086. begin
  1087. if not(m_repeat_forward in aktmodeswitches) or
  1088. (equal_paras(aktprocsym^.definition^.para,pd^.nextoverloaded^.para,false) and
  1089. { for operators equal_paras is not enough !! }
  1090. ((aktprocsym^.definition^.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1091. is_equal(pd^.nextoverloaded^.retdef,aktprocsym^.definition^.retdef))) then
  1092. begin
  1093. if pd^.nextoverloaded^.forwarddef then
  1094. { remove the forward definition but don't delete it, }
  1095. { the symtable is the owner !! }
  1096. begin
  1097. hd:=pd^.nextoverloaded;
  1098. { Check if the procedure type and return type are correct }
  1099. if (hd^.proctypeoption<>aktprocsym^.definition^.proctypeoption) or
  1100. (not(is_equal(hd^.retdef,aktprocsym^.definition^.retdef)) and
  1101. (m_repeat_forward in aktmodeswitches)) then
  1102. begin
  1103. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName);
  1104. exit;
  1105. end;
  1106. { Check calling convention, no check for internconst,internproc which
  1107. are only defined in interface or implementation }
  1108. if (hd^.proccalloptions-[pocall_internconst,pocall_internproc]<>
  1109. aktprocsym^.definition^.proccalloptions-[pocall_internconst,pocall_internproc]) then
  1110. begin
  1111. { only trigger an error, becuase it doesn't hurt }
  1112. Message(parser_e_call_convention_dont_match_forward);
  1113. end;
  1114. { manglednames are equal? }
  1115. hd^.count:=false;
  1116. if (m_repeat_forward in aktmodeswitches) or
  1117. aktprocsym^.definition^.haspara then
  1118. begin
  1119. if (hd^.mangledname<>aktprocsym^.definition^.mangledname) then
  1120. begin
  1121. { When overloading is not possible then we issue an error }
  1122. { This is not true, tp7/delphi don't give an error when a renamed
  1123. type is used in the other declaration (PFV)
  1124. if not(m_repeat_forward in aktmodeswitches) then
  1125. begin
  1126. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName);
  1127. exit;
  1128. end; }
  1129. if not(po_external in aktprocsym^.definition^.procoptions) then
  1130. Message2(parser_n_interface_name_diff_implementation_name,hd^.mangledname,
  1131. aktprocsym^.definition^.mangledname);
  1132. { reset the mangledname of the interface part to be sure }
  1133. { this is wrong because the mangled name might have been used already !! }
  1134. if hd^.is_used then
  1135. renameasmsymbol(hd^.mangledname,aktprocsym^.definition^.mangledname);
  1136. hd^.setmangledname(aktprocsym^.definition^.mangledname);
  1137. { so we need to keep the name of interface !!
  1138. No!!!! The procedure directives can change the mangledname.
  1139. I fixed this by first calling check_identical and then doing
  1140. the proc directives, but this is not a good solution.(DM)}
  1141. { this is also wrong (PM)
  1142. aktprocsym^.definition^.setmangledname(hd^.mangledname);}
  1143. end
  1144. else
  1145. begin
  1146. { If mangled names are equal, therefore }
  1147. { they have the same number of parameters }
  1148. { Therefore we can check the name of these }
  1149. { parameters... }
  1150. if hd^.forwarddef and aktprocsym^.definition^.forwarddef then
  1151. begin
  1152. Message1(parser_e_function_already_declared_public_forward,aktprocsym^.demangledName);
  1153. Check_identical:=true;
  1154. { Remove other forward from the list to reduce errors }
  1155. pd^.nextoverloaded:=pd^.nextoverloaded^.nextoverloaded;
  1156. exit;
  1157. end;
  1158. ad:=psym(hd^.parast^.symindex^.first);
  1159. fd:=psym(aktprocsym^.definition^.parast^.symindex^.first);
  1160. if assigned(ad) and assigned(fd) then
  1161. begin
  1162. while assigned(ad) and assigned(fd) do
  1163. begin
  1164. s:=ad^.name;
  1165. if s<>fd^.name then
  1166. begin
  1167. Message3(parser_e_header_different_var_names,
  1168. aktprocsym^.name,s,fd^.name);
  1169. break;
  1170. end;
  1171. { it is impossible to have a nil pointer }
  1172. { for only one parameter - since they }
  1173. { have the same number of parameters. }
  1174. { Left = next parameter. }
  1175. ad:=psym(ad^.left);
  1176. fd:=psym(fd^.left);
  1177. end;
  1178. end;
  1179. end;
  1180. end;
  1181. { also the call_offset }
  1182. hd^.parast^.address_fixup:=aktprocsym^.definition^.parast^.address_fixup;
  1183. hd^.count:=true;
  1184. { remove pd^.nextoverloaded from the list }
  1185. { and add aktprocsym^.definition }
  1186. pd^.nextoverloaded:=pd^.nextoverloaded^.nextoverloaded;
  1187. hd^.nextoverloaded:=aktprocsym^.definition^.nextoverloaded;
  1188. { Alert! All fields of aktprocsym^.definition that are modified
  1189. by the procdir handlers must be copied here!.}
  1190. hd^.forwarddef:=false;
  1191. hd^.proccalloptions:=hd^.proccalloptions + aktprocsym^.definition^.proccalloptions;
  1192. hd^.procoptions:=hd^.procoptions + aktprocsym^.definition^.procoptions;
  1193. if aktprocsym^.definition^.extnumber=-1 then
  1194. aktprocsym^.definition^.extnumber:=hd^.extnumber
  1195. else
  1196. if hd^.extnumber=-1 then
  1197. hd^.extnumber:=aktprocsym^.definition^.extnumber;
  1198. { switch parast for warning in implementation PM }
  1199. if (m_repeat_forward in aktmodeswitches) or
  1200. aktprocsym^.definition^.haspara then
  1201. begin
  1202. storeparast:=hd^.parast;
  1203. hd^.parast:=aktprocsym^.definition^.parast;
  1204. aktprocsym^.definition^.parast:=storeparast;
  1205. end;
  1206. if pd=aktprocsym^.definition then
  1207. p:=nil
  1208. else
  1209. p:=pd;
  1210. aktprocsym^.definition:=hd;
  1211. check_identical:=true;
  1212. end
  1213. else
  1214. { abstract methods aren't forward defined, but this }
  1215. { needs another error message }
  1216. if not(po_abstractmethod in pd^.nextoverloaded^.procoptions) then
  1217. Message(parser_e_overloaded_have_same_parameters)
  1218. else
  1219. Message(parser_e_abstract_no_definition);
  1220. break;
  1221. end;
  1222. pd:=pd^.nextoverloaded;
  1223. end;
  1224. end
  1225. else
  1226. begin
  1227. { there is no overloaded, so its always identical with itself }
  1228. check_identical:=true;
  1229. end;
  1230. end;
  1231. { insert opsym only in the right symtable }
  1232. if ((procinfo^.flags and pi_operator)<>0) and assigned(opsym)
  1233. and not parse_only then
  1234. begin
  1235. if ret_in_param(aktprocsym^.definition^.retdef) then
  1236. begin
  1237. pprocdef(aktprocsym^.definition)^.parast^.insert(opsym);
  1238. { this increases the data size }
  1239. { correct this to get the right ret $value }
  1240. dec(pprocdef(aktprocsym^.definition)^.parast^.datasize,opsym^.getpushsize);
  1241. { this allows to read the funcretoffset }
  1242. opsym^.address:=-4;
  1243. opsym^.varspez:=vs_var;
  1244. end
  1245. else
  1246. pprocdef(aktprocsym^.definition)^.localst^.insert(opsym);
  1247. end;
  1248. end;
  1249. procedure compile_proc_body(const proc_names:Tstringcontainer;
  1250. make_global,parent_has_class:boolean);
  1251. {
  1252. Compile the body of a procedure
  1253. }
  1254. var
  1255. oldexitlabel,oldexit2label : pasmlabel;
  1256. oldfaillabel,oldquickexitlabel:Pasmlabel;
  1257. _class,hp:Pobjectdef;
  1258. { switches can change inside the procedure }
  1259. entryswitches, exitswitches : tlocalswitches;
  1260. { code for the subroutine as tree }
  1261. {$ifdef newcg}
  1262. code:pnode;
  1263. {$else newcg}
  1264. code:ptree;
  1265. {$endif newcg}
  1266. { size of the local strackframe }
  1267. stackframe:longint;
  1268. { true when no stackframe is required }
  1269. nostackframe:boolean;
  1270. { number of bytes which have to be cleared by RET }
  1271. parasize:longint;
  1272. { filepositions }
  1273. entrypos,
  1274. savepos,
  1275. exitpos : tfileposinfo;
  1276. begin
  1277. { calculate the lexical level }
  1278. inc(lexlevel);
  1279. if lexlevel>32 then
  1280. Message(parser_e_too_much_lexlevel);
  1281. { static is also important for local procedures !! }
  1282. if (po_staticmethod in aktprocsym^.definition^.procoptions) then
  1283. allow_only_static:=true
  1284. else if (lexlevel=normal_function_level) then
  1285. allow_only_static:=false;
  1286. { save old labels }
  1287. oldexitlabel:=aktexitlabel;
  1288. oldexit2label:=aktexit2label;
  1289. oldquickexitlabel:=quickexitlabel;
  1290. oldfaillabel:=faillabel;
  1291. { get new labels }
  1292. getlabel(aktexitlabel);
  1293. getlabel(aktexit2label);
  1294. { exit for fail in constructors }
  1295. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1296. begin
  1297. getlabel(faillabel);
  1298. getlabel(quickexitlabel);
  1299. end;
  1300. { reset break and continue labels }
  1301. block_type:=bt_general;
  1302. aktbreaklabel:=nil;
  1303. aktcontinuelabel:=nil;
  1304. { insert symtables for the class, by only if it is no nested function }
  1305. if assigned(procinfo^._class) and not(parent_has_class) then
  1306. begin
  1307. { insert them in the reverse order ! }
  1308. hp:=nil;
  1309. repeat
  1310. _class:=procinfo^._class;
  1311. while _class^.childof<>hp do
  1312. _class:=_class^.childof;
  1313. hp:=_class;
  1314. _class^.symtable^.next:=symtablestack;
  1315. symtablestack:=_class^.symtable;
  1316. until hp=procinfo^._class;
  1317. end;
  1318. { insert parasymtable in symtablestack}
  1319. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  1320. for checking of same names used in interface and implementation !! }
  1321. if lexlevel>=normal_function_level then
  1322. begin
  1323. aktprocsym^.definition^.parast^.next:=symtablestack;
  1324. symtablestack:=aktprocsym^.definition^.parast;
  1325. symtablestack^.symtablelevel:=lexlevel;
  1326. end;
  1327. { insert localsymtable in symtablestack}
  1328. aktprocsym^.definition^.localst^.next:=symtablestack;
  1329. symtablestack:=aktprocsym^.definition^.localst;
  1330. symtablestack^.symtablelevel:=lexlevel;
  1331. { constant symbols are inserted in this symboltable }
  1332. constsymtable:=symtablestack;
  1333. { reset the temporary memory }
  1334. cleartempgen;
  1335. {$ifdef newcg}
  1336. tg.usedinproc:=[];
  1337. {$else newcg}
  1338. { no registers are used }
  1339. usedinproc:=0;
  1340. {$endif newcg}
  1341. { save entry info }
  1342. entrypos:=aktfilepos;
  1343. entryswitches:=aktlocalswitches;
  1344. {$ifdef newcg}
  1345. { parse the code ... }
  1346. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1347. code:=convtree2node(assembler_block)
  1348. else
  1349. code:=convtree2node(block(current_module^.islibrary));
  1350. {$else newcg}
  1351. { parse the code ... }
  1352. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1353. code:=assembler_block
  1354. else
  1355. code:=block(current_module^.islibrary);
  1356. {$endif newcg}
  1357. { get a better entry point }
  1358. if assigned(code) then
  1359. entrypos:=code^.fileinfo;
  1360. { save exit info }
  1361. exitswitches:=aktlocalswitches;
  1362. exitpos:=last_endtoken_filepos;
  1363. { save current filepos }
  1364. savepos:=aktfilepos;
  1365. {When we are called to compile the body of a unit, aktprocsym should
  1366. point to the unit initialization. If the unit has no initialization,
  1367. aktprocsym=nil. But in that case code=nil. hus we should check for
  1368. code=nil, when we use aktprocsym.}
  1369. { set the framepointer to esp for assembler functions }
  1370. { but only if the are no local variables }
  1371. { already done in assembler_block }
  1372. {$ifdef newcg}
  1373. tg.setfirsttemp(procinfo^.firsttemp);
  1374. {$else newcg}
  1375. setfirsttemp(procinfo^.firsttemp);
  1376. {$endif newcg}
  1377. { ... and generate assembler }
  1378. { but set the right switches for entry !! }
  1379. aktlocalswitches:=entryswitches;
  1380. {$ifndef NOPASS2}
  1381. {$ifdef newcg}
  1382. tg.setfirsttemp(procinfo^.firsttemp);
  1383. {$else newcg}
  1384. if assigned(code) then
  1385. generatecode(code);
  1386. {$endif newcg}
  1387. { set switches to status at end of procedure }
  1388. aktlocalswitches:=exitswitches;
  1389. if assigned(code) then
  1390. begin
  1391. aktprocsym^.definition^.code:=code;
  1392. { the procedure is now defined }
  1393. aktprocsym^.definition^.forwarddef:=false;
  1394. {$ifdef newcg}
  1395. aktprocsym^.definition^.usedregisters:=tg.usedinproc;
  1396. {$else newcg}
  1397. aktprocsym^.definition^.usedregisters:=usedinproc;
  1398. {$endif newcg}
  1399. end;
  1400. {$ifdef newcg}
  1401. stackframe:=tg.gettempsize;
  1402. {$else newcg}
  1403. stackframe:=gettempsize;
  1404. {$endif newcg}
  1405. { first generate entry code with the correct position and switches }
  1406. aktfilepos:=entrypos;
  1407. aktlocalswitches:=entryswitches;
  1408. {$ifdef newcg}
  1409. if assigned(code) then
  1410. cg^.g_entrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  1411. {$else newcg}
  1412. if assigned(code) then
  1413. genentrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  1414. {$endif newcg}
  1415. { now generate exit code with the correct position and switches }
  1416. aktfilepos:=exitpos;
  1417. aktlocalswitches:=exitswitches;
  1418. if assigned(code) then
  1419. begin
  1420. {$ifdef newcg}
  1421. cg^.g_exitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  1422. {$else newcg}
  1423. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  1424. {$endif newcg}
  1425. procinfo^.aktproccode^.insertlist(procinfo^.aktentrycode);
  1426. procinfo^.aktproccode^.concatlist(procinfo^.aktexitcode);
  1427. {$ifdef i386}
  1428. {$ifndef NoOpt}
  1429. if (cs_optimize in aktglobalswitches) and
  1430. { do not optimize pure assembler procedures }
  1431. ((procinfo^.flags and pi_is_assembler)=0) then
  1432. Optimize(procinfo^.aktproccode);
  1433. {$endif NoOpt}
  1434. {$endif}
  1435. { save local data (casetable) also in the same file }
  1436. if assigned(procinfo^.aktlocaldata) and
  1437. (not procinfo^.aktlocaldata^.empty) then
  1438. begin
  1439. procinfo^.aktproccode^.concat(new(pai_section,init(sec_data)));
  1440. procinfo^.aktproccode^.concatlist(procinfo^.aktlocaldata);
  1441. end;
  1442. { now we can insert a cut }
  1443. if (cs_create_smart in aktmoduleswitches) then
  1444. codesegment^.concat(new(pai_cut,init));
  1445. { add the procedure to the codesegment }
  1446. codesegment^.concatlist(procinfo^.aktproccode);
  1447. end;
  1448. {$else}
  1449. if assigned(code) then
  1450. firstpass(code);
  1451. {$endif NOPASS2}
  1452. { ... remove symbol tables, for the browser leave the static table }
  1453. { if (cs_browser in aktmoduleswitches) and (symtablestack^.symtabletype=staticsymtable) then
  1454. symtablestack^.next:=symtablestack^.next^.next
  1455. else }
  1456. if lexlevel>=normal_function_level then
  1457. symtablestack:=symtablestack^.next^.next
  1458. else
  1459. symtablestack:=symtablestack^.next;
  1460. { ... check for unused symbols }
  1461. { but only if there is no asm block }
  1462. if assigned(code) then
  1463. begin
  1464. if (Errorcount=0) then
  1465. begin
  1466. aktprocsym^.definition^.localst^.check_forwards;
  1467. aktprocsym^.definition^.localst^.checklabels;
  1468. end;
  1469. if (procinfo^.flags and pi_uses_asm)=0 then
  1470. begin
  1471. { not for unit init, becuase the var can be used in finalize,
  1472. it will be done in proc_unit }
  1473. if not(aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1474. aktprocsym^.definition^.localst^.allsymbolsused;
  1475. aktprocsym^.definition^.parast^.allsymbolsused;
  1476. end;
  1477. end;
  1478. { the local symtables can be deleted, but the parast }
  1479. { doesn't, (checking definitons when calling a }
  1480. { function }
  1481. { not for a inline procedure !! (PM) }
  1482. { at lexlevel = 1 localst is the staticsymtable itself }
  1483. { so no dispose here !! }
  1484. if assigned(code) and
  1485. not(cs_browser in aktmoduleswitches) and
  1486. not(pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1487. begin
  1488. if lexlevel>=normal_function_level then
  1489. dispose(aktprocsym^.definition^.localst,done);
  1490. aktprocsym^.definition^.localst:=nil;
  1491. end;
  1492. { only now we can remove the temps }
  1493. resettempgen;
  1494. { remove code tree, if not inline procedure }
  1495. if assigned(code) and not(pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1496. {$ifdef newcg}
  1497. dispose(code,done);
  1498. {$else newcg}
  1499. disposetree(code);
  1500. {$endif newcg}
  1501. { remove class member symbol tables }
  1502. while symtablestack^.symtabletype=objectsymtable do
  1503. symtablestack:=symtablestack^.next;
  1504. { restore filepos, the switches are already set }
  1505. aktfilepos:=savepos;
  1506. { free labels }
  1507. freelabel(aktexitlabel);
  1508. freelabel(aktexit2label);
  1509. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1510. begin
  1511. freelabel(faillabel);
  1512. freelabel(quickexitlabel);
  1513. end;
  1514. { restore labels }
  1515. aktexitlabel:=oldexitlabel;
  1516. aktexit2label:=oldexit2label;
  1517. quickexitlabel:=oldquickexitlabel;
  1518. faillabel:=oldfaillabel;
  1519. { reset to normal non static function }
  1520. if (lexlevel=normal_function_level) then
  1521. allow_only_static:=false;
  1522. { previous lexlevel }
  1523. dec(lexlevel);
  1524. end;
  1525. procedure parse_proc_directives(Anames:Pstringcontainer;var pdflags:word);
  1526. {
  1527. Parse the procedure directives. It does not matter if procedure directives
  1528. are written using ;procdir; or ['procdir'] syntax.
  1529. }
  1530. var
  1531. res : boolean;
  1532. begin
  1533. while token in [_ID,_LECKKLAMMER] do
  1534. begin
  1535. if try_to_consume(_LECKKLAMMER) then
  1536. begin
  1537. repeat
  1538. parse_proc_direc(Anames^,pdflags);
  1539. until not try_to_consume(_COMMA);
  1540. consume(_RECKKLAMMER);
  1541. { we always expect at least '[];' }
  1542. res:=true;
  1543. end
  1544. else
  1545. res:=parse_proc_direc(Anames^,pdflags);
  1546. { A procedure directive normally followed by a semicolon, but in
  1547. a const section we should stop when _EQUAL is found }
  1548. if res then
  1549. begin
  1550. if (block_type=bt_const) and
  1551. (token=_EQUAL) then
  1552. break;
  1553. consume(_SEMICOLON);
  1554. end
  1555. else
  1556. break;
  1557. end;
  1558. end;
  1559. procedure parse_var_proc_directives(var sym : psym);
  1560. var
  1561. anames : pstringcontainer;
  1562. pdflags : word;
  1563. oldsym : pprocsym;
  1564. pd : pabstractprocdef;
  1565. begin
  1566. oldsym:=aktprocsym;
  1567. anames:=new(pstringcontainer,init);
  1568. pdflags:=pd_procvar;
  1569. { we create a temporary aktprocsym to read the directives }
  1570. aktprocsym:=new(pprocsym,init(sym^.name));
  1571. case sym^.typ of
  1572. varsym :
  1573. pd:=pabstractprocdef(pvarsym(sym)^.definition);
  1574. typedconstsym :
  1575. pd:=pabstractprocdef(ptypedconstsym(sym)^.definition);
  1576. typesym :
  1577. pd:=pabstractprocdef(ptypesym(sym)^.definition);
  1578. else
  1579. internalerror(994932432);
  1580. end;
  1581. if pd^.deftype<>procvardef then
  1582. internalerror(994932433);
  1583. pabstractprocdef(aktprocsym^.definition):=pd;
  1584. { names should never be used anyway }
  1585. inc(lexlevel);
  1586. parse_proc_directives(anames,pdflags);
  1587. dec(lexlevel);
  1588. aktprocsym^.definition:=nil;
  1589. dispose(aktprocsym,done);
  1590. dispose(anames,done);
  1591. aktprocsym:=oldsym;
  1592. end;
  1593. procedure parse_object_proc_directives(var sym : pprocsym);
  1594. var
  1595. anames : pstringcontainer;
  1596. pdflags : word;
  1597. begin
  1598. pdflags:=pd_object;
  1599. anames:=new(pstringcontainer,init);
  1600. inc(lexlevel);
  1601. parse_proc_directives(anames,pdflags);
  1602. dec(lexlevel);
  1603. dispose(anames,done);
  1604. if (po_containsself in aktprocsym^.definition^.procoptions) and
  1605. (([po_msgstr,po_msgint]*aktprocsym^.definition^.procoptions)=[]) then
  1606. Message(parser_e_self_in_non_message_handler);
  1607. end;
  1608. procedure checkvaluepara(p:pnamedindexobject);{$ifndef FPC}far;{$endif}
  1609. var
  1610. vs : pvarsym;
  1611. s : string;
  1612. begin
  1613. with pvarsym(p)^ do
  1614. begin
  1615. if copy(name,1,3)='val' then
  1616. begin
  1617. s:=Copy(name,4,255);
  1618. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  1619. begin
  1620. vs:=new(Pvarsym,init(s,definition));
  1621. vs^.fileinfo:=fileinfo;
  1622. vs^.varspez:=varspez;
  1623. aktprocsym^.definition^.localst^.insert(vs);
  1624. vs^.islocalcopy:=true;
  1625. vs^.varstate:=vs_used;
  1626. localvarsym:=vs;
  1627. end
  1628. else
  1629. begin
  1630. aktprocsym^.definition^.parast^.rename(name,s);
  1631. end;
  1632. end;
  1633. end;
  1634. end;
  1635. procedure read_proc;
  1636. {
  1637. Parses the procedure directives, then parses the procedure body, then
  1638. generates the code for it
  1639. }
  1640. var
  1641. oldprefix : string;
  1642. oldprocsym : Pprocsym;
  1643. oldprocinfo : pprocinfo;
  1644. oldconstsymtable : Psymtable;
  1645. oldfilepos : tfileposinfo;
  1646. names : Pstringcontainer;
  1647. pdflags : word;
  1648. prevdef,stdef : pprocdef;
  1649. begin
  1650. { save old state }
  1651. oldprocsym:=aktprocsym;
  1652. oldprefix:=procprefix;
  1653. oldconstsymtable:=constsymtable;
  1654. oldprocinfo:=procinfo;
  1655. { create a new procedure }
  1656. new(names,init);
  1657. codegen_newprocedure;
  1658. with procinfo^ do
  1659. begin
  1660. parent:=oldprocinfo;
  1661. { clear flags }
  1662. flags:=0;
  1663. { standard frame pointer }
  1664. framepointer:=frame_pointer;
  1665. funcret_is_valid:=false;
  1666. { is this a nested function of a method ? }
  1667. if assigned(oldprocinfo) then
  1668. _class:=oldprocinfo^._class;
  1669. end;
  1670. parse_proc_dec;
  1671. { set the default function options }
  1672. if parse_only then
  1673. begin
  1674. aktprocsym^.definition^.forwarddef:=true;
  1675. { set also the interface flag, for better error message when the
  1676. implementation doesn't much this header }
  1677. aktprocsym^.definition^.interfacedef:=true;
  1678. pdflags:=pd_interface;
  1679. end
  1680. else
  1681. begin
  1682. pdflags:=pd_body;
  1683. if current_module^.in_implementation then
  1684. pdflags:=pdflags or pd_implemen;
  1685. if (not current_module^.is_unit) or (cs_create_smart in aktmoduleswitches) then
  1686. pdflags:=pdflags or pd_global;
  1687. procinfo^.exported:=false;
  1688. aktprocsym^.definition^.forwarddef:=false;
  1689. end;
  1690. { parse the directives that may follow }
  1691. inc(lexlevel);
  1692. parse_proc_directives(names,pdflags);
  1693. dec(lexlevel);
  1694. { set aktfilepos to the beginning of the function declaration }
  1695. oldfilepos:=aktfilepos;
  1696. aktfilepos:=aktprocsym^.definition^.fileinfo;
  1697. { search for forward declarations }
  1698. if not check_identical(prevdef) then
  1699. begin
  1700. { A method must be forward defined (in the object declaration) }
  1701. if assigned(procinfo^._class) and (not assigned(oldprocinfo^._class)) then
  1702. Message(parser_e_header_dont_match_any_member);
  1703. { Give a better error if there is a forward def in the interface and only
  1704. a single implementation }
  1705. if (not aktprocsym^.definition^.forwarddef) and
  1706. assigned(aktprocsym^.definition^.nextoverloaded) and
  1707. aktprocsym^.definition^.nextoverloaded^.forwarddef and
  1708. aktprocsym^.definition^.nextoverloaded^.interfacedef and
  1709. not(assigned(aktprocsym^.definition^.nextoverloaded^.nextoverloaded)) then
  1710. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName)
  1711. else
  1712. begin
  1713. { check the global flag }
  1714. if (procinfo^.flags and pi_is_global)<>0 then
  1715. Message(parser_e_overloaded_must_be_all_global);
  1716. end
  1717. end;
  1718. { set return type here, becuase the aktprocsym^.definition can be
  1719. changed by check_identical (PFV) }
  1720. procinfo^.retdef:=aktprocsym^.definition^.retdef;
  1721. { pointer to the return value ? }
  1722. if ret_in_param(procinfo^.retdef) then
  1723. begin
  1724. procinfo^.retoffset:=procinfo^.call_offset;
  1725. inc(procinfo^.call_offset,target_os.size_of_pointer);
  1726. end;
  1727. { allows to access the parameters of main functions in nested functions }
  1728. aktprocsym^.definition^.parast^.address_fixup:=procinfo^.call_offset;
  1729. { when it is a value para and it needs a local copy then rename
  1730. the parameter and insert a copy in the localst. This is not done
  1731. for assembler procedures }
  1732. if (not parse_only) and (not aktprocsym^.definition^.forwarddef) then
  1733. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}checkvaluepara);
  1734. { restore file pos }
  1735. aktfilepos:=oldfilepos;
  1736. { compile procedure when a body is needed }
  1737. if (pdflags and pd_body)<>0 then
  1738. begin
  1739. Message1(parser_p_procedure_start,aktprocsym^.demangledname);
  1740. names^.insert(aktprocsym^.definition^.mangledname);
  1741. { set _FAIL as keyword if constructor }
  1742. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1743. tokeninfo^[_FAIL].keyword:=m_all;
  1744. if assigned(aktprocsym^.definition^._class) then
  1745. tokeninfo^[_SELF].keyword:=m_all;
  1746. compile_proc_body(names^,((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  1747. { reset _FAIL as normal }
  1748. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1749. tokeninfo^[_FAIL].keyword:=m_none;
  1750. if assigned(aktprocsym^.definition^._class) and (lexlevel=main_program_level) then
  1751. tokeninfo^[_SELF].keyword:=m_none;
  1752. consume(_SEMICOLON);
  1753. end;
  1754. { close }
  1755. dispose(names,done);
  1756. codegen_doneprocedure;
  1757. { Restore old state }
  1758. constsymtable:=oldconstsymtable;
  1759. { from now on all refernece to mangledname means
  1760. that the function is already used }
  1761. aktprocsym^.definition^.count:=true;
  1762. { restore the interface order to maintain CRC values PM }
  1763. if assigned(prevdef) and assigned(aktprocsym^.definition^.nextoverloaded) then
  1764. begin
  1765. stdef:=aktprocsym^.definition;
  1766. aktprocsym^.definition:=stdef^.nextoverloaded;
  1767. stdef^.nextoverloaded:=prevdef^.nextoverloaded;
  1768. prevdef^.nextoverloaded:=stdef;
  1769. end;
  1770. aktprocsym:=oldprocsym;
  1771. procprefix:=oldprefix;
  1772. procinfo:=oldprocinfo;
  1773. opsym:=nil;
  1774. end;
  1775. end.
  1776. {
  1777. $Log$
  1778. Revision 1.34 1999-11-10 00:24:02 pierre
  1779. * more browser details
  1780. Revision 1.33 1999/11/09 23:43:08 pierre
  1781. * better browser info
  1782. Revision 1.32 1999/11/09 23:06:45 peter
  1783. * esi_offset -> selfpointer_offset to be newcg compatible
  1784. * hcogegen -> cgbase fixes for newcg
  1785. Revision 1.31 1999/11/06 14:34:23 peter
  1786. * truncated log to 20 revs
  1787. Revision 1.30 1999/10/26 12:30:44 peter
  1788. * const parameter is now checked
  1789. * better and generic check if a node can be used for assigning
  1790. * export fixes
  1791. * procvar equal works now (it never had worked at least from 0.99.8)
  1792. * defcoll changed to linkedlist with pparaitem so it can easily be
  1793. walked both directions
  1794. Revision 1.29 1999/10/22 10:39:35 peter
  1795. * split type reading from pdecl to ptype unit
  1796. * parameter_dec routine is now used for procedure and procvars
  1797. Revision 1.28 1999/10/13 10:37:36 peter
  1798. * moved mangledname creation of normal proc so it also handles a wrong
  1799. method proc
  1800. Revision 1.27 1999/10/12 21:20:46 florian
  1801. * new codegenerator compiles again
  1802. Revision 1.26 1999/10/03 19:38:39 peter
  1803. * fixed forward decl check for tp7/delphi
  1804. Revision 1.25 1999/10/01 10:05:44 peter
  1805. + procedure directive support in const declarations, fixes bug 232
  1806. Revision 1.24 1999/10/01 08:02:47 peter
  1807. * forward type declaration rewritten
  1808. Revision 1.23 1999/09/27 23:44:56 peter
  1809. * procinfo is now a pointer
  1810. * support for result setting in sub procedure
  1811. Revision 1.22 1999/09/20 16:39:00 peter
  1812. * cs_create_smart instead of cs_smartlink
  1813. * -CX is create smartlink
  1814. * -CD is create dynamic, but does nothing atm.
  1815. Revision 1.21 1999/09/15 20:35:42 florian
  1816. * small fix to operator overloading when in MMX mode
  1817. + the compiler uses now fldz and fld1 if possible
  1818. + some fixes to floating point registers
  1819. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  1820. * .... ???
  1821. Revision 1.20 1999/09/10 18:48:09 florian
  1822. * some bug fixes (e.g. must_be_valid and procinfo^.funcret_is_valid)
  1823. * most things for stored properties fixed
  1824. Revision 1.19 1999/09/07 14:59:40 pierre
  1825. * bugfix for 237 and 244
  1826. Revision 1.18 1999/09/02 18:47:45 daniel
  1827. * Could not compile with TP, some arrays moved to heap
  1828. * NOAG386BIN default for TP
  1829. * AG386* files were not compatible with TP, fixed.
  1830. Revision 1.17 1999/08/30 10:17:57 peter
  1831. * fixed crash in psub
  1832. * ansistringcompare fixed
  1833. * support for #$0b8
  1834. Revision 1.16 1999/08/27 10:41:46 pierre
  1835. * pi_is_global was missing for global methods
  1836. + code to restore overloaded list order
  1837. (necessary to get same CRC values after interface and after
  1838. implementation)
  1839. Revision 1.15 1999/08/19 13:02:11 pierre
  1840. + label faillabel added for _FAIL support
  1841. Revision 1.14 1999/08/10 16:24:44 pierre
  1842. * linking to C code with cdecl;external; was broken
  1843. Revision 1.13 1999/08/10 12:37:44 pierre
  1844. + procsym field of tprocdef set
  1845. Revision 1.12 1999/08/05 16:53:06 peter
  1846. * V_Fatal=1, all other V_ are also increased
  1847. * Check for local procedure when assigning procvar
  1848. * fixed comment parsing because directives
  1849. * oldtp mode directives better supported
  1850. * added some messages to errore.msg
  1851. Revision 1.11 1999/08/04 13:03:01 jonas
  1852. * all tokens now start with an underscore
  1853. * PowerPC compiles!!
  1854. }