psub.pas 68 KB

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