psub.pas 71 KB

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